PHP - Selection In One Drop Down Box, Makes Another Drop Down Box Appear
I am creating a form that will allow the user to select the make of vehicle "FORD" for example. If that make of vehicle is selected among different makes of vehicles, then another box will appear, with all the models for that particular model "Fiesta" for example.
What type of code accomplishes this setup in my web page? I do not want to list 500 models in one drop down list, but just those for each make in the first drop down list. Thanks much! Similar TutorialsHi All, I have 2 tables: one CarMake - CarMakeID - CarMakeDesc two CarModel - CarModelID - CarModelMake - CarModelDesc Depending on what the user selects in the first dropdown (carmake) the possible selection in the second dropdown (model) needs to be limited to only the models from the selected carmake. in the second table (Carmodel : the 'CarModelMake' = CarMakeID, to identify the make) How do I limit the dropdown 'CarModel' based on the selected CarMake in the first dropdown. link : http://98.131.37.90/postCar.php code : -- -- -- Code: [Select] <label> <select name="carmake" id="CarMake" class="validate[required]" style="width: 200px;"> <option value="">Select CAR MAKE...</option> <?php while($obj_queryCarMake = mysql_fetch_object($result_queryCarMake)) { ?> <option value="<?php echo $obj_queryCarMake->CarMakeID;?>" <?php if($obj_queryCarMake->CarMakeID == $CarAdCarMake) { echo 'selected="selected"'; } ?> > <?php echo $obj_queryCarMake->CarMakeDesc;?></option> <?php } ?> </select> </label> <label> <select name="carmodel" id="CarModel" class="validate[required]" style="width: 200px;"> <option value="">Select MODEL...</option> <?php while($obj_queryCarModel = mysql_fetch_object($result_queryCarModel)) { ?> <option value="<?php echo $obj_queryCarModel->CarModelID;?>" <?php if($obj_queryCarModel->CarmodelID == $CarAdCarModel) { echo 'selected="selected"'; } ?> > <?php echo $obj_queryCarModel->CarModelDesc;?></option> <?php } ?> </select> </label> I'm a novice.. and appreciates all the help ! If I have a drop-down list... Code: [Select] <select name="attendees"> <option value="">--</option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> </select> Where does the value that the user selects get stored? Does it get stored in $_POST by default (if a Form is used)? What variable would hold the selected value? Debbie guys i have this dropdown menu echo "<form method=\"post\" action=\"\">"; echo "<center><select name=\"mydropdown\" size=\"0\" style=\"height:4em; width:15em;\">"; echo "<option value=\"Milk1\">{$_SESSION['tem1']}</option>"; echo "<option value=\"Milk2\">{$_SESSION['tem2']}</option>"; echo "<option value=\"Milk3\">{$_SESSION['tem3']}</option>"; echo "</select></center>"; echo "</form>"; and i wand to update a variable every time i select one of the contents... how can i done this??? I am trying to make a Web Form for people to fill out that has two drop down menus where i want the first drop down menu's selection changes the values of the selections in the second drop down. ((IE. If the first drop down value is "product 01" then the second drop down shows values "Red, Green, Blue" while if the first drop down value is "product 02" then the second drop down shows values "Yellow, Green")) Anyone have any ideas? Thanks in Advance. hello fellas, need some help please if possible. i have created a date of birth section in my form where the user selects his/her date of birth from the dropdown menu. they would first select the day then month then year of their birthday. how would i setup the database to get this to work? i currently have: Code: [Select] day VARCHAR( 2 ) NOT NULL , month VARCHAR( 4 ) NOT NULL , year VARCHAR( 4 ) NOT NULL , is this correct? many thanks Hello, i want to reproduce a content switcher based on drop-down menu (example: http://www.infocercetare.ro/index.php) anyone can help me with the logic of the function the rest i think i can handle. best regards This topic has been moved to Ajax Help. http://www.phpfreaks.com/forums/index.php?topic=341998.0 Need some help to define the php logic to echo back a selection if/when chosen from the drop down menu. Note: I'm using just a small subset of the USA states to show setup and what I'm trying to accomplish - it will be the same for all the other drop down menus as well.... Here's the example code: Code: [Select] <form name="form1" method="post" action="<?php $_SERVER[PHP_SELF]; ?>"> <select name="address_state" id="address_state" tabindex="25"> <option value="" selected> - Select State -</option> <option value="AK">Alaska</option> <option value="AL">Alabama</option> <option value="AR">Arkansas</option> <option value="AZ">Arizona</option> <option value="CA">California</option> <option value="CO">Colorado</option> <option value="CT">Connecticut</option> <option value="DC">District of Columbia</option> </select> <input name="submit" type="submit" value="submit" tabindex="900" id="submit" /> </form> Issue: I have validation (logic) define to check all the (other) text, radio, and check boxes of the form and that all works great! However, any time one of those particular fields fail validation, any selected "drop down" item is lost - it defaults back to no selection and the user has to make all drop down menu selections again. Can someone provide method to echo back - in php being the page reloads to itself - the drop down selection as in the example above? Thanks! My problem is when the entries in the form are validated and some fields with incorrect entries are found the fields filled out stay on the re-load of the form, all of them except the US States drop down menu which reverts back to Alabama every time. I would like to see that the user selection stays in place even after a re-load of the form. and with my limited experience I am having an issue trying to figure this out for a school project. Any help at all would be greatly appreciated, like what variables to create/call on and where exactly I insert this code. Much appreciated. here is the display code: <?php /* Program name: exercise_4_form.inc * Description: Defines a form that collects a user's * name and mailing address. */ $rows = array( "first_name" => "First Name (optional)", "last_name" => "Last Name", "phone" => "Phone Number", "city" => "City", "state" => "State", "address" => "Address", "zip_code" => "ZIP Code", "e_mail" => "E-Mail"); $states_list = array('AL'=>"Alabama",'AK'=>"Alaska",'AZ'=>"Arizona", 'AR'=>"Arkansas",'CA'=>"California",'CO'=>"Colorado",'CT'=>"Connecticut", 'DE'=>"Delaware",'DC'=>"District Of Columbia",'FL'=>"Florida",'GA'=>"Georgia", 'HI'=>"Hawaii",'ID'=>"Idaho",'IL'=>"Illinois", 'IN'=>"Indiana", 'IA'=>"Iowa", 'KS'=>"Kansas",'KY'=>"Kentucky",'LA'=>"Louisiana",'ME'=>"Maine", 'MD'=>"Maryland", 'MA'=>"Massachusetts",'MI'=>"Michigan",'MN'=>"Minnesota", 'MS'=>"Mississippi",'MO'=>"Missouri",'MT'=>"Montana",'NE'=>"Nebraska", 'NV'=>"Nevada",'NH'=>"New Hampshire",'NJ'=>"New Jersey",'NM'=>"New Mexico", 'NY'=>"New York",'NC'=>"North Carolina",'ND'=>"North Dakota",'OH'=>"Ohio", 'OK'=>"Oklahoma", 'OR'=>"Oregon",'PA'=>"Pennsylvania",'RI'=>"Rhode Island", 'SC'=>"South Carolina",'SD'=>"South Dakota",'TN'=>"Tennessee",'TX'=>"Texas", 'UT'=>"Utah",'VT'=>"Vermont",'VA'=>"Virginia",'WA'=>"Washington",'WV'=>"West Virginia", 'WI'=>"Wisconsin",'WY'=>"Wyoming"); $submit = "Submit mailing information"; ?> <html> <head> <style type='text/css'> <!-- body { background: #42413C; color: #000; font-family: Tahoma, Geneva, sans-serif; font-size: 90%; } form { margin: 1em 2 2 2; padding: 1; width: 1100px; margin-left: 35%; } .field {padding-bottom: 1em; } label { font-weight: bold; float: left; width: 15%; margin-right: 1em; text-align: right; background-color: #9F6; padding-right: 5px; } select {margin-bottom: 1em;} #submit { margin-left: 15%; } h3 { width: 500px; margin-left: 35%; } --> </style> </head> <body> <h3>Please enter your mailing information below:</h3> <?php /* loop that displays the form */ echo "<form action='$_SERVER[PHP_SELF]' method='POST'>"; foreach($rows as $field => $label) if($field == "state") { echo "<label for='$field'>$label</label> <select id='$field' name='$field'>\n"; foreach ( $states_list as $abbr => $sname ) { echo "<option value='$abbr'>$sname</option>\n"; } echo "</select>\n"; } else { echo "<div class='field'><label for='$field'>$label</label> <input id='$field' name='$field' type='text' value='".@$$field."' size='25%' maxlength='65' /></div>\n"; } echo "<div id='submit'> <input type='hidden' name='submitted' value='yes'> <input type='submit' value='$submit'></div>"; ?> </form> </body> </html>Here is the Logic on the combined php file that displays and has the statements on it for the form: <?php /* Program name: exercise_4_form_verifications.php * Description: Program checks all the form fields for * blank fields and verifies them */ if(isset($_POST['submitted']) and $_POST['submitted'] == "yes") { foreach($_POST as $field => $value) { if(empty($value)) { if($field != "first_name") { $blank_array[] = $field; } } else { $good_data[$field] = strip_tags(trim($value)); } } if(@sizeof($blank_array) > 0) { $message = "<p style='color: black; margin-bottom: 0;font-weight: bold; margin-left: 35%'> You didn't fill in one or more required fields. You must enter: <ul style='color: yellow; margin-top: 0; margin-left: 35%; list-style: none' >"; foreach($blank_array as $value) { $message .= "<li>$value</li>"; } $message .= "</ul>"; echo $message; extract($good_data); include("exercise_4_form.inc"); exit(); } foreach($_POST as $field => $value) { if(!empty($value)) { $name_verify = "/^[A-Za-z' -]{1,50}$/"; $phone_verify = "/^[0-9)(xX -]{7,20}$/"; $city_verify = "/^[A-Za-z' -]{1,50}$/"; $address_verify = "/^[A-Za-z0-9 .,'-]{1,50}$/"; $zip_verify = "/^[0-9]{5}(\-[0-9]{4})?$/"; $e_mail_verify = "/^.+@.+\\..+$/"; if(preg_match("/last/i",$field)) { if(!preg_match($name_verify,$value)) { $error_array[] = "$value is not a valid last name"; } } if(preg_match("/phone/i",$field)) { if(!preg_match($phone_verify,$value)) { $error_array[] = "$value is not a valid phone number"; } } // endif phone format check if(preg_match("/city/i",$field)) { if(!preg_match($city_verify,$value)) { $error_array[] = "$value is not a valid city"; } } if(preg_match("/address/i",$field)) { if(!preg_match($address_verify,$value)) { $error_array[] = "$value is not a valid address"; } } if(preg_match("/zip/i",$field)) { if(!preg_match($zip_verify,$value)) { $error_array[] = "$value is not a valid ZIP code"; } } if(preg_match("/e_mail/i",$field)) { if(!preg_match($e_mail_verify,$value)) { $error_array[] = "$value is not a valid e-mail address"; } } } $clean_data[$field] = strip_tags(trim($value)); } if(@sizeof($error_array) > 0) { $message = "<ul style='color: yellow; margin-top: 0; margin-left: 35%; list-style: none' >"; foreach($error_array as $value) { $message .= "<li>$value</li>"; } $message .= "</ul>"; echo $message; extract($clean_data); include("exercise_4_form.inc"); exit(); } else { echo "<ol>"; foreach($_POST as $field => $value) { echo "<h3><li> $field = $value</li></h3>"; } echo "</ol>"; } } else { include("exercise_4_form.inc"); } ?> Edited by mac_gyver, 15 November 2014 - 12:14 PM. code tags around posted code please Hi all, ive got a script ive finishing coding from the other day, but ive got a slight problem with it. <?php if ($fetch->missions == "4"){ // Mission 4, Toyota Aygo up to 500MPH $car = "Toyota Aygo"; if ($garage->car == $car && $garage->mph == "500"){ $rightcar = "Yes"; }else{ $rightcar = "No"; } if (strip_tags($_POST['submit'])){ if ($rightcar == "Yes"){ echo ("<table width='30%' cellpadding='0' align='center' cellspacing='0' border='1' class='table'> <tr> <td class='header' align='center'>Well Done!</td> </tr> <tr> <td>You Successfully Completed Mission 3!</td> </tr> </table><br />"); $newmoney = $fetch->money + 70000000; $newrep = $fetch->rep + 750000; mysql_query ("UPDATE users SET money='$newmoney' WHERE username='$username'"); mysql_query ("UPDATE users SET rep='$newrep' WHERE username='$username'"); mysql_query ("UPDATE users SET missions='5' WHERE username='$username'") or die ("Error - Query : Line 121 : " . mysql_error()); mysql_query ("INSERT INTO `inbox` ( `id` , `to` , `from` , `message` , `date` , `read` , `saved` , `event_id` ) VALUES ( '', '$username', 'System', 'Well done! <br /> As You completed Mission 4 Successfully we will reward you with<br /> £70,000,000 Money<br /> 750,000 Rep!', '$date', '0', '0', '0' )"); } } } // Mission 4 ?> <?php if ($fetch->missions == "4"){ ?> <form action="" method="POST" name="mission4"> <table width="50%" cellpadding="0" cellspacing="0" border="1" class="table" align="center"> <tr> <td class='header' align='center'>Mission 4</td> </tr> <tr> <td>Now SD Stars can now see your progressing, they have asked you if you can get a Toyota Aygo up to 500MPH. You must get it to them without any damage to prove your werthy!</td> </tr> <tr> <td align='center' class='header'>Select Your Car:</td> </tr> <tr> <td><select class="dropbox"> <option selected>Pick Car</option> <?php $get=mysql_query("SELECT * FROM garage WHERE owner='$username' AND car='Toyota Aygo' AND mph='500'"); while($stats=mysql_fetch_object($get)){ echo "<option value=?carname=$stats->id>$stats->car, $stats->damage%, $stats->mph MPH</option>"; } ?></select></td> </tr> <tr> <td class='omg'><input type="submit" name="submit" class="button" value="Send Car!"></td> </tr> </table> </form> <?php } ?> I blive it works apart from the Drop Down Box. It selects all the cars its suppose to but when I pick the car and press submit it just basicly refreshes the page :S. I'm not sure why its doing it . Can any one else see the problem? Thanks for any help given Hi all, ive just coded a new thing for my website which lets users upgrade there car to a certain speed, its all coded and works fine, but im just not sure how todo one thing on it. <?php $getall = mysql_query("SELECT car, id FROM garage WHERE owner='$username'"); while($row = mysql_fetch_array($getall)){ echo $row['car']. " - ". $row['id']; echo "<br />"; } ?> That bit of my code, select the car and the car ID from my table garage, but i'm wanting it in a drop down box which shows all the cars in there garage, but not sure how I would do this. Any advice? Thanks. hi how do i get the id value from the dropdown selection from the database Here is my code. i need the id to store it another table to refer it in my application. its like a categories and subcategories table. <html> <form id="name" action="<?php $_POST['SERVER_SELF'] ?>" method="POST" > <input type="text" name="name1"> <br> <select name="select"> <?php mysql_connect('localhost','root','') or die('Could not connect to mysql ' . mysql_error()); // error message mysql_select_db("dbtest") or die(mysql_error()); $query = "select name,id from main"; $result = mysql_query($query); if ($query) { while ($row = mysql_fetch_array($result)) { $strB=$row['name']; ?> <option value="<?php $stra ?>"><?php echo $strB ?></option> <br> <?php } } ?> </select> <input type="text" name="name2"> <br> <input type="submit" name="submit1"> </form> <?php if(isset($_POST['submit'])) { //$name=$_POST['val']; $strB=$stra; mysql_connect('localhost','root','') or die('Could not connect to mysql ' . mysql_error()); // error message mysql_select_db("dbtest") or die(mysql_error()); $query = "insert into sub (sub) values('$strB')"; $result = mysql_query($query); if ($query) { echo "query executed"; } } ?> </html> thanks trying to get my drop down box to display more then one result. I have two users with Mod wrote in the column Monitor but right now it only displays just the one. So I need to display all the goaulds that have mod in the monitor column <?php Code: [Select] $result = mysql_query("SELECT goauld FROM users WHERE monitor ='mod' ORDER BY id DESC") or die(mysql_error()); while($row = mysql_fetch_array( $result )) { if($row['goauld'] == $_POST['name_list']){ echo '<div class="containerc">'; echo '<div class="search">'; echo '<form method="post" >'; echo '<select name="name_list" class="textbox" id="name_list">'; echo "<option value=\"".$row['goauld']."\">".$row['goauld']."</option>"; echo "<option selected=\"selected\" value=\"".$row['goauld']."\">".$row['goauld']."</option>"; echo '</select>'; echo '<input type="submit" name="remove" id="remove" value="Remove">'; } } } '</form>'; '</div>'; '</div>'; ?> Hello, I'm trying to use a dropdown to display different divs. <html> <head> <title>DDlist Div Display</title> <script type="text/javascript"> function ShowDivArea(info) { var sel = document.getElementById('divArea').getElementsByTagName('div'); for (var i=0; i<sel.length; i++) { sel.style.display = 'none'; } if (info == '0') { return; } document.getElementById('divArea'+info).style.display = 'block'; } </script> <style type="text/css"> .divArea { display:none; height:100px; width:200px; border:1px solid red; } </style> </head> <body> <select id="DDDivList" onchange="ShowDivArea(this.selectedIndex)"> <option value="0" selected> -- Select A Design Service --</option> <option value="1"> QR Bookmark </option> <option value="2"> Twitter </option> <option value="3"> Ning or Tumblr </option> <option value="4"> Flyers </option> <option value="5"> Business Card or Brochure</option> <option value="6"> Album or Mixtape cover</option> <option value="7"> Other</option> </select> <div id="divArea"> <div id="divArea1" class="divArea"> <h1>QR Bookmarks</h1> <form action='qrgen.php' method='POST'> <input type='text' name='bmarksite'> URL<br> <input type='text' name='sitedesc'> Description<br> <input type='submit' value='Convert'><br> <?php include("QrCodes.php") ?> <?php $bmarksite = $_POST['bmarksite']; $sitedesc = $_POST['sitedesc']; if ($sitedesc&&$bmarksite) { $qrcode = new QrCodes; $qrcode -> IsImage =1; echo $qrcode -> GetBookmark("$bmarksite","$sitedesc"); echo "</br>"; echo $bmarksite; echo "</br>"; print $sitedesc; } else { echo "Please fill in all the required fields."; } ?> </div> <div id="divArea2" class="divArea">Twitter</div> <div id="divArea3" class="divArea">Ning or Tumblr</div> <div id="divArea4" class="divArea">Flyers</div> <div id="divArea5" class="divArea">Business Card or Brochure</div> <div id="divArea6" class="divArea">Album or Mixtape Cover</div> <div id="divArea7" class="divArea">Other</div> </div> </body> </html> The problem is with this line: <form action='qrgen.php' method='POST'>. I am using a wordpress page template. What should I change the qrgen.php to? Or how do I get it to work? Thank you for your help. The code for the drop down: Code: [Select] echo '<select name="select" ONCHANGE="goto(this.form)">'; while ($dropDown > 0) { if ($page == $dropDown) { $selected = 'selected="selected"'; } else { $selected = ""; } echo "<option value=\"searchFlights.php?page=" . $dropDown . "\" $selected>" . $dropDown . "</option>"; $dropDown = $dropDown - 1; } echo '</select>'; It works fine, but I want 1 to be at the top of the list and go down from there because right now, it starts with the biggest number and goes down. Any suggestions? Thanks! Hello, must admit i'm very new to this and just a hobiest , I am creating a page which will be used for logging information. With a mysql database i have two tables "tbl_rides" & "tbl_courses" rides is the main table for storing information and has a foreign key associating column "course_id" with course_id in the tbl_courses What i want to do on a HTML/PHP page is display a drop down box that has the contents of "course" from the tbl_courses but when the user selects it and submits the record is written to the main form "tbl_rides" "course_id" so display the friendly name via the foreign key but the record is written to "tbl_rides" Can that be done .. am i going about this wrong ?
Thanks for any help Andrew. Holy smoke, it has been a looooooong time since I have been here. Love the new look of the site btw. Well I am working on a new project for my church. The idea is an email to SMS program. So one use of this would be that if there is a youth activity I could send a message to the entire youth group with one message. I am trying to figure out something specific with it. I need to create a form that grabs info from the database. So more specifically what I need is one drop down that will have "Youth Group", "Parents", "Deacons", etc. now based on what I select I need the second drop down to populate with the corresponding people in that group. Now I want an "All" to be in the drop down to which would let me send the message I type to ALL people in the desired group but to also have the individual names come up just in case I need to do a specific person. How would I go about doing something like this? I have done much research and cant quite find what I would need to do. Any help would be GREATLY appreciated. Is there a way to use a html form and a dropdown menu to INSERT data into a mysql database for example can get the form to submit but then i want to use the dropdown menu to be the different tables in the mysql database. Any ideas? Hi, i know this is probably very basic but i have been banging my head and looking for tuts. i have built a mysql php dropdown menu. all displays fine. now, how do i get the menu to actualy take me to a new url? the new url should be www.mysite.com/"menu selection" Code: [Select] <? include_once 'includes/db.php'; $result = mysql_query("select * from crimerate WHERE DISTRICT = 'Limpopo'", $con); if (!$result) { die('Invalid query: ' . mysql_error()); } $options=""; while ($row=mysql_fetch_array($result)) { $id=$row["id"]; $crime=$row["CRIME"]; $options.="<OPTION VALUE=\"$id\">$crime</option>"; } ?> <SELECT NAME=crime> <OPTION VALUE=0>Choose <?=$options?> </SELECT> |