suttercain Posted April 15, 2007 Share Posted April 15, 2007 I have a dropdown menu populated from the MySQL database in a form. When I hit submit I want the results of the selection to be displayed... but I can't get it. FORM CODE <form action="comic_index_view.php" method="post"> <?php $result = mysql_query("SELECT title FROM comics GROUP BY title") or die(mysql_error()); $options=""; echo "<select>"; while($row = mysql_fetch_array($result)){ $title=$row["title"]; echo "<option value'" . $title . "'>".$title.'</option>'; } echo "</select>"; ?> <input type="submit" name="submit"> </form> RESULTS PAGE CODE: <?php require('get_connected.php'); $title = $_POST['title']; $sql = mysql_query("SELECT title, issue_number, cover_date, comic_id FROM comics WHERE title ='" . $title . "'") or die(mysql_error()); if ($result = mysql_query($sql)) { if (mysql_num_rows($result)) { while ($row = mysql_fetch_assoc($result)) { echo $row['title']; } } } echo $row['title'] . "<br>"; ?> I know this is a common thing people do with PHP but I could not find any info on the web. Can any one suggest a possible solution? Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/47072-solved-got-the-dropdown-menu-populated-from-mysql-but-not-the-results-after-submit/ Share on other sites More sharing options...
sasa Posted April 15, 2007 Share Posted April 15, 2007 try <?php require('get_connected.php'); $title = $_POST['title']; $sql = "SELECT title, issue_number, cover_date, comic_id FROM comics WHERE title ='" . $title . "'" $result = mysql_query($sql)) or die(mysql_error()); if (mysql_num_rows($result)) { while ($row = mysql_fetch_assoc($result)) { echo $row['title']; } } echo $row['title'] . "<br>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/47072-solved-got-the-dropdown-menu-populated-from-mysql-but-not-the-results-after-submit/#findComment-229598 Share on other sites More sharing options...
suttercain Posted April 15, 2007 Author Share Posted April 15, 2007 Hi Sasa, thanks for the try. Sadly that did not work either. Does anyone have any other suggestions? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/47072-solved-got-the-dropdown-menu-populated-from-mysql-but-not-the-results-after-submit/#findComment-229782 Share on other sites More sharing options...
ignace Posted April 15, 2007 Share Posted April 15, 2007 if ($result = [b]mysql_query[/b]($sql)) // You can not execute a query result as a query try this edited version of sasa's code: <?php <?php require('get_connected.php'); $title = $_POST['comics']; $sql = sprintf("SELECT title, issue_number, cover_date, comic_id FROM comics WHERE title ='%s'", $title); $result = mysql_query($sql) or die(mysql_error()); if (mysql_num_rows($result)) { echo "<select name='comics'>"; while ($row = mysql_fetch_assoc($result)) { vprintf("<option value='%s'>%s</option>", array_fill(0, 2, $row['title'])); } echo "</select>"; } echo $title; ?> ?> Quote Link to comment https://forums.phpfreaks.com/topic/47072-solved-got-the-dropdown-menu-populated-from-mysql-but-not-the-results-after-submit/#findComment-229795 Share on other sites More sharing options...
suttercain Posted April 15, 2007 Author Share Posted April 15, 2007 Hi Ignace, I tried the above code, but that also didn't work. I am wondering why you changed $title = $_POST['titles']; to $title = $_POST['comics']; ? I tried running it with both, still nada. Anyone? Quote Link to comment https://forums.phpfreaks.com/topic/47072-solved-got-the-dropdown-menu-populated-from-mysql-but-not-the-results-after-submit/#findComment-229799 Share on other sites More sharing options...
wafflestomper Posted April 15, 2007 Share Posted April 15, 2007 I believe your <select> needs a name. like <select name="title"> then your variable on your next page would be: $title = $_POST['title']; I think.... (I am new to this, so I could be way off...) Quote Link to comment https://forums.phpfreaks.com/topic/47072-solved-got-the-dropdown-menu-populated-from-mysql-but-not-the-results-after-submit/#findComment-229804 Share on other sites More sharing options...
ignace Posted April 15, 2007 Share Posted April 15, 2007 i use <select name='comics'> so when i submit the form my chosen option will be in the $_POST['comics'] atleast when my form looks like <form method="post" .. Quote Link to comment https://forums.phpfreaks.com/topic/47072-solved-got-the-dropdown-menu-populated-from-mysql-but-not-the-results-after-submit/#findComment-229806 Share on other sites More sharing options...
suttercain Posted April 15, 2007 Author Share Posted April 15, 2007 Hi Wafflestomper, I think you're right. I have now added the name to the select option. Here is my form code now: <form action="comic_index_view.php" method="post"> <?php $result = mysql_query("SELECT title FROM comics GROUP BY title") or die(mysql_error()); $options=""; $title=$row["title"]; echo "<select name='title'>"; while($row = mysql_fetch_array($result)){ echo "<option value'" . $title . "'>".$title.'</option>'; } echo "</select>"; ?> <input type="submit" name="submit"> </form> And here is my results page now: <?php require('get_connected.php'); $title = $_POST['title']; $sql = sprintf("SELECT title, issue_number, cover_date, comic_id FROM comics WHERE title ='%s'", $title); $result = mysql_query($sql) or die(mysql_error()); if (mysql_num_rows($result)) { echo "<select name='title'>"; while ($row = mysql_fetch_assoc($result)) { vprintf("<option value='%s'>%s</option>", array_fill(0, 2, $row['title'])); } echo "</select>"; } echo $title; ?> I still can't get it to work though... Quote Link to comment https://forums.phpfreaks.com/topic/47072-solved-got-the-dropdown-menu-populated-from-mysql-but-not-the-results-after-submit/#findComment-229816 Share on other sites More sharing options...
HeyRay2 Posted April 15, 2007 Share Posted April 15, 2007 Change: echo "<option value'" . $title . "'>".$title.'</option>'; ... to this ... echo "<option value='" . $title . "'>".$title."</option>"; You're missing an "=" in that HTML field, and you're using mixed quotes. Quote Link to comment https://forums.phpfreaks.com/topic/47072-solved-got-the-dropdown-menu-populated-from-mysql-but-not-the-results-after-submit/#findComment-229821 Share on other sites More sharing options...
suttercain Posted April 15, 2007 Author Share Posted April 15, 2007 Wow, that worked... but not how I wanted it. So I selected a comic title and hit submit. It took me to the next page and echoed another drop down menu with all the issues in that drop down. I just want it to echo the titles but not in another drop down. I'll mess with the code, but any other suggestions? Thank you for your time. Quote Link to comment https://forums.phpfreaks.com/topic/47072-solved-got-the-dropdown-menu-populated-from-mysql-but-not-the-results-after-submit/#findComment-229884 Share on other sites More sharing options...
suttercain Posted April 15, 2007 Author Share Posted April 15, 2007 Got it: This is the code: <?php require('get_connected.php'); $title = $_POST['title']; $sql = sprintf("SELECT title, issue_number, cover_date, comic_id FROM comics WHERE title ='%s'", $title); $result = mysql_query($sql) or die(mysql_error()); if (mysql_num_rows($result)) { while ($row = mysql_fetch_assoc($result)) { echo $row['title']; } } echo $title; ?> Thanks guys for your help! Quote Link to comment https://forums.phpfreaks.com/topic/47072-solved-got-the-dropdown-menu-populated-from-mysql-but-not-the-results-after-submit/#findComment-229886 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.