Solved

PHP Equivalent to html FORM LISTS

Posted on 2004-09-19
8
208 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
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…
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

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