harlequeen Posted February 10, 2007 Share Posted February 10, 2007 Hi I am trying to teach myself php but am getting bogged down. I have some code which populates a dropdown box and I want to then pass the selected item so that the database record is recalled to the page. Ultimately, I would like this to appear on the same page, underneath the dropdown box. I can't seem to get it to work though. I'm sure its a simple thing, but I can't do it. Here's my code from the sending page.. $query="SELECT title FROM recipes"; $result = mysql_query ($query); echo "<form action=dropdrip3.php method=get> <select title=Title value=''>Title</option>"; // printing the list box select command while($dropdown=mysql_fetch_array($result)){//Array or records stored in $dropdown echo "<option value=$dropdown>$dropdown[title]</option>"; /* Option values are added by looping through the array */ } echo "</select>";// Closing of list box ?> <input type="submit" name="Submit" value="Submit" /> and here's my code from the receiving page. $dropdown=$_GET['title']; $sql=mysql_query("SELECT * FROM recipes WHERE title='$title'"); while(@$row=mysql_fetch_array($sql)){ //Build formatted result here echo("<h4>"); echo $row['title']."<br/>"; echo $row['ingredients']."<br/>"; echo $row['method']."<br/></h4>"; } My grasp of php is very tenuous, so if someone could help I would be grateful Thanks Harlequeen Quote Link to comment Share on other sites More sharing options...
alpine Posted February 10, 2007 Share Posted February 10, 2007 Name the select <select name="title"> And add the value echo "<option value=\"".$dropdown['title']."\">".$dropdown['title']."</option>"; Quote Link to comment Share on other sites More sharing options...
harlequeen Posted February 10, 2007 Author Share Posted February 10, 2007 Thanks, brilliant, that works great. Is it a simple matter to get it to print on the same page as the dropdown box is? Is it a matter of putting the code together on the same page, or is it not as simple as that? Cheers harlequeen Quote Link to comment Share on other sites More sharing options...
alpine Posted February 10, 2007 Share Posted February 10, 2007 No, that's fairly easy - detect based on if the title is present or not in the adress bar: <?php if(!empty($_GET['title'])){ // title is present - do search $dropdown = htmlspecialchars($_GET['title'],ENT_QUOTES); $sql = mysql_query("SELECT * FROM recipes WHERE title='$title'"); while($row = mysql_fetch_array($sql)){ echo <<<_HTML <h4> {$row['title']}<br /> {$row['ingredients']}<br /> {$row['method']}<br /> </h4> _HTML } } else // title is absent or empty - show dropdown { echo <<<_HTML <form action="this.php" method="get"> <select name="title"> _HTML; $query = mysql_query("SELECT title FROM recipes"); while($dropdown = mysql_fetch_array($query)){ echo "<option value=\"".$dropdown['title']."\">".$dropdown['title']."</option>"; } echo <<<_HTML </select> <input type="submit" name="Submit" value="Submit" /> _HTML; } ?> Should do it... Quote Link to comment Share on other sites More sharing options...
harlequeen Posted February 15, 2007 Author Share Posted February 15, 2007 I don't think I've got the hang of this.... I tried combining the code above with my code, but I seem to have got it all wrong. Here is my code, can someone tell me what I need to correct. Apologies if its bad coding but I'm trying to learn. All I get is an empty dropdown box and the submit button. I'm confused. Thanks <link rel="stylesheet" type="text/css" href="apple.css"> <?php //database connection include'myconnect.php'; if(!empty($_GET['title'])) //if title is present do search { $dropdown=htmlspecialchars($_GET['title'], ENT_QUOTES); $sql=mysql_query("SELECT * FROM recipes WHERE title='$title'"); while(@$row=mysql_fetch_array($sql)) //Build formatted result here echo("<h4>"); echo $row['title']."<br/>"; echo $row['ingredients']."<br/>"; echo $row['method']."<br/></h4>"; // This is here to remind me to add a link to the title for the recipe. //echo "<a href=$url>$article_image</a>"; } else // title is absent or empty - show dropdown { echo "<form action=dropdrip3.php method=get> <select name='title'>Title</option>"; // printing the list box select command while($dropdown=mysql_fetch_array($result)) { //Array or records stored in $dropdown //echo "<option value=$dropdown>$dropdown[title]</option>"; echo "<option value=\"".$dropdown['title']."\">".$dropdown['title']."</option>"; /* Option values are added by looping through the array */ } echo "</select>";// Closing of list box } ?> <input type="submit" name="Submit" value="Submit" /> </body> </html> Quote Link to comment Share on other sites More sharing options...
JJohnsenDK Posted February 15, 2007 Share Posted February 15, 2007 You need to change this: echo "<option value=\"".$dropdown['title']."\">".$dropdown['title']."</option>"; To this: echo "<option value=\"".$row['title']."\">".$row['title']."</option>"; Quote Link to comment Share on other sites More sharing options...
harlequeen Posted February 15, 2007 Author Share Posted February 15, 2007 HI Did what you suggested but if I get to the page from the initial selection page I only get a submit button and if I go straight to the page so that there has been no selection I get an empty dropdown box and a submit button. Any further ideas? Thanks Quote Link to comment Share on other sites More sharing options...
JJohnsenDK Posted February 15, 2007 Share Posted February 15, 2007 I cant see what you are using the $_GET variable for?? I would do this: <link rel="stylesheet" type="text/css" href="apple.css"> <?php //database connection include'myconnect.php'; if(!empty($_GET['title'])) //if title is present do search { $dropdown=htmlspecialchars($_GET['title'], ENT_QUOTES); $sql=mysql_query("SELECT * FROM recipes WHERE title='$title'"); while(@$row=mysql_fetch_array($sql)) //Build formatted result here echo("<h4>"); echo $row['title']."<br/>"; echo $row['ingredients']."<br/>"; echo $row['method']."<br/></h4>"; // This is here to remind me to add a link to the title for the recipe. //echo "<a href=$url>$article_image</a>"; } else // title is absent or empty - show dropdown { echo "<form action=dropdrip3.php method=get> <select name='title'>Title</option>"; // printing the list box select command while($row2=mysql_fetch_array($result)) { //Array or records stored in $dropdown //echo "<option value=$row2>$row2</option>"; echo "<option value=\"".row2."\">".$row2."</option>"; /* Option values are added by looping through the array */ } echo "</select>";// Closing of list box } ?> <input type="submit" name="Submit" value="Submit" /> </body> </html> I made chances in your last while loop... Quote Link to comment Share on other sites More sharing options...
Archadian Posted February 16, 2007 Share Posted February 16, 2007 Well your first mistake is this: include'myconnect.php'; It needs to be: include('myconnect.php'); That could be why its not showing anything...you aren't connecting to your database to start with. The empty dropdown box says you aren't connecting to your database. The smallest thing could mess up the whole script and cause it not to load at all. My pages haven't even loaded because of this: echo "<form action=dropdrip3.php method=get> It works when i change it to this: echo "<form action=\"dropdrip3.php\" method=\"get\">"; You are missing "echo", ";", etc. If you write HTML with PHP you have to use echo " "; Quote Link to comment Share on other sites More sharing options...
Archadian Posted February 16, 2007 Share Posted February 16, 2007 Try this: <link rel="stylesheet" type="text/css" href="apple.css"> <?php //database connection include('myconnect.php'); if(isset($_GET['title'])) //if title is present do search { $dropdown=htmlspecialchars($_GET['title'], ENT_QUOTES); $sql=mysql_query("SELECT * FROM recipes WHERE title='$title'"); while(@$row=mysql_fetch_array($sql)) //Build formatted result here echo("<h4>"); echo $row['title']."<br/>"; echo $row['ingredients']."<br/>"; echo $row['method']."<br/></h4>"; // This is here to remind me to add a link to the title for the recipe. //echo "<a href=\"$url/$article_image\"></a>"; } else // title is absent or empty - show dropdown { echo "<form action=\"dropdrip3.php\" method=\"get\">"; echo "<select name=\"title\">Title</option>"; // ( or if you meant $title then echo "<select name=\"" .$title . "\">Title</option>"; ) // printing the list box select command while($dropdown=mysql_fetch_array($result)) { //Array or records stored in $dropdown //echo "<option value=$dropdown>$dropdown[title]</option>"; echo "<option value=\"".$dropdown['title']."\">".$dropdown['title']."</option>"; /* Option values are added by looping through the array */ } echo "</select>";// Closing of list box } ?> <input type="submit" name="Submit" value="Submit" /> </body> </html> Quote Link to comment Share on other sites More sharing options...
The_Assistant Posted February 16, 2007 Share Posted February 16, 2007 Well your first mistake is this: include'myconnect.php'; It needs to be: include('myconnect.php'); it doesn't have to be written like that at all. it just needs to have a space between include and the filename Quote Link to comment Share on other sites More sharing options...
harlequeen Posted February 16, 2007 Author Share Posted February 16, 2007 Hi everyone and thanks for the help. My include statement works just as it was, as I am using it in other pages and can connect correctly and get output. Also, my connect file includes the usual failure statements which leads me to believe that I am connecting OK. JJohnsenDK, I tried your code and it didn't work, neither did the code from Archadian. I have a page (http://www.slim-with-me.co.uk/dropdrip.php) which gets the info from the database, populates the dropdown box and allows me to select an item and then passes the item to the receiving page for output of the record from the database. That works ok. But I also want to be able to have the box appear on the receiving page so that if someone wants another selection they don't have to go back to the originating page. As I said before, I seem to get the box, but its empty, or I don't get anything at all. I am not proficient at php and can't seem to get what I want. Any further help would be appreciated. Harlequeen Quote Link to comment Share on other sites More sharing options...
Archadian Posted February 16, 2007 Share Posted February 16, 2007 You can either copy the code onto the recieving page that you want or use a variable or function for the dropdown box itself. Don't feel bad harlequeen, i still have alot to learn myself. I use the Php Bible 2nd Edition you can get from any book store. Anyways, i believe the function would look like this: <?php function dropdown() { echo "<form action=\"dropdrip3.php\" method=\"get\">"; echo "<select name=\"title\">Title</option>"; while($dropdown=mysql_fetch_array($result)) { //echo "<option value=$dropdown>$dropdown[title]</option>"; echo "<option value=\"".$dropdown['title']."\">".$dropdown['title']."</option>"; /* Option values are added by looping through the array */ } echo "</select>";// Closing of list box } ?> <input type="submit" name="Submit" value="Submit" /> Then you just call the function "dropdown()" or whatever you name it whenever you want it to show up on one of your pages. Please anyone correct me if im wrong about this code but from what i read this is how you do it. Hopefully this helps and works. $result = mysql_query("SELECT id, sess, admsess, levelad FROM users WHERE sess = '$cookie'", $db) or die(mysql_error()); $row = mysql_fetch_array($result); For some reason i have to add the $db at the end of the () in the mysql_query or it doesnt work for me...$db being the DB i use for the info for my website. Quote Link to comment Share on other sites More sharing options...
harlequeen Posted February 17, 2007 Author Share Posted February 17, 2007 Sorry, Archadian. Thats just confused me even more. I can't get it to work even though it seems that it should be a simple matter. I thought it would be easier to add to code that I know is working, check for the variable being present then if it is do the working code, if its not, do something else that I know was working, but that didn't do it. Cheers Harlequeen Quote Link to comment Share on other sites More sharing options...
Archadian Posted February 17, 2007 Share Posted February 17, 2007 The best way i know to see what is working and being passed through is to echo every variable. Maybe you will find your problem that way and be able to troubleshoot a little better. . Quote Link to comment 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.