CyberShot Posted December 16, 2010 Share Posted December 16, 2010 I have a test database, I have two names in the database which get returned just fine in my html dropdown. I am trying to figure out how to figure out which item in the list was selected so that I can return information from another table based on the selected id. This is what I am trying now but I don't know how to proceed or if it is correct $result = $mysql->query("SELECT * FROM names") or die($mysql->error); ?> <select> <?php if($result){ while($row = $result->fetch_object()){ $id = $row->nameID; $name = $row->firstName . " " . $row->lastName; ?> <option value"<?php $id ?>"><?php echo $name ?></option> <?php }?> </select> <?php } ?> Quote Link to comment https://forums.phpfreaks.com/topic/221915-get-selected-from-html-select-menu/ Share on other sites More sharing options...
Pikachu2000 Posted December 16, 2010 Share Posted December 16, 2010 Your <select> needs a name attribute, then the value will be available in the $_POST array. Quote Link to comment https://forums.phpfreaks.com/topic/221915-get-selected-from-html-select-menu/#findComment-1148354 Share on other sites More sharing options...
CyberShot Posted December 16, 2010 Author Share Posted December 16, 2010 okay, i did that like so $list = $_POST["listNames"]; if($result){ echo "<select name=\"listNames\">"; while($row = $result->fetch_object()){ $id = $row->namesID; $name = $row->firstName . " " . $row->lastName; echo "<option value\"$id\">$name </option>"; } echo "</select>"; echo $list; } but I get an error saying Undefined index: listNames on this line $list = $_POST["listNames"]; Quote Link to comment https://forums.phpfreaks.com/topic/221915-get-selected-from-html-select-menu/#findComment-1148363 Share on other sites More sharing options...
CyberShot Posted December 16, 2010 Author Share Posted December 16, 2010 wait, I may know why Quote Link to comment https://forums.phpfreaks.com/topic/221915-get-selected-from-html-select-menu/#findComment-1148364 Share on other sites More sharing options...
CyberShot Posted December 16, 2010 Author Share Posted December 16, 2010 nope. I thought because I didn't have the select list in a form tag. but it's still not working. <form method="post" name="form2" action=""> <?php if($result){ echo "<select name=\"listNames\">"; while($row = $result->fetch_object()){ $id = $row->namesID; $name = $row->firstName . " " . $row->lastName; echo "<option value\"$id\">$name </option>"; } echo "</select>"; echo $list; } ?> </form> Quote Link to comment https://forums.phpfreaks.com/topic/221915-get-selected-from-html-select-menu/#findComment-1148366 Share on other sites More sharing options...
Pikachu2000 Posted December 16, 2010 Share Posted December 16, 2010 Well, the value won't be available until the form has been submitted, naturally . . . Quote Link to comment https://forums.phpfreaks.com/topic/221915-get-selected-from-html-select-menu/#findComment-1148369 Share on other sites More sharing options...
CyberShot Posted December 16, 2010 Author Share Posted December 16, 2010 I am working on that now. What would you suggest I do? I want this select box to be in a different location on my page. So it is outside the main form which means I made a second form that when you push the submit button goes to my formdata.php page. But I already have a isset function for the first submit button. How would you suggest handling the second submit button on this page? Quote Link to comment https://forums.phpfreaks.com/topic/221915-get-selected-from-html-select-menu/#findComment-1148374 Share on other sites More sharing options...
sstangle73 Posted December 16, 2010 Share Posted December 16, 2010 name the second submit button submit 2 name="submit2" and do if(isset($_POST['submit'])){ code for first form } else if(isset($_POST['submit2'])){ code for 2nd form } Quote Link to comment https://forums.phpfreaks.com/topic/221915-get-selected-from-html-select-menu/#findComment-1148377 Share on other sites More sharing options...
CyberShot Posted December 16, 2010 Author Share Posted December 16, 2010 okay, I did that. But I am coming up empty. I know the button works because I am getting the success echoed my form <form method="post" name="form2" action="formData.php"> <?php if($result){ echo "<select name=\"listNames\">"; while($row = $result->fetch_object()){ $id = $row->namesID; $name = $row->firstName . " " . $row->lastName; echo "<option value\"$id\">$name </option>"; } echo "</select>"; } ?> <input type="submit" name="form2" value="submit"> </form> and my formData.php if(isset($_POST['form2'])){ $list = $_POST["listNames"]; echo $list; echo "Success"; } Quote Link to comment https://forums.phpfreaks.com/topic/221915-get-selected-from-html-select-menu/#findComment-1148382 Share on other sites More sharing options...
Pikachu2000 Posted December 16, 2010 Share Posted December 16, 2010 First you should ask yourself if there's a reason you actually need to have two forms on the same page. Then, if there truly is: I nearly always include a hidden form field to use solely to see if the form has been submitted. You could do the same, giving each of them a different value. <input type="hidden" name="submitted" value="form1">, and another one for form 2. then if( isset($_POST['submitted']) ) { if( $_POST['submitted'] == 'form1' ) { // do the form 1 stuff. } if( $_POST['submitted'] == 'form2' ) { // do the form 2 stuff. } } Quote Link to comment https://forums.phpfreaks.com/topic/221915-get-selected-from-html-select-menu/#findComment-1148384 Share on other sites More sharing options...
sstangle73 Posted December 16, 2010 Share Posted December 16, 2010 echo "<option value\"$id\">$name </option>"; needs an equal sign and the actual varible echo "<option value=\"" . $id. "\">$name </option>"; Quote Link to comment https://forums.phpfreaks.com/topic/221915-get-selected-from-html-select-menu/#findComment-1148387 Share on other sites More sharing options...
CyberShot Posted December 16, 2010 Author Share Posted December 16, 2010 okay, I made all those changes. Good catch. I am still coming up empty. it's not echoing out the list. Which I am doing simply to test that the code is working here is both scripts just in case <form method="post" action="formData.php" id="form1"> <table width="350" border="0"> <tr> <td>First Name:</td> <td><input type="text" name="firstName" id="first" /></td> </tr> <tr> <td>Last Name:</td> <td><input type="text" name="lastName" id="last" /></td> </tr> <tr> <td>Company Name:</td> <td><input type="text" name="companyName" id="company" /></td> </tr> </table> <br /> <br /> <table width="350" border="0"> <tr> <td>Home Phone:</td> <td><input type="text" name="homePhone" id="homePhone" /></td> </tr> <tr> <td>Cell Phone:</td> <td><input type="text" name="cellPhone" id="cellPhone" /></td> </tr> <tr> <td>Company Phone:</td> <td><input type="text" name="companyPhone" id="companyPhone" /></td> </tr> <tr> <td>Amount Owed:</td> <td><input type="text" name="amount" id="amount" /></td> </tr> <tr> <td> </td> <input type="hidden" name="submitted" value="form1"> <td><input type="submit" value="submit" name="submit" id="submit"/></td> </tr> </table> </form> the second form <form method="post" name="form2" action="formData.php"> <?php if($result){ echo "<select name=\"listNames\">"; while($row = $result->fetch_object()){ $id = $row->namesID; $name = $row->firstName . " " . $row->lastName; echo "<option value=\"{$id}\">$name </option>"; } echo "</select>"; } ?> <input type="hidden" name="submitted" value="form2"> <input type="submit" name="form2" value="submit"> </form> and the action page if(isset($_POST['submitted'])){ if($_POST['submitted'] == 'form1') if(isset($_POST['submit'])){ $firstName = $_POST['firstName']; $lastName = $_POST['lastName']; $companyName = $_POST['companyName']; $homePhone = $_POST['homePhone']; $cellPhone = $_POST['cellPhone']; $companyPhone = $_POST['companyPhone']; //checking the values are filled //echo $firstName. " " . $lastName . " " . $companyName . " " . $homePhone . " " . $cellPhone . " " . $companyPhone; $mysql->query("INSERT INTO names(firstName,lastName,companyName)values('$firstName','$lastName','$companyName')"); } if( $_POST['submitted'] == 'form2' ){ $list = $_POST["listNames"]; echo $list; echo "Success"; } } Thanks for the help Quote Link to comment https://forums.phpfreaks.com/topic/221915-get-selected-from-html-select-menu/#findComment-1148398 Share on other sites More sharing options...
sstangle73 Posted December 16, 2010 Share Posted December 16, 2010 im not the best at php but shouldnt $list = $_POST["listNames"]; use ' instead of " $list = $_POST['ListNames']; Quote Link to comment https://forums.phpfreaks.com/topic/221915-get-selected-from-html-select-menu/#findComment-1148400 Share on other sites More sharing options...
CyberShot Posted December 16, 2010 Author Share Posted December 16, 2010 yes, that did the trick. I had that once but changed it while I was looking for the problem. Good catch Quote Link to comment https://forums.phpfreaks.com/topic/221915-get-selected-from-html-select-menu/#findComment-1148404 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.