Solved

PHP Equivalent to html FORM LISTS

Posted on 2004-09-19
8
230 Views
Last Modified: 2008-03-06
Hi,

Is it possible to create a list box in PHP that contains the data retrieved from a database "field=domains", then a Submit button which is basically a link bank to the same page with the selection...

So...

domain1.com   would return 0
domain2.com   would return 1
domain3.com   would return 2

and so on, once the SUBMIT link it clicked it would reload the same page but as http://www.domain.com/test.php?domainselected=<number of result>

Cheers
0
Comment
Question by:MirageSF
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
8 Comments
 
LVL 6

Expert Comment

by:peyox
ID: 12098618
Yes, it is possible:

http://www.experts-exchange.com/Web/Web_Languages/PHP/Q_21136994.html

If you need further assistance, let us know!
0
 
LVL 26

Accepted Solution

by:
Umesh earned 250 total points
ID: 12099319
try this..


$dbLink = mysql_connect("yourhostname","username","secret-password");

$sQuery = @mysql_query("SELECT domains from  tableName ",$dbLink);

print("<form name=myForm action='$_SERVER[PHP_SELF]' method='GET'>");
print("<SELECT name='domainselected'>");
$i=0;
while($row=mysql_fetch_row($sQuery))
{

   print("<OPTION VALUE='$i'>$row[0]</OPTION>"); // would return 0 1 2 etc
  $i++;
}

print("</SELECT>");
print("<INPUT TYPE=SUBMIT name='sub' value='submit'>");
print("</form>");
0
 
LVL 27

Assisted Solution

by:Diablo84
Diablo84 earned 250 total points
ID: 12100415
This is my approach to it, similar to the above. It also remembers which domain was selected when the form is submitted.

Note, i have used the post method rather then get because its a slightly better method unless there is a specific reason why you want to use the query string. If you do want to use get change the forms method to "get" and the two instances of $_POST to $_GET.

<?php
//included the connection code up here as you may already have it
 $conn = mysql_connect("host","username","password") or die(mysql_error());
 mysql_select_db("db_name",$conn) or die(mysql_error());
?>

<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>";>
 <select name="domain">
 <option value="">Please Select</option>

<?php
 $query = mysql_query("SELECT * FROM tablename") or die(mysql_error());
 
 $num = 0;

 while ($row = mysql_fetch_assoc($query)) {
  echo "<option value=\"$num\"";
  if (isset($_POST['domain']) && $_POST['domain'] == $num) echo " selected";
  echo ">".$row["domains"]."</option>\n";
 }
?>

 </select>
</form>
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 27

Expert Comment

by:Diablo84
ID: 12100423
sorry, this section should be

<?php
 $query = mysql_query("SELECT * FROM tablename") or die(mysql_error());
 
 $num = 0;

 while ($row = mysql_fetch_assoc($query)) {
  echo "<option value=\"$num\"";
  if (isset($_POST['domain']) && $_POST['domain'] == $num) echo " selected";
  echo ">".$row["domains"]."</option>\n";
  $num++;
 }
?>

forgot to increment.

0
 
LVL 27

Expert Comment

by:Diablo84
ID: 12492455
I object to the 3 way split.

My recommendation is split
ushastry {http:#12099319} & Diablo84 {http:#12100415}

The first link posted by peyox references my answer in another thread and i do not believe that pointing to another experts answer in another thread via a link warrants the award of expert points especially as it was a straight forward question, laziness does not signify expertise.
0
 
LVL 6

Expert Comment

by:peyox
ID: 12494522
Instead of pasting code every time I provide url to the right topic if user cannot or don't want to search for it.
However I agree with Diablo's comment.

Good luck guys!
0

Featured Post

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question