monty187 Posted June 4, 2007 Share Posted June 4, 2007 Hi Guys, Im relitively new to php but I am currently trying to create a drop menu that will decide what contents are displayed on screen. so i need a drop menu and then when the user selects an option from the list an sql query uses the selection value to get details from the db, The problem is I cant seem to get the value out of the list to use in the query. Here is my code so far, any suggestions much appreciated <form action="<? echo $HTTP_SERVER_VARS['PHP_SELF']; ?>" method="post"> <select name="siteselect"> <? $result = mysql_query("SELECT site_name FROM communitypage"); while($row = mysql_fetch_array($result)) { ?> <option value="<? $row['site_name'] ?>"><? echo $row['site_name'] ?></option> <? } ?> </select> <input type="submit" border="0" name="reg" value="GO"> </form> and then I attempt to get the data out as below if(isset($_POST["siteselect"])) { echo "select :".$_POST["siteselect"]; $result = mysql_query("SELECT * FROM communitypage WHERE site_name='richmond court'"); while($row = mysql_fetch_array($result)) { $site_name=$row[0]; // 42 $uname=$row[1]; // $site_details=$row[3]; // $site_notice=$row[4]; // $site_files=$row[5]; // } ?> <br /> <? echo $site_name; ?> <br /> <? echo $site_details; I was trying to make it so the user could just click on the selection but i think it may be easier to use a submit button. Thanks a million in advance:) Michael Quote Link to comment https://forums.phpfreaks.com/topic/54193-solved-php-drop-list-value/ Share on other sites More sharing options...
trq Posted June 4, 2007 Share Posted June 4, 2007 You never actually use the posted variable within your query. eg; $result = mysql_query("SELECT * FROM communitypage WHERE site_name='{$_POST['siteselect']}'"); Quote Link to comment https://forums.phpfreaks.com/topic/54193-solved-php-drop-list-value/#findComment-267939 Share on other sites More sharing options...
monty187 Posted June 4, 2007 Author Share Posted June 4, 2007 You never actually use the posted variable within your query. eg; $result = mysql_query("SELECT * FROM communitypage WHERE site_name='{$_POST['siteselect']}'"); appologies, I forgot to say I am just trying to print out the value on the previous line to that, cheers for looking:) Quote Link to comment https://forums.phpfreaks.com/topic/54193-solved-php-drop-list-value/#findComment-267942 Share on other sites More sharing options...
Wildbug Posted June 5, 2007 Share Posted June 5, 2007 Did you print_r($_POST)? Check the HTML source on the form to make sure the action is pointing to the correct script. You're not echoing the value in the value="" section. Easier to read: <select name="siteselect"> <?php $result = mysql_query("SELECT site_name FROM communitypage"); while($row = mysql_fetch_array($result)) echo "<option value=\"$row[site_name]\">$row[site_name]</option>"; ?> </select> Quote Link to comment https://forums.phpfreaks.com/topic/54193-solved-php-drop-list-value/#findComment-267946 Share on other sites More sharing options...
monty187 Posted June 5, 2007 Author Share Posted June 5, 2007 Did you print_r($_POST)? Check the HTML source on the form to make sure the action is pointing to the correct script. You're not echoing the value in the value="" section. Easier to read: <select name="siteselect"> <?php $result = mysql_query("SELECT site_name FROM communitypage"); while($row = mysql_fetch_array($result)) echo "<option value=\"$row[site_name]\">$row[site_name]</option>"; ?> </select> Cool, I have never used that print_r before but this is what it displays Array ( [siteselect] => [reg] => GO ) How would I echo the value? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/54193-solved-php-drop-list-value/#findComment-267955 Share on other sites More sharing options...
redarrow Posted June 5, 2007 Share Posted June 5, 2007 example. <select name="siteselect"> <?php $row[]="i"; $row[]="love"; $row[]="php"; foreach($row as $r){ echo "<option value='$r'>$r</option>"; } ?> </select> edited <select name="siteselect"> <?php $result = mysql_query("SELECT site_name FROM communitypage"); while($row = mysql_fetch_array($result)) echo "<option value='$row[site_name]'>$row[site_name]</option>"; ?> </select> Quote Link to comment https://forums.phpfreaks.com/topic/54193-solved-php-drop-list-value/#findComment-267969 Share on other sites More sharing options...
dbillings Posted June 5, 2007 Share Posted June 5, 2007 The loop is left open, maybe that is the problem ??? In order to simply click the drop down box you will need to use AJAX else you will need a submit button. <select name="siteselect"> <?php $result = mysql_query("SELECT site_name FROM communitypage"); while($row = mysql_fetch_array($result)){ echo "<option value=\"$row[site_name]\">$row[site_name]</option>"; } ?> </select> Quote Link to comment https://forums.phpfreaks.com/topic/54193-solved-php-drop-list-value/#findComment-267971 Share on other sites More sharing options...
monty187 Posted June 5, 2007 Author Share Posted June 5, 2007 The loop is left open, maybe that is the problem ??? In order to simply click the drop down box you will need to use AJAX else you will need a submit button. <select name="siteselect"> <?php $result = mysql_query("SELECT site_name FROM communitypage"); while($row = mysql_fetch_array($result)){ echo "<option value=\"$row[site_name]\">$row[site_name]</option>"; } ?> </select> Thank you so much guys, it is working now, cheers for all the advice. monty Quote Link to comment https://forums.phpfreaks.com/topic/54193-solved-php-drop-list-value/#findComment-267975 Share on other sites More sharing options...
Wildbug Posted June 5, 2007 Share Posted June 5, 2007 The loop is left open, maybe that is the problem ??? The loop wasn't left open; you can use while/for/foreach/if, etc as a single line if there's only one line. I.e., while ($row = mysql_fetch_row($result)) echo "$row[0]\n"; Quote Link to comment https://forums.phpfreaks.com/topic/54193-solved-php-drop-list-value/#findComment-268387 Share on other sites More sharing options...
dbillings Posted June 6, 2007 Share Posted June 6, 2007 But the loop wasn't on one line. Quote Link to comment https://forums.phpfreaks.com/topic/54193-solved-php-drop-list-value/#findComment-269580 Share on other sites More sharing options...
Wildbug Posted June 11, 2007 Share Posted June 11, 2007 It's still "one line" despite the whitespace (newline). The line is terminated by a semi-colon, not a newline. I used a carriage return since it was too long to fit on the screen. Legibility, you know! Quote Link to comment https://forums.phpfreaks.com/topic/54193-solved-php-drop-list-value/#findComment-272284 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.