PHP - Echoing The Selected Option From A Populated List
I have a form that contains 3 text fields and 7 populated lists (that are dependent on the choice of the previous lists). When I post the information to MySQL and to my processing page, it posts the table "ids" instead of the selected option.
Here's the php code for the form: <form action="gccsuccess_form.php" method="post" enctype="multipart/form-data" name="gcc" id="gcc"> <table width="700" border="0" align="center" cellpadding="3" cellspacing="3"> <tr> <td><label for="date">Today's Date:</label> <input type="text" name="date" id="date"></td> <td> </td> </tr> <tr> <td>Week Number: <select name="week" size="1" id="week"> <option value="0" selected>Week 0 - 3/26 - 4/1</option> <option value="1">Week 1 - 4/2 - 4/8</option> <option value="2">Week 2 - 4/9 - 4/15</option> <option value="3">Week 3 - 4/16 - 4/22</option> <option value="4">Week 4 - 4/23 - 4/29</option> <option value="5">Week 5 - 4/30 - 5/6</option> <option value="6">Week 6 - 5/7 - 5/13</option> </select></td> <td><label for="location"></label> </td> </tr> <tr> <td>Location: <select id='locations' name='location'> </select></td> <td> </td> </tr> <tr> <td><label for="label8">Team Name</label> <select id='team_names' name='team_name'> </select></td> <td> </td> </tr> <tr> <td><em>*Please enter each team member's first and last name:</em></td> <td> </td> </tr> <tr> <td><label for="mem_1"> Team <strong>Captain:</strong></label> <select id='team_members 1' name='capt'> </select></td> <td><label for="label4"><strong>Steps:</strong></label> <input type="text" name="steps_1" id="steps_1"></td> </tr> <tr> <td><label for="mem_2">Team Member #2 <strong>Name: <select id='team_members 2' name='mem_2'> </select> </strong></label></td> <td><label for="label5"><strong>Steps:</strong></label> <input type="text" name="steps_2" id="steps_2"></td> </tr> <tr> <td><label for="mem_3">Team Member #3 <strong>Name: <select id='team_members 3' name='mem_3'> </select> </strong></label></td> <td><label for="label6"><strong>Steps:</strong></label> <input type="text" name="steps_3" id="steps_3"></td> </tr> <tr> <td><label for="mem_4">Team Member #4<strong> Name</strong>: <select id='team_members 4' name='mem_4'> </select> </label></td> <td><label for="label7"><strong>Steps:</strong></label> <input type="text" name="steps_4" id="steps_4"></td> </tr> <tr> <td><label for="label">Team Member #5<strong> Name</strong>: <select id='team_members 5' name='mem_5'> </select> </label></td> <td><label for="label7"><strong>Steps:</strong></label> <input type="text" name="steps_5" id="steps_5"></td> </tr> <tr> <td> </td> <td> </td> </tr> <tr> <td> </td> <td><em>* Maximum steps for each team member per week is <strong>105,000. </strong></em></td> </tr> <tr> <td> </td> <td><input type="submit" name="Submit Form" id="Submit Form" value="Submit"></td> </tr> </table> <label></label> <div align="center"><br> If you have questions about the competition or reporting please contact KC WELLNESS, INC toll free at <SPAN id="lw_1237248942_0">877-634-1412.</SPAN><br> <br> </div> </form> And here's the code for the page it is posted in: <?php $date = $_POST['date']; $week = $_POST['week']; $location = $_POST['location']; $team_name = $_POST['team_name']; $capt = $_POST['capt']; $mem_2 = $_POST['mem_2']; $mem_3 = $_POST['mem_3']; $mem_4 = $_POST['mem_4']; $mem_5 = $_POST['mem_5']; $steps_1= $_POST['steps_1']; $steps_2= $_POST['steps_2']; $steps_3= $_POST['steps_3']; $steps_4 = $_POST['steps_4']; $steps_5 = $_POST['steps_5']; mysql_connect("mysql1.myregisteredsite.com", "49200_kandace", "Leonardo56") or die(mysql_error()); mysql_select_db("49200_GCC") or die(mysql_error()); mysql_query("INSERT INTO `walkathon` VALUES ('$date', '$week', '$location', '$team_name', '$capt','$mem_2','$mem_3', '$mem_4', '$mem_5', '$steps_1','$steps_2', '$steps_3','$steps_4', '$steps_5' )"); Print "Thank you for your entry. <br> Report summary: <br/><br/>"; echo "<strong> Date: </strong> {$date} <br/> <br/><strong> Week: </strong> {$week} <br/> <br/><strong> Location: </strong> {$location} <br/> <br/><strong> Team Name: </strong> {$team_name} <br/> <br/><strong> Team Captain: </strong> {$capt} / <strong> Steps: </strong> {$steps_1} <br /> <br/><strong> Team Member 2 Name: </strong> {$mem_2} / <strong> Steps: </strong> {$steps_2} <br /> <br/><strong> Team Member 3 Name: </strong> {$mem_3} / <strong> Steps: </strong> {$steps_3} <br /> <br/><strong> Team Member 4 Name: </strong> {$mem_4} / <strong> Steps: </strong> {$steps_4} <br /> <br/><strong> Team Member 5 Name: </strong> {$mem_5} / <strong> Steps: </strong> {$steps_5}" ?> How do I get the processing page to echo the selected option instead of the id? Similar TutorialsHey--I'm new to the forum, to php, and to web development in general. I am using php to populate a drop-down list from a MySQL database, and then echo the text in a field associated with the selected value below. I'm having no trouble creating the drop-down list, but I can't figure out how to echo the associated field below, or the ?Abbreviation=value that I expected in the url. Here is the php file: Code: [Select] <!DOCTYPE HTML> <html> <head> </head> <body> <form name="form" method="GET" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <select> <?php $con = mysql_connect("localhost","root","root"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("calculators", $con); $result = mysql_query("SELECT * FROM calculator"); while($row = mysql_fetch_array($result)) { echo "<option type='text' name='Title' " . "value='" . $row['Title'] . "'>" . $row['Abbreviation'] . "</option>"; } mysql_close($con); ?> </select> <br> <input type="submit" name="submit"> </form> <?php echo $_GET['Title']; ?> </body> </html> This is what the source code looks like in the browser: Code: [Select] <!DOCTYPE HTML> <html> <head> </head> <body> <form name="form" method="GET" action="/calculators/index.php"> <select> <option type='text' name='Title' value='Mean Arterial Pressure'>MAP</option><option type='text' name='Title' value='Body Mass Index'>BMI</option></select> <br> <input type="submit" name="submit"> </form> </body> </html> And this is appearing in the url after I press submit: Code: [Select] ?submit=Submit I'm sorry if this is a silly question, or if I'm being long-winded in asking it. I'm very new to web development, and I'm not sure exactly what information is necessary to solve the problem. Also, though changes to the code are helpful, I may need an explanation to understand them... Thanks, Davis Hi I have a list of states using the array method in a form. The drop down menu works fine. I want to save the user choice,if the form is re-displayed due to a blank field or pattern mismatch. I know I can use the selected=selected, but don't know wher to put the statement: My array is: state_province = array ("list of states", "provinces") Var in my labels array is "state"=>"state" Here is my code for the select/option statement: { if($field == "state") { echo "<div class='province_state'><label for='state' size='10'>* Province/State</label><select>"; foreach($state_province as $state) { echo "\n<option value='$state_province' /> "; echo $state ; echo "</option>"; } echo "</select></div>\n"; } ?Is this the correct code to add and where would I add it? if(@$_POST['state'] == $value) { echo "selected='selected' "; } Hi, basically, here's the deal: I have a lit of checkboxes that are added by the admin (there's an unlimited amount, just depends on how many are added). Then, those are put in a form, in which the user picks whichever ones need to be chosen and those values get sent to a MySQL table. Here's the code that displays the checkboxes Code: [Select] <?php $sql="SELECT * FROM category ORDER BY categoryID ASC"; $result=mysql_query($sql); while($row=mysql_fetch_array($result)){ echo "<input type='checkbox' id='". $row['categoryName'] ."' name='licensed[]' value='" . $row['categoryID'] ."' /><label for='". $row['categoryName'] ."'><br>" . $row['categoryName'] ."</label><br>"; } ?> What I'm making now, is an edit form where whichever checkboxes were checked, will show up checked in the edit form. But I'm not really sure how to go about this, since there is only one actual input tag in the code, I can't select the different ones. I also have this SQL query which selects whichever boxes were inputted into the DB Code: [Select] $sql2="SELECT * FROM category, categoryInfo WHERE category.categoryID = categoryInfo.categoryID AND categoryInfo.parentID = $parentID"; $result2=mysql_query($sql2); Where $parentID is the ID of whichever form you're editing. But yes, I'm basically not really sure how to go about this and would like some help figuring this out Thanks for your time I am simply trying to keep a check box checked once selected. What I have works, but not correctly. Because I am calling each array element in my checkbox like name="checkbox[]" And I am using this as the input return the check: <input type="checkbox" name="checkbox[]" value="checkOneValue" <?php if(isset($_POST['checkbox'])) echo "checked"; ?> /> I have 5 checkboxes that use the same code! And as I said it works, but returns ALL checkboxes as selected when submitted. I only want the actual selected boxes to remained checked. Any insight on a work around? The problem is I cannot solve how to identify unique array values as they are all named the same... Maybe I am not thinking right on all of this. Here is my full source code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Checkbox Echoing Test</title> </head> <body> <form name="" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> One <input type="checkbox" name="checkbox[]" value="checkOneValue" <?php if(isset($_POST['checkbox'])) echo "checked"; ?> /> <br /> Two <input type="checkbox" name="checkbox[]" value="checkTwoValue" <?php if(isset($_POST['checkbox'])) echo "checked"; ?> /> <br /> Three <input type="checkbox" name="checkbox[]" value="checkThreeValue" <?php if(isset($_POST['checkbox'])) echo "checked"; ?> /> <br /> Four <input type="checkbox" name="checkbox[]" value="checkFourValue" <?php if(isset($_POST['checkbox'])) echo "checked"; ?> /> <br /> Five <input type="checkbox" name="checkbox[]" value="checkFiveValue" <?php if(isset($_POST['checkbox'])) echo "checked"; ?> /> <br /> <input type="submit" name="submit" value="Echo Checkbox Data!" /> </form> <?php if (isset($_POST['checkbox']) && (isset($_POST['submit']))) { $checkbox = $_POST['checkbox']; foreach ($checkbox as $checkboxValue) { var_dump($checkboxValue); if (!empty($checkboxValue)) { echo "<br />$checkboxValue is checked"; } } }; // End of foreach loop if (!isset($_POST['checkbox']) && (isset($_POST['submit']))) { echo "<br />No checkboxes were selected!"; }; ?> </body> </html> I have the following which fils a combo box with years from 1950 to 2100. This is the year box for the date of birth. Year <select name="year_select"> <?php for( $i=1950; $i<=2100; $i++ ) { echo "<option value=\"{$i}\" class=\"year\">{$i}</option>"; } ?> </select> I want it to automatically display the year that is in the database so instead of automatically displaying 1950 i want their birth year displayed which is $tab_info->user_dob_year. How would i go about this? i dont know where to start since im using a for loop to populate it. I'm just attempting to learn how PHP handles things and I can't quite wrap my head around how to apply Selected to the final Option and show the Traits for the Character based on the Selected Option. I understand this might need POST, if it does, I would appreciate a bit of help on how I would set this up as POST since I didn't think a drop down required a submit button. Code: [Select] $character= array (array(Name=>"Barry","Class"=>"Fighter",Level=>1,Str=>10,Dex=>10,"Int"=>10),array(Name=>"Lindehar","Class"=>"Ranger",Level=>1,Str=>10,Dex=>10,"Int"=>10),array(Name=>"Verelden","Class"=>"Mage",Level=>1,Str=>10,Dex=>10,"Int"=>10)); print "Select a Character:<br /><select>"; foreach($character as $array_num) { foreach($array_num as $char_trait=>$trait_value) { if($char_trait==Name) { $selected_value=""; $generated_chars="<option selected=".$selected_value." value='".$char_trait."'>".$char_trait.": ".$trait_value."</option>"; print $generated_chars; if($selected_value=/".$char_trait.": Barry"/") { foreach($char_trait=="Barry") { print "<h4>".$char_trait.": ".$trait_value."</h4>"; } } } } } print "</select><p />"; mysql.php <?php $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("ckeditor",$con); ?> --------------------------------------------------------- add.php <?php include("mysql.php"); if(isset($_POST["button2"])) { $sql="INSERT INTO cktext (section) VALUES ('$_POST[select2]')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } ?> ----------------------------------------------------------------- home.php <form id="form1" name="form1" method="post" action="add.php"> <tr> <td>Section:</td> <td><select name="select2" id="select2"> <option selected="selected" value="MALE">Male</option> <option selected="" value="FEMAIL">FEMAIL</option> </select></td> </tr> <input type="submit" name="button2" id="button2" value="Upload" /> </form> In DATABASE :- cktext table attribute "section" is varchar type. BUT IT RETURN ME BLANK OUTPUT. If I have a variable and a hard coded select dropdown, what is the easiest way to compare the variable with the value and set that particular option's selected attributed to selected? Hi, I wanted to make a dynamic php page. I want the user to choose an option you could select in an option box (html) and after that I wish that down the page some text would change. If I take option 1 -> text 1, option 2 -> text 2, ... But this doesn't work, do I need a button or something to made my code running, and how do I have to use it on the same page? Or is it possible to run it automaticly after a new choice? Thanx, Kevin My code: <?php /* Template Name: template voor Ieder1Trainer # */ get_header(); $Tacktiek = ""; $Tacktiek1 = "3-5-2"; $Tacktiek2 = "3-4-3"; $Tacktiek3 = "4-5-1"; $Tacktiek4 = "4-4-2"; $Tacktiek5 = "4-3-3"; $Tacktiek6 = "5-4-1"; $Tacktiek7 = "5-3-2"; $aantalDef; $aantalMid; $aantalAtt; ?> <div id="newsfeed"> <div class="banderol"> <div class="banderol_left"></div> <h1>Dynamic page</h1> </div> <?php if(is_user_logged_in()) { get_currentuserinfo(); //echo 'Username: ' . $current_user->user_login . "\n"; //echo 'User email: ' . $current_user->user_email . "\n"; ?> <!-- Here you choose a tactiek; 1,2,3, .. after that has been choosen I whise the next php-code would be run (the if-then-elseif-...) --> <tr> <td><p class="formulier">Kies je Tacktiek:</p></td> <td> <select size="1" name="$Tacktiek"> <option selected><?php echo $Tacktiek1;?></option> <option><?php echo $Tacktiek2;?></option> <option><?php echo $Tacktiek3;?></option> <option><?php echo $Tacktiek4;?></option> <option><?php echo $Tacktiek5;?></option> <option><?php echo $Tacktiek6;?></option> <option><?php echo $Tacktiek7;?></option> </select> </td> </tr> <!-- This must be run after choosen a tactiek. --> <?php if($Tacktiek == '3-5-2') { $aantalDef = 3; $aantalMid = 5; $aantalAtt = 2; } elseif($Tacktiek == '3-4-3') { $aantalDef = 3; $aantalMid = 4; $aantalAtt = 3; } elseif($Tacktiek == '4-5-1') { $aantalDef = 4; $aantalMid = 5; $aantalAtt = 1; } elseif($Tacktiek == '4-4-2') { $aantalDef = 4; $aantalMid = 4; $aantalAtt = 2; } elseif($Tacktiek == '4-3-3') { $aantalDef = 4; $aantalMid = 3; $aantalAtt = 3; } elseif($Tacktiek == '5-4-1') { $aantalDef = 5; $aantalMid = 4; $aantalAtt = 1; } elseif($Tacktiek == '5-3-2') { $aantalDef = 5; $aantalMid = 3; $aantalAtt = 2; } ?> <!-- After this run, the next code must be run and made a choose of text --> <tr> <td><p class="formulier"> <?php if($aantalDef == '3') { echo "3 defs"; //Choosen text if aantalDef == 3 } elseif($aantalDef == '4') { echo "4 defs"; //Choosen text if aantalDef == 4 } elseif($aantalDef == '5') { echo "5 defs"; //Choosen text if aantalDef == 5 } ?> </p></td> <!-- Other choose of options, but always the same options. --> <td> <select size="1" name="$Speler4"> <option selected><?php echo $NaamDef1;?></option> <option><?php echo $NaamDef2;?></option> <option><?php echo $NaamDefMid1;?></option> <option><?php echo $NaamDefMid2;?></option> <option><?php echo $NaamDefMid3;?></option> <option><?php echo $NaamDefMid4;?></option> <option><?php echo $NaamDefMid5;?></option> <option><?php echo $NaamDefMid6;?></option> <option><?php echo $NaamDefMid7;?></option> <option><?php echo $NaamDefMid8;?></option> <option><?php echo $NaamDefMidAtt1;?></option> <option><?php echo $NaamDefMidAtt2;?></option> <option><?php echo $NaamDefMidAtt3;?></option> </select> </td> </tr> <?php } else{ echo 'U dient in te loggen.'; } ?> </div> <?php get_footer(); ?> Not sure exactly where this problem is coming from, but php seems the most likely candidate to fix it. My php loads, from a MySQL database, a list of names (students in a class) and a drop-down for each student with attendance options. Based on the information in the database, the appropriate attendance option is selected (<option selected></option>). Everything works fine on the initial page load. Next, I want to update the status, so I select a new option from the drop-down list and click submit. The php code updates the database correctly, but the selected option (in HTML) is still the original one. If I change the option to something different and submit again, I get the previously selected option (that I selected the first time) - in essence, the HTML is always one step behind the database. I've attached the snippet of relevant code - again, I know that the code works when the page loads initially and that the database is getting updated correctly. Any help or comments are appreciated. I'm trying to create a dynamic option menu with one alert selected based on the first query to the db. Any help would be greatly appreciated. Code: [Select] //function to get alerts and create select menu with current alerts pre-selected function getALERTS1($id){ require('db.php'); $alert = mysqli_query($conn, "SELECT alert1 FROM visit_data WEHERE patientid = $id AND discharged IS NULL"); $row = mysqli_fetch_array($alert); $selects=null; $query = mysqli_query($conn, "SELECT alertid, name FROM alerts"); while($row1 = mysqli_fetch_array($query)) { $selects .= "<option value=\"" . $row1['alertid'] . "\"> if($row1['alertid']==$row['alert1']) { echo ' selected'; } ".$row1['name']."</option>"; } return $selects; }
<?php I have a drop down list that contains expiration dates in the format 01/12 and if the user selected a certain drop down list I would like that same date selected. Does anyone know the code to do this? Thanks. Hi, I have an html form in "send.htm" that sends data to "retrieve.php" with the following structu send.htm: Code: [Select] <html> <body> <form method="post" action="retrieve.php" <select id='select' name='select' > <option>option1</option> <option>option2</option> <option>option3</option> <option>etc...</option> </select> <input type="submit"> </form> </body> </html> retrieve.php: Code: [Select] <?php $selectedoption = $_POST['select']; print" <html> <body> <select id='select' name='select' > <option>option1</option> <option>option2</option> <option>option3</option> <option>etc...</option> </select> </body> </html> "; ?> How do I specify that the select menu in retrieve.php should have the option selected that was chosen from the select menu in send.htm? I'm guessing this is probably quite straightforward, but I can't quite work it out. Please help! Thanks. Hi all, I have a html form list box that needs to show all the types of products that are for sale and have a number in the list box saying how many of that type are for sale. eg. If the mysql database looks like this productType | productDetails chair | outdoor large chair table | 2m x 1m chair | indoor chair chair | childs chair I would want the form list box to show the values inside as: chair (3) table (1) could someone give me the heads up on how to do this Thanks Hi all, I have a html drop down menu and want to show the initially selected value as the one stored in the mysql database that a member selected. I have 180+ rows in mysql that a member could have selected and would like to basically do this: IF (value of option == $row['value'] ) { echo "selected=\"selected\"; } I could put this in every one of the rows it would be much better to have an if statement. As there are so many values I do not know where to begin. Can someone point me in the right direction? Thanks Hey guys, i have a small problemo. As you can see i have made an option list from 0 to 3. How can I put this optionlist in a for loop so I can save some place? I have found an example but I dont know how to integrate it in my php Example Code: [Select] for( $i = 0; $i < 4; $i++ ) { $optionlist .= "<option value=\"$i\">$i</option>\n"; } $optionlist .= "</select>\n"; Code: [Select] <?php $dranken = array("cola", "fanta", "bier", "koffie", "thee"); $prijzen = array("2", "2", "1.80", "2.20", "2.20"); $i = 0; echo "<table>"; while ($dranken[$i]) { $listnaam = $dranken[$i] . "_aantal"; $optionlist = "<select name= '$listnaam'><option>0</option><option>1</option><option>2</option><option>3</option></select>"; echo "<tr><td >" . $dranken[$i] . "</td>"; echo "<td>" . $prijzen[$i] . "</td>"; echo "<td>" . $optionlist . "</td></tr>"; $i++; } echo "</table>"; ?> thanks i have the following code: <td width='100px'>Suppliers <select name="supplier"> <?php $catcher_id = $service->getCatcherId(); $supplier_names = LpmAdnetworkPeer::getByName($catcher_id); foreach($supplier_names as $row) { ?> <option><?php echo $row->getName(); ?></option> <?php } ?> </select> </td> then on the same form i have a submit button that takes me to the next form..the problem now is how can i access the ID of the item seleted in the dropdown on the NEXT form please? i can get the name from the list by $_POST['supplier'] on the next form thanks i skipped solving this the other day with an easier way but now i am stuck with this stumble again in another area and i have no way out.... so here it goes, i have an ajax dropdown box...i need to get the value that is selected by the user when it is clicked and then pass this value to a new pop window by appending to its url....any suggestions? |