Jump to content

[SOLVED] populating drop down menu


denoteone

Recommended Posts

I have a table named strata_links and  field named 'name'    I would like for the php to query the database and create an array. I think i am going wrong with the query statement. it should just show a list of names with the value being the actual name itself. any help would be great thanks everyone.

 

 

<?
include "connect.php";

$query="SELECT * from strata_links order by name, name";

$result=mysql_query($query) or die ("Unable to Make the Query:" . mysql_error() );


echo "<form name='form1' method='post' action='script.php'>\n";

echo "<select name='imagename'>\n";

while ($result = mysql_fetch_assoc($query))
{
echo "<option value='".$result['name']."'>".$data['name']."</option>\n";

echo "</select>\n";

echo "<input type='submit' name='submit' value='Submit'>\n";

echo "</form>\n";
}
?>

Link to comment
Share on other sites

Not sure where you are getting $data from

 

echo "<option value={$result['name']}>{$result['name']}</option>\n";

 

Also, take your submit out of the loop unless you want a submit for every option value.

 

Can also do

 

echo "<option>{$result['name']}</option>\n";

 

Pretty sure the value defaults to the field if it's omitted.

 

 

Link to comment
Share on other sites

the "&data" was supposed to read $result.

 

I guess I want to make sure my mySql syntax is correct.

I have taken the submit button out of the loop as well.

 

here is my code now.

 

<?
include "connect.php";

$query="SELECT * from strata_links order by name, name";

$result=mysql_query($query) or die ("Unable to Make the Query:" . mysql_error() );


echo "<form name='form1' method='post' action='script.php'>\n";

echo "<select name='people'>\n";

while ($result = mysql_fetch_assoc($query))
{
echo "<option>"{$result['name']}"</option>\n";
}
echo "</select>\n";

echo "<input type='submit' name='submit' value='Submit'>\n";

echo "</form>\n";

?>

Link to comment
Share on other sites

You need to change some things around.

 

 

<?php
include "connect.php";

$query="SELECT * from strata_links order by name, name";

$result=mysql_query($query) or die ("Unable to Make the Query:" . mysql_error() );


echo "<form name='form1' method='post' action='script.php'>\n";

echo "<select name='people'>\n";

while ($row = mysql_fetch_assoc($result))
{
echo "<option>{$row['name']}</option>\n";
}
echo "</select>\n";

echo "<input type='submit' name='submit' value='Submit'>\n";

echo "</form>\n";

?> 

Link to comment
Share on other sites

Sorry i thought that I had to have the name of the field to get the array from in the mySQL query. In this case the name for the field is "name"

 

In any case thanks I have it working.

 

??? before I start looking is it even possible to put the mySQL array in alphabetical order? before it is put in the drop down menu

 

 

 

Link to comment
Share on other sites

ok so I have my drop down but the value of  <option> is not being sent to the script.php page.

 

 

here is the drop down page...

 

<?php
include "connect.php";

$query="SELECT * from strata_links order by name";

$result=mysql_query($query) or die ("Unable to Make the Query:" . mysql_error() );


echo "<form name='form1' method='post' action='script.php'>\n";

echo "<select name='people'>\n";

while ($row = mysql_fetch_assoc($result))
{
echo "<option name='option'>{$row['description']}</option>\n";
}
echo "</select>\n";

echo "<input type='submit' name='submit' value='Submit'>\n";

echo "</form>\n";

?>

 

 

 

Here is the script page that should show the value associated with the drop down menu <option>

 

<?
include "connect.php";


if (isset($_POST['submit'])) // name of submit button
{
$choice = $POST_['option'];
echo $choice;
} 
else{
print "<center>";
  print "<table>";
  print "<tr><td><center>please select a File first</center></td></tr>";
  print "<tr><td><center>";   
  print "Wrong username or password or unactivated account, redirecting back to login page... <META HTTP-EQUIV = 'Refresh' Content = '2; URL =getimage.php'></center>";
  print "</td></tr></table></center>";
  }

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.