Solved

PHP Equivalent to html FORM LISTS

Posted on 2004-09-19
8
189 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
  • 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:
ushastry 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

744 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now