?
Solved

PHP Equivalent to html FORM LISTS

Posted on 2004-09-19
8
Medium Priority
?
233 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 1000 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 1000 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

777 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