PHP - [help] Dynamic Dependent Select Boxes
Brand new to the forum, and have only been working with HTML, PHP, & MySQL for about 3 months.
I've run into an issue I can't seem to figure out how to work around. I want to have two select boxes, that are dependent on one another. For Example: First box lists types of games: RPG LARP Board Game Miniatures CCG The second box displays game names of the type selected above. I have a MySQL table setup for the second box. To be honest I kinda already have it working... with one small but MAJOR glitch. I select the first box and the page submits using onchange='this.form.submit()'. When the page reloads the first select box reverts back to it's default setting, but the second box does correctly filter & show the content based on the selection that was made in the first box. My Question: How do I set this up so that the first box displays the user selected setting while also passing the $_POST information to the second box? My Code: <table> <tr> <td width=50%> Game Type: </td> <td width=50%> <form name='game_type' action='event_submit.php' method='post'> <select name='game_type' align=center onchange='this.form.submit()' style="width:150px;margin:5px 0 5px 0;"> <option value=''>Chose a Game Type</option> <option value='RPG'>RPG</option> <option value='LARP'>LARP</option> <option value='Board Game'>Board Game</option> <option value='CCG'>Collectable Cards & Games</option> <option value='Miniatures'>Miniatures</option> </select> </form> </td> </tr> <tr> <td width=50%> Game System: </td> <td width=50%> <?php $game_type = $_POST['game_type']; echo "$game_type"; if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("DrowCon", $con); $result = mysql_query("SELECT * FROM Games WHERE Type='$game_type' ORDER BY System ASC"); echo "<form name='game_system' action='event_submit_form.php' method='post'>"; echo "<select name='game_system' align=center style='width:150px;margin:5px 0 5px 0;'>"; echo "<option value=''>Chose a Game System</option>"; while($row_game = mysql_fetch_array($result)) { echo "<option value=''>".$row_game['System']."</option>"; } echo "</form>"; mysql_close($con); ?> </td> </tr> </table> Similar TutorialsDear Support, Here is my code: <?php if ( strtolower($_SERVER['REQUEST_METHOD']) === 'post' ) { $name = isset($_POST['name]) ? htmlspecialchars($_POST['name']) : ""; $area = isset($_POST['area']) ? htmlspecialchars($_POST['area']) : ""; $choice = isset($_POST['choice ']) ? htmlspecialchars($_POST['choice ']) : ""; //Do Others } ?> <form class="customersForm" method="post"> <div class=""> <p>Name<br /> <select style="width:120px;" name="name"> <?php $sql = "select * from `name_info` order by `name`"; $query = mysql_query($sql); $dd = '<option>Name</option>'; while($row = mysql_fetch_object($query)) { $dd .= '<option>'.$row->name.'</option>'; } echo $dd; ?> </select> </p> </div> <div class=""> <p>Area<br /> <select style="width:120px;" area="area"> <?php $sql = "select * from `area_info` order by `area`"; $query = mysql_query($sql); $dd = '<option>Area</option>'; while($row = mysql_fetch_object($query)) { $dd .= '<option>'.$row->area.'</option>'; } echo $dd; ?> </select> </p> </div> <div class=""> <p>Choice<br /> <select style="width:120px;" choice="choice"> <?php $sql = "select * from `choice_info` where `name`='".$name."' and `area`='".$area."' order by `choice`"; $query = mysql_query($sql); $dd = '<option>Choice</option>'; while($row = mysql_fetch_object($query)) { $dd .= '<option>'.$row->choice.'</option>'; } echo $dd; ?> </select> </p> </div> ......//Others input fields ...some text fields ... </ form> I have the above code, where I would like to show the choice Drop-down box, depends on $name and $area match of the choice_info table. $name field and $area field needs to have any dependency and straight from the MySQL table. But, choice needs to depend on $name & $area. How can I do it without much using Javacript? I do not mind to use JavaScript, but prefer to minimal. By the way, I will have some other input fields too. It's an input form but, one drop-down box will be depends on two others choice. Thanks for your help. I have a table in a database that I need to filter upon using dependent select boxes. The database was poorly designed and would be difficult to break into separate tables without affecting code written in other places. I have found some good code and understanding of how to implement the dependent select boxes if you have two tables with one column functioning as a lookup value between the two. I have provided a short bit of sql of what the table looks like with some column names changed. I would need to first sort by lastname and have firstname dependent on the selection of lastname. Any help would be greatly appreciated. Code: [Select] create table `user` ( `id` int(3) Not Null Auto_increment, `firstname` varchar (60), `lastname` varchar (24), `age` double , `hometown` varchar (75), `job` varchar (75), `birthdate` date ); insert into `user` (`id`, `firstname`, `lastname`, `age`, `hometown`, `job`, `birthdate`) values ('1','Peter','Griffin','41','Quahog','Brewery','1960-01-01'), ('2','Lois','Griffin','40','Newport','Piano Teacher','1961-08-11'), ('3','Joseph','Swanson','39','Quahog','Police Officer','1962-07-23'), ('4','Glenn','Quagmire','41','Quahog','Pilot','1960-02-28'), ('5','Megan','Griffin','16','Quahog','Student','1984-04-24'), ('6','Stewie','Griffin','2','Quahog','Dictator','2008-03-03'); Hi, I had a form which contains, 8 select boxes. In those 8 select boxes, 3 of the boxes are interlinked, like, I am able to generate data in other 2 select boxes, if one box is been selected. i.e. based upon selection of one select box, and i am able to generate data in other select box. So, 3 select boxes are interlinked. And remaining 5 selects are optional, if the user selects, then we need to generate data based upon selection of the user. Here the main issue is how to grab data in the post back form, like, how to capture the number of selects boxes selected and what are those selected, and based upon that we need to generate data. Here one more important thing is that, in the report generation form(postback form), I need to show the data in a table. So , in table I had 9 columns and these would be common to whatever the selection made, but, only the requeired data would be changed based upon the selection of the remaining 5 optional boxes. Hope you got me!!!! Thanks in advance for any help that you may be able to provide. I'm a graphic designer with VERY limited knowledge of php, javascript, etc. I'm trying to create a form on my company's website that will allow customers to view specifications for particular products. I'm trying to use selection boxes to narrow the customer's search, with the selection from the second box dependent on the first, and the resulting information displayed dependent on the choice from the second list. So far, I've only been able to adapt some code I found in a tutorial to display the data in the first selection box. No matter what I've tried, I can't seem to get any information to display in the second box, let alone information that is dependent on the first selection. I am brand new to php and javascript, and I didn't even know AJAX, JSON, or JQuery even existed until only a few days ago. Below is the three php files I'm using to try and make this work. If someone could point out what is wrong with it, I would be eternally grateful. Here is the code from db.php, the file I'm using to connect to my database: Code: [Select] <?php /*changing "db_username" to "root", changing "db_password" to "xxXXXX", changing "database" to "trailers" */ mysql_connect("localhost", "root", "xxXXXX") or die(mysql_error()); mysql_select_db("trailers") or die(mysql_error()); ?> Here is the code from func.php, the file with all the functions and database queries: Code: [Select] <?php //************************************** // Page load dropdown results - changing tier_one to tnttrailer_brand, changing two_drops to trailerquote, changing 'tier_one' to 'tnttrailer_brand' // //************************************** function getTierOne() { $result = mysql_query("SELECT DISTINCT tnttrailer_brand FROM trailerquote") or die(mysql_error()); while($tier = mysql_fetch_array( $result )) { echo '<option value="'.$tier['tnttrailer_brand'].'">'.$tier['tnttrailer_brand'].'</option>'; } } //************************************** // First selection results - changing two_drops to trailerquote, changing tier_one to tnttrailer_brand, changing 'tier_two' to 'trailer_description', // //************************************** if($_GET['func'] == "drop_1" && isset($_GET['func'])) { drop_1($_GET['drop_var']); } function drop_1($drop_var) { include_once('db.php'); $result = mysql_query("SELECT trailer_description FROM trailerquote WHERE tnttrailer_brand='$drop_var'") or die(mysql_error()); echo '<select name="tier_two" id="tier_two"> <option value=" " disabled="disabled" selected="selected">Choose one</option>'; while($drop_2 = mysql_fetch_array( $result )) { echo '<option value="'.$drop_2['trailer_description'].'">'.$drop_2['trailer_description'].'</option>'; } echo '</select> '; echo '<input type="submit" name="submit" value="Submit" />'; } ?> Finally, this is the code from index.php, the file with the form, javascript and the AJAX: Code: [Select] <?php include('db.php'); include('func.php'); ?><!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>Chained Select Boxes using PHP, MySQL and jQuery</title> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script> <script type="text/javascript"> $(document).ready(function() { $('#wait_1').hide(); $('#drop_1').change(function(){ $('#wait_1').show(); $('#result_1').hide(); $.get("func.php", { func: "drop_1", drop_var: $('#drop_1').val() }, function(response){ $('#result_1').fadeOut(); setTimeout("finishAjax('result_1', '"+escape(response)+"')", 400); }); return false; }); }); function finishAjax(id, response) { $('#wait_1').hide(); $('#'+id).html(unescape(response)); $('#'+id).fadeIn(); } </script> </head> <body> <p> <form action="" method="post"> <select name="drop_1" id="drop_1"> <option value="" selected="selected" disabled="disabled">Select a Category</option> <?php getTierOne(); ?> </select> <span id="wait_1" style="display: none;"> <img alt="Please Wait" src="ajax-loader.gif"/> </span> <span id="result_1" style="display: none;"></span> </form> </p> <p> <?php if(isset($_POST['submit'])){ $drop = $_POST['drop_1']; $tier_two = $_POST['trailer_description']; echo "You selected "; echo $drop." & ".$tier_two; } /* changed 'tier_two' to 'trailer_description' */ ?> </body> </html> Here's the page on my website. I'm using Joomla for my website, and the above php is loaded into a module at the bottom of the page: http://www.tnttrailer.com/index.php/dimensions.html This topic has been moved to JavaScript Help. http://www.phpfreaks.com/forums/index.php?topic=347324.0 I was using this wonderful tutorial on dynamic check boxes and databases http://www.phpfreaks.com/tutorial/working-with-checkboxes-and-a-database . This example is uses checkboxes to give users admin rites. I have tried to revamp this code to handle more inputs from checkboxes but some where I have made some errors. <?php include("db.php"); $updated = FALSE; if(count($_POST) > 0){ $DEP = $_POST['MMARS_DEP']; array_map('intval',$DEP); $DEP = implode(',',$DEP); mysql_query("UPDATE master_roles SET MMARS_DEPT=0") or trigger_error(mysql_error(),E_USER_ERROR); mysql_query("UPDATE master_roles SET MMARS_DEPT=1 WHERE id IN ($DEP)") or trigger_error(mysql_error(),E_USER_ERROR); $MD = $_POST['MMARS_MD']; array_map('intval',$MD); $MD = implode(',',$MD); mysql_query("UPDATE master_roles SET MMARS_MD=0") or trigger_error(mysql_error(),E_USER_ERROR); mysql_query("UPDATE master_roles SET MMARS_MD=1 WHERE id IN ($MD)") or trigger_error(mysql_error(),E_USER_ERROR); $ORG = $_POST['MMARS_ORG']; array_map('intval',$ORG); $ORG = implode(',',$ORG); mysql_query("UPDATE master_roles SET MMARS_ORG=0") or trigger_error(mysql_error(),E_USER_ERROR); mysql_query("UPDATE master_roles SET MMARS_ORG=1 WHERE id IN ($ORG)") or trigger_error(mysql_error(),E_USER_ERROR); $SEC = $_POST['MMARS_SEC']; array_map('intval',$SEC); $SEC = implode(',',$SEC); mysql_query("UPDATE master_roles SET MMARS_SEC=0") or trigger_error(mysql_error(),E_USER_ERROR); mysql_query("UPDATE master_roles SET MMARS_SEC=1 WHERE id IN ($SEC)") or trigger_error(mysql_error(),E_USER_ERROR); $BOG = $_POST['MMARS_BOG']; array_map('intval',$BOG); $BOG = implode(',',$BOG); mysql_query("UPDATE master_roles SET MMARS_BOG=0") or trigger_error(mysql_error(),E_USER_ERROR); mysql_query("UPDATE master_roles SET MMARS_BOG=1 WHERE id IN ($BOG)") or trigger_error(mysql_error(),E_USER_ERROR); $SW = $_POST['MMARS_SW']; array_map('intval',$SW); $SW = implode(',',$SW); mysql_query("UPDATE master_roles SET MMARS_SW=0") or trigger_error(mysql_error(),E_USER_ERROR); mysql_query("UPDATE master_roles SET MMARS_SW=1 WHERE id IN ($SW)") or trigger_error(mysql_error(),E_USER_ERROR); $updated=TRUE; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>phpfreaks checkbox tutorial</title> </head> <body> <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post"> <?php if($updated===TRUE){ echo '<div>Privileges Updated!</div>'; } ?> <table> <tr> <th><td>DEPT</td><td>MD</td><td>ORG</td><td>SEC</td><td>BOG</td><td>SW</td></th> <?php $label = array('MMARS','LCM','NM SEC','Classic MMARS','CAPS','PMIS','WRK COMP'); $table = array('MMars','LCM', 'NMSec', 'ClassMars', 'CAPS', 'PMIS', 'WrkComp'); $i= 0; while($i < 1){ echo "<tr>" ."<td>".$label[$i]."</td>"; $array = array(); $count = 0; $Aid = mysql_query("SELECT DISTINCT id FROM DB") or die (mysql_error()); while ($row= mysql_fetch_assoc($Aid)){ $ID = $row['id']."<br>"; $array[$count] = $ID; $count++; } $Disabled =""; $sql = "SELECT id, UAID, DB_NAME, MMARS_DEPT, MMARS_MD, MMARS_ORG, MMARS_SEC, MMARS_BOG, MMARS_SW FROM master_roles WHERE `UAID`='1000' AND id <= 6"; $result = mysql_query($sql) or trigger_error(mysql_error(),E_USER_ERROR); $row = mysql_fetch_row($result); list($id, $UAID, $DB_NAME, $MMARS_DEP, $MMARS_MD, $MMARS_ORG, $MMARS_SEC, $MMARS_BOG, $MMARS_SW)= $row; $checked = ($MMARS_DEPT==1) ? 'checked="checked"' : ''; echo '<td>'.$array[0].'<input type="checkbox" name="MMARS_DEP[]" value="'.$array[0].'" '.$checked.' , '.$Disabled.' /></td>'; $checked = ($MMARS_MD==1) ? 'checked="checked"' : ''; echo '<td>'.$array[1].'<input type="checkbox" name="MMARS_MD[]" value="'.$array[1].'" '.$checked.' , '.$Disabled.' /></td>'; $checked = ($MMARS_ORG==1) ? 'checked="checked"' : ''; echo '<td>'.$array[2].'<input type="checkbox" name="MMARS_ORG[]" value="'.$array[2].'" '.$checked.' , '.$Disabled.' /></td>'; $checked = ($MMARS_SEC==1) ? 'checked="checked"' : ''; echo '<td>'.$array[3].'<input type="checkbox" name="MMARS_SEC[]" value="'.$array[3].'" '.$checked.' , '.$Disabled.' /></td>'; $checked = ($MMARS_BOG==1) ? 'checked="checked"' : ''; echo '<td>'.$array[4].'<input type="checkbox" name="MMARS_BOG[]" value="'.$array[4].'" '.$checked.' , '.$Disabled.' /></td>'; $checked = ($MMARS_SW==1) ? 'checked="checked"' : ''; echo '<td>'.$array[5].'<input type="checkbox" name="MMARS_SW[]" value="'.$array[5].'" '.$checked.', '.$Disabled.' /></td>'; echo "</tr>"; $i ++; } ?> <tr><td colspan="2"><input type="submit" name="submit" value="Update Privileges" /></td></tr> </table> </form> </body> </html> After the post each individual checkbox input is saved into a variable then we query the database and check if any of the checkboxes have been changed to 1. If so then than right is updated to one. if(count($_POST) > 0){ $DEP = $_POST['MMARS_DEP']; array_map('intval',$DEP); $DEP = implode(',',$DEP); mysql_query("UPDATE master_roles SET MMARS_DEPT=0") or trigger_error(mysql_error(),E_USER_ERROR); mysql_query("UPDATE master_roles SET MMARS_DEPT=1 WHERE id IN ($DEP)") or trigger_error(mysql_error(),E_USER_ERROR); $MD = $_POST['MMARS_MD']; array_map('intval',$MD); $MD = implode(',',$MD); mysql_query("UPDATE master_roles SET MMARS_MD=0") or trigger_error(mysql_error(),E_USER_ERROR); mysql_query("UPDATE master_roles SET MMARS_MD=1 WHERE id IN ($MD)") or trigger_error(mysql_error(),E_USER_ERROR); $ORG = $_POST['MMARS_ORG']; array_map('intval',$ORG); $ORG = implode(',',$ORG); mysql_query("UPDATE master_roles SET MMARS_ORG=0") or trigger_error(mysql_error(),E_USER_ERROR); mysql_query("UPDATE master_roles SET MMARS_ORG=1 WHERE id IN ($ORG)") or trigger_error(mysql_error(),E_USER_ERROR); $SEC = $_POST['MMARS_SEC']; array_map('intval',$SEC); $SEC = implode(',',$SEC); mysql_query("UPDATE master_roles SET MMARS_SEC=0") or trigger_error(mysql_error(),E_USER_ERROR); mysql_query("UPDATE master_roles SET MMARS_SEC=1 WHERE id IN ($SEC)") or trigger_error(mysql_error(),E_USER_ERROR); $BOG = $_POST['MMARS_BOG']; array_map('intval',$BOG); $BOG = implode(',',$BOG); mysql_query("UPDATE master_roles SET MMARS_BOG=0") or trigger_error(mysql_error(),E_USER_ERROR); mysql_query("UPDATE master_roles SET MMARS_BOG=1 WHERE id IN ($BOG)") or trigger_error(mysql_error(),E_USER_ERROR); $SW = $_POST['MMARS_SW']; array_map('intval',$SW); $SW = implode(',',$SW); mysql_query("UPDATE master_roles SET MMARS_SW=0") or trigger_error(mysql_error(),E_USER_ERROR); mysql_query("UPDATE master_roles SET MMARS_SW=1 WHERE id IN ($SW)") or trigger_error(mysql_error(),E_USER_ERROR); The html is a straight forward form. I have arrays set up and while loop ready for implementation because I plan on having this repeat multiple times. ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>phpfreaks checkbox tutorial</title> </head> <body> <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post"> <?php if($updated===TRUE){ echo '<div>Privileges Updated!</div>'; } ?> <table> <tr> <th><td>DEPT</td><td>MD</td><td>ORG</td><td>SEC</td><td>BOG</td><td>SW</td></th> <?php $label = array('MMARS','LCM','NM SEC','Classic MMARS','CAPS','PMIS','WRK COMP'); $table = array('MMars','LCM', 'NMSec', 'ClassMars', 'CAPS', 'PMIS', 'WrkComp'); $i= 0; while($i < 1){ echo "<tr>" ."<td>".$label[$i]."</td>"; I had a lot of problems keeping the id unique for each checkbox. This is why I created an array with the ids and I use them as "value" in the input field. $array = array(); $count = 0; $Aid = mysql_query("SELECT DISTINCT id FROM DB") or die (mysql_error()); while ($row= mysql_fetch_assoc($Aid)){ $ID = $row['id']."<br>"; $array[$count] = $ID; $count++; } I sql the database for id, UAID, DB_NAME, and all of the necessary rites. With list I assign each row with the correct name. Each checkbox's associated field in the db is checked to see if it is set to 1. If it is checked="checked". $sql = "SELECT id, UAID, DB_NAME, MMARS_DEPT, MMARS_MD, MMARS_ORG, MMARS_SEC, MMARS_BOG, MMARS_SW FROM master_roles WHERE `UAID`='1000' AND id <= 6"; $result = mysql_query($sql) or trigger_error(mysql_error(),E_USER_ERROR); $row = mysql_fetch_row($result); list($id, $UAID, $DB_NAME, $MMARS_DEPT, $MMARS_MD, $MMARS_ORG, $MMARS_SEC, $MMARS_BOG, $MMARS_SW)= $row; $checked = ($MMARS_DEPT==1) ? 'checked="checked"' : ''; echo '<td>'.$array[0].'<input type="checkbox" name="MMARS_DEP[]" value="'.$array[0].'" '.$checked.' , '.$Disabled.' /></td>'; $checked = ($MMARS_MD==1) ? 'checked="checked"' : ''; echo '<td>'.$array[1].'<input type="checkbox" name="MMARS_MD[]" value="'.$array[1].'" '.$checked.' , '.$Disabled.' /></td>'; $checked = ($MMARS_ORG==1) ? 'checked="checked"' : ''; echo '<td>'.$array[2].'<input type="checkbox" name="MMARS_ORG[]" value="'.$array[2].'" '.$checked.' , '.$Disabled.' /></td>'; $checked = ($MMARS_SEC==1) ? 'checked="checked"' : ''; echo '<td>'.$array[3].'<input type="checkbox" name="MMARS_SEC[]" value="'.$array[3].'" '.$checked.' , '.$Disabled.' /></td>'; $checked = ($MMARS_BOG==1) ? 'checked="checked"' : ''; echo '<td>'.$array[4].'<input type="checkbox" name="MMARS_BOG[]" value="'.$array[4].'" '.$checked.' , '.$Disabled.' /></td>'; $checked = ($MMARS_SW==1) ? 'checked="checked"' : ''; echo '<td>'.$array[5].'<input type="checkbox" name="MMARS_SW[]" value="'.$array[5].'" '.$checked.', '.$Disabled.' /></td>'; I am attaching a print out of my joined table I am querying as well as my final vision for this form. In advance, thank you for your help. Hey guys,
My problem is, iam loading with mysql_fetch_array a table from my database and draw it into my website page.
The table is a user table which has an id, user, password and rights(Admin, Mod , User).
Only the rights are given out as <select> </select> boxes with the 3 options(Admin, Mod , User).
And i also have 1 more column which has for each row a href to a new site with the id of the array_index as paramter.
My Problem is i want to know which select box was changed and which option is selected so i can change the rights in my database of this user with the right id.
At the moment i cant post the code in here cause i have a problem with my laptop now.
I hope you can help me,
Thank you
Edited by Kandey, 30 October 2014 - 02:28 PM. Hi There, Is there any way that I can get a select box to auto-select the value from a SQL query? For example, if I have a select box like this: Dave,Jon,Simon,Fred And a select statement that brings back the value of Fred then the select box (in HTML) would: <select name='people'><option>Dave</option><option>Jon</option><option>Simon</option><option selected='selected'>Fred</option></select> Same as if the name was Dave, or Simon etc etc. Hope that makes sense? Cheers Matt Hi I am trying to have the following as an example: Code: [Select] <select name="test[]" multiple="multiple">options here</select> <select name="test[]" multiple="multiple">options here</select> Then using $_POST['test'] to grab the boxes in the array. I want to then loop over each box so for example: Code: [Select] $boxes = $_POST['test']; foreach ( $boxes as $box ) // now here i would of expected $box to be an array of the options, } but it's not working like this, all the options from all submitted select boxes come in one array so I cannot distinguish which select box in the array they relate to, is there a way to do this?? Hello, I have the following function function make_agent_drop($dropname,$parent=''){ $agents = mysql_query("SELECT * FROM ad_category WHERE cat_status='1' AND parent_id='".(is_numeric($parent)?$parent:"0")."'") or die(mysql_error()); $anum = mysql_num_rows($agents); if($anum>0){ $agentdrop='<select style="width:150px; height:20px; margin-left:100px; font-size:11px;" name="'.$dropname.'" id="'.$dropname.'" class="text" '.(is_numeric($parent)?'':'onchange="update_subcatdrop($(this).val());').'"> <option value="0">Select a Category</option>'; while($row= mysql_fetch_array($agents)){ $agentdrop.='<option value="'.$row['cat_id'].'">'.$row['cat_name'].'</option>'; } $agentdrop.='</select>'; }else{ $agentdrop= 'No '.(is_numeric($parent)?'Sub':'').'Categories Found.'; } return $agentdrop; ; } I creates a drop down from database cats and sub cats.. I am trying to figure out how to add a submit button to dynamically appear when it displays the sub category... Thanks! Dan I need some help with the query for a form that selects values across multiple columns, and allow users to select multiple values in several columns. http://brinsterinc.com/tpa/tpasearch.php I assume you build the where clause for the sql by determining if there is an option value has been selected in the single-value select boxes. But how do I handle the multi-select list boxes? I'm desperate for help! Need to get somewhere with this over the weekend! TIA! I'm having trouble getting the dynamic data from my <select> menus to write into my MYSQL database. Can anyone see what I'm doing wrong here? First post btw The output looks like this, which is obviously wrong: Code: [Select] <html> <head> </head> <link rel="stylesheet" type="text/css" href="./css/newuser.css" /> <body> <?php session_start(); require 'default.inc.php'; ?> <?php if (isset($_POST['amount'])): $host = 'localhost'; $user = 'user'; $pass = 'password'; $conn = mysql_connect($host, $user, $pass); if (!$conn) { exit('<p>Unable to connect to the database server</p>'); } if (!@mysql_select_db('spikesusers')) { exit('<p>Unable to locate the database</p>'); } $locationname = $_POST['donor']; $donorid = mysql_query("SELECT id FROM donors WHERE locationname='$locationname'"); $amount = $_POST['amount']; $year = $_POST['year']; $type = $_POST['type']; $typeid = mysql_query("SELECT id FROM donationtype WHERE type='$type'"); $player = $_POST['player']; //$playerid = mysql_query("SELECT id FROM players WHERE $sql = "INSERT INTO donations SET donorid='$donorid', amount='$amount', yearofdonation='$year', typeid='$typeid'"; mysql_query($sql); ?> <div class='standard'> <h1>Donation Management</h1> <?php if ($sql) { echo "New donation added "; echo "<p></p>"; echo "<a href=managedonations.php>Back to donation management</a>"; exit(); } else { echo "Error adding new donation"; echo "<a href=adddonation.php>Try again</a>"; exit(); } ?></div> <?php else: $host = 'localhost'; $user = 'user'; $pass = 'pass'; $conn = mysql_connect($host, $user, $pass); if (!$conn) { exit('<p>Unable to connect to the database server</p>'); } if (!@mysql_select_db('spikesusers')) { exit('<p>Unable to locate the database</p>'); } $donor=@mysql_query('SELECT id, locationname FROM donors'); ?> <div class='standard'> <h1>Donation Management</h1> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <label>Donor: <select class="text" name="donor"> <option value=new>Add a new donor...</option> <?php while ($donors=mysql_fetch_array($donor)) { $donorname=$donors['locationname']; echo "<option value='<?php echo $donorname;?>'>$donorname</option>"; //echo "<option value='hi'>hi</option>"; } ?> </option> </select> </label><br /> <label>Amount: <input class="text" type="text" name="amount" class="text" /></label><br /> <label>Year of donation: <select class="text" name="year"> <option value='2011'>2011</option> <option value='2010'>2010</option> <option value='2009'>2009</option> </select> </label><br /> <?php $player=@mysql_query('SELECT id, firstname, lastname FROM players'); ?> <label>Player: <select class="text" name="player"> <option value="player" selected="selected"></option> <?php while ($players=mysql_fetch_array($player)) { $playerfirstname=$players['firstname']; $playerlastname=$players['lastname']; echo "<option value=player>$playerfirstname $playerlastname</option>"; } ?> </select> </label><br /> <?php $type=@mysql_query('SELECT id, type FROM donationtype'); ?> <label>Donation type: <select class="text" name="type"> <?php while ($types=mysql_fetch_array($type)) { $donationtype=$types['type']; echo "<option value=type>$donationtype</option>"; } ?> </select> </label><br /> <input type="submit" value="SUBMIT" class="buttons"/> <input type="button" name="Cancel" value="CANCEL" onclick="window.location = 'managedonations.php'" class="buttons"/> </form> </div> <?php endif; ?> </body> </html> Hello, I had 8 select boxes(dropdowns) in a form. Its like a search kind of implementation in the website. I don't know what the user selects, he may choose different select boxes, any number of select boxes. So, based upon the user selection, I need to generate the data. The data fields to be generated would be almost same. So, How would I know what the user selection is and how many select boxes has been selected and how could I generate different data based upon selected boxes. I had to make a small note abt this, that the data generation fields may change for some of the user select combinations, but most of the result fields would be same. so, can you please help me out, how to do, how to make different combination data generations, because, i had 8 fields, i had to make 8! combinations, that would result in a big code. I want to create 2 select menus - one static (Menu 1) and the other (Menu 2) populated according to the selection made in Menu 1. Data will will be pulled from a CSV file. Menu 2 will not have any values in it until an option from Menu 1 has been selected. Then, the options available will be limited based on the Item 1 selection. PHP must be used to read the CSV file and to capture the data (using arrays) I met this problem before and my approach then was to call a function using the onChange event for Menu1 ... <script type = "text/javascript">function update_select(obj){ window.location.href="./call_stats_main.php?choice=" + obj.value;} </script> onchange="update_select(this) This would reload the page passing a parameter to the URL (?choice=) I could then use $_GET['choice'] to capture the selected value and dynamically populate Menu 2 according to this value. It is the onchange event that triggers the population of menu 2. I would like to know IF THERE IS A BETTER WAY, other than using ajax ? Steven M Hi, sorry for being a newbie, but I need help setting up a dynamic form. The form will be a reservation type form. I have a single table, which I will post below. What I am looking to do is choose a month from a select box and then have it populate another select box with the available dates. Here is my table info from mysql... Code: [Select] CREATE TABLE `daterange` ( `RID` int(5) NOT NULL auto_increment, `DEND` date NOT NULL, `MONTH` varchar(50) NOT NULL, `DATE` varchar(50) NOT NULL, `SITE` varchar(50) NOT NULL, `PRICE` varchar(10) NOT NULL, `STATUS` varchar(1) NOT NULL default 'A', `FNAME` varchar(50) NOT NULL, `LNAME` varchar(50) NOT NULL, `ADDR1` varchar(50) NOT NULL, `ADDR2` varchar(50) NOT NULL, `CITY` varchar(50) NOT NULL, `STATE` varchar(2) NOT NULL, `ZIP` varchar(5) NOT NULL, `PHONE1` varchar(3) NOT NULL, `PHONE2` varchar(3) NOT NULL, `PHONE3` varchar(4) NOT NULL, `EMAIL` varchar(50) NOT NULL, PRIMARY KEY (`RID`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=83 ; Here is a sample mysql dump... Code: [Select] INSERT INTO `daterange` VALUES(1, '2011-05-15', 'May 2011', '5/15-5/22/11', 'Carterville Pond/Adirondack', '$2625.00', 'N', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''); INSERT INTO `daterange` VALUES(2, '2011-05-22', 'May 2011', '5/22-5/29/11', 'Carterville Pond/Adirondack', '$2625.00', 'A', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''); INSERT INTO `daterange` VALUES(3, '2011-05-29', 'May 2011', '5/29-6/5/11', 'Carterville Pond/Adirondack', '$2625.00', 'A', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''); Finally, here is a php form that I started to develop but it only has a single select box, which works but I wanted another select box that will filter the list down a little... Code: [Select] <? // script to display all the Available Reservations in the daterange table // connection information $hostName = "my_host"; $userName = "my_user"; $password = "my_pass"; $dbName = "my_db"; // make connection to database mysql_connect($hostName, $userName, $password) or die("Unable to connect to host $hostName"); mysql_select_db($dbName) or die("Unable to select database $dbName"); // Select all the fields in all the records of the daterange table $query = "SELECT * FROM daterange WHERE DEND > DATE(NOW()) AND STATUS='A' ORDER BY RID, DATE, SITE"; $result = mysql_query($query); // Determine the number of reservation dates $number = mysql_numrows($result); // Create drop-down menu of reservation dates print "<font size=\"3\" face=\"Arial\"><b>Select Reservation Date:</b> <form action=\"test.php\" method=\"post\"> <select name=\"RID\"> <option value=\"\">Choose One</option>"; for ($i=0; $i<$number; $i++) { $RID = mysql_result($result,$i,"RID"); $DATE = mysql_result($result,$i,"DATE"); $SITE = mysql_result($result,$i, "SITE"); $PRICE = mysql_result($result,$i, "PRICE"); print "<option value=\"$RID\">$DATE, $SITE, $PRICE</option>"; } print "</select><p align=left><label><font size=\"3\" face=\"Arial\">First Name: <input type=\"text\" name=\"FNAME\" size=\"50\" maxlength=\"50\" tabindex=\"1\"<br>"; print "<p align=left><label>Last Name: <input type=\"text\" name=\"LNAME\" size=\"50\" maxlength=\"50\" tabindex=\"2\"<br>"; print "<p align=left><label>Address Line 1: <input type=\"text\" name=\"ADDR1\" size=\"50\" maxlength=\"50\" tabindex=\"3\"<br>"; print "<p align=left><label>Address Line 2: <input type=\"text\" name=\"ADDR2\" size=\"50\" maxlength=\"50\" tabindex=\"4\"<br>"; print "<p align=left><label>City: <input type=\"text\" name=\"CITY\" size=\"50\" maxlength=\"50\" tabindex=\"5\"<br>"; print "<p align=left><label>State (abbrev.): <input type=\"text\" name=\"STATE\" size=\"2\" maxlength=\"2\" tabindex=\"6\"<br>"; print "<p align=left><label>Zip Code: <input type=\"text\" name=\"ZIP\" size=\"5\" maxlength=\"5\" tabindex=\"7\"<br>"; print "<p align=left><label>Contact Phone Number: (<input type=\"text\" name=\"PHONE1\" size=\"3\" maxlength=\"3\" tabindex=\"8\""; print "<label>)<input type=\"text\" name=\"PHONE2\" size=\"3\" maxlength=\"3\" tabindex=\"9\""; print "<label>-<input type=\"text\" name=\"PHONE3\" size=\"4\" maxlength=\"4\" tabindex=\"10\"<br>"; print "<p align=left><label>Email: <input type=\"text\" name=\"EMAIL\" size=\"50\" maxlength=\"50\" tabindex=\"11\"<br>"; print "<p align=left><label>Payment Method: <select name=\"PM\"> <option value=\"\">Choose One</option> <option value=\"Visa\">Visa</option> <option value=\"Mastercard\">Mastercard</option> <option value=\"Discover\">Discover</option>"; print "</select><p align=left><label>Credit Card Number: <input type=\"text\" name=\"C1\" size=\"4\" maxlength=\"4\" tabindex=\"12\""; print "<label> <input type=\"text\" name=\"C2\" size=\"4\" maxlength=\"4\" tabindex=\"13\""; print "<label> <input type=\"text\" name=\"C3\" size=\"4\" maxlength=\"4\" tabindex=\"14\""; print "<label> <input type=\"text\" name=\"C4\" size=\"4\" maxlength=\"4\" tabindex=\"15\"<br>"; print "<p align=left><label>Expiration Month: <select name=\"EXM\"> <option value=\"\">Choose One</option> <option value=\"January\">January</option> <option value=\"February\">February</option> <option value=\"March\">March</option> <option value=\"April\">April</option> <option value=\"May\">May</option> <option value=\"June\">June</option> <option value=\"July\">July</option> <option value=\"August\">August</option> <option value=\"September\">September</option> <option value=\"October\">October</option> <option value=\"November\">November</option> <option value=\"December\">December</option>"; print "</select><p align=left><label> Expiration Year: <select name=\"EXY\"> <option value=\"\">Choose One</option> <option value=\"2011\">2011</option> <option value=\"2012\">2012</option> <option value=\"2013\">2013</option> <option value=\"2014\">2014</option> <option value=\"2015\">2015</option> <option value=\"2016\">2016</option> <option value=\"2017\">2017</option> <option value=\"2018\">2018</option> <option value=\"2019\">2019</option> <option value=\"2020\">2020</option>"; print "</select><p align=left><label>Security Code (3 or 4 digits): <input type=\"text\" name=\"CSC\" size=\"4\" maxlength=\"4\" tabindex=\"16\"<br>"; print "<p align=left><input type=\"submit\" value=\"Book Now!\" name=\"submit\"></form>"; print " <input type=\"reset\" value=\"reset\" name=\"reset\"></form>"; // Close the database connection mysql_close(); ?> Sorry this is so long, any help would be appreciated...... -Bob Hi guys, I have an entry site with six fields, (country, town, school, language, course type, starting date) all of them take their content from the database. (Just those countries will be shown, if someone registered from there) However, if a visitor chooses e.g. country USA, and then schools, he will still be shown ALL schools, no matter if they are in the USA or not. I need to change that! How can I make the fields dependent from each other. So if a visitor chooses a country, that just the schools, the towns, the courses, etc.. will be shown in the other drop down menus from that particular country. Thanks! Here is my code: Code: [Select] <style type="text/css"> .search_panel { width: 250px; margin: 10px 0; } .search_panel input { width: 220px; border: 2px solid #000643; padding: 5px; } .search_panel select { width: 230px; border: 2px solid #000643; padding: 5px; } </style> <script> $(function() { $( "#course_start_date" ).datepicker(); }); </script> <div id="home"> <div style="width: 500px; height:260px; padding: 40px 50px; margin: 0 auto; background-image: url(<?=base_url()?>images/front_blue.png); background-repeat: no-repeat;"> <form method="post" action="<?=site_url('search')?>"> <div class="search_panel" style="float: left;"> <select name="country" style=""> <option value="">Choose a country</option> <?php if($countries): foreach($countries as $country): ?> <option value="<?=$country->school_country?>"><?=$country->school_country?></option> <?php endforeach; endif; ?> </select> </div> <div class="search_panel" style="float: left;"> <select name="city"> <option value="">Choose a city</option> <?php if($cities): foreach($cities as $city): ?> <option value="<?=$city->school_city?>"><?=$city->school_city?></option> <?php endforeach; endif; ?> </select> </div> <br clear="all"/> <div class="search_panel" style="float: left;"> <input type="text" name="starting_date" id="course_start_date" value="Choose a starting date" onclick="this.value='';"/> </div> <div class="search_panel" style="float: left;"> <select name="course_type"> <option value="">Course type</option> <?php if($course_types): foreach($course_types as $course_type): ?> <option value="<?=$course_type->course_type?>"><?=$course_type->course_type?></option> <?php endforeach; endif; ?> </select> </div> <br clear="all"/> <div class="search_panel" style="float: left;"> <select name="school_name"> <option value="">Search by school name</option> <?php if($schools): foreach($schools as $school): ?> <option value="<?=$school->school_name?>"><?=$school->school_name?></option> <?php endforeach; endif; ?> </select> </div> <div class="search_panel" style="float: left;"> <select name="language"> <option value="">Search by language</option> <?php if($languages): foreach($languages as $language): ?> <option value="<?=$language?>"><?=$language?></option> <?php endforeach; endif; ?> </select> </div> <br clear="all"/> <center> <p><br /> <input type="image" src="<?=base_url()?>images/search.png"/> </p> </center> </form> </div> </div> How to create Dependent Dropdown List?
<div class="form-group"> <label class="control-label col-sm-2" for="i_have">I have :</label> <div class="col-sm-10"> <select id="old" class="form-control" placeholder="Select I have" name="i_have"> <option value = "select_option">Select Option</option> <option value = "three_compact">3 Compact</option> <option value = "three_regular">3 Regular</option> <option value = "three_triple">3 Triple</option> <option value = "five_compact">5 Compact</option> <option value = "five_regular">5 Regular</option> <option value = "five_triple">5 Triple</option> <option value = "seven_compact">7 Compact</option> <option value = "seven_regular">7 Regular</option> <option value = "seven_triple">7 Triple</option> <option value = "nine_compact">9 Compact</option> <option value = "nine_regular">9 Regular</option> <option value = "nine_triple">9 Triple</option> </select> </div> </div> <div class="form-group"> <label class="control-label col-sm-2" for="i_want">I want :</label> <div class="col-sm-10"> <select id="new" class="form-control" placeholder="Select I want" name="i_want"> <option value = "select_option">Select Option</option> <option value = "three_compact">3 Compact</option> <option value = "three_regular">3 Regular</option> <option value = "three_triple">3 Triple</option> <option value = "five_compact">5 Compact</option> <option value = "five_regular">5 Regular</option> <option value = "five_triple">5 Triple</option> <option value = "seven_compact">7 Compact</option> <option value = "seven_regular">7 Regular</option> <option value = "seven_triple">7 Triple</option> <option value = "nine_compact">9 Compact</option> <option value = "nine_regular">9 Regular</option> <option value = "nine_triple">9 Triple</option> </select> </div> </div>
If the first dropdown "3 Compact " is selected second dropdown should not be able to select "3 Compact" & if first dropdown "5 Regular" is selected second dropdown should not be able to select below values of 5 Regular like "3 Compact, 3 Regular, 3 Triple, 5 Compact. The logic is, if first dropdown value selects like 5, second dropdown value must select above 5 not 4 or 3 or 2 or 1. Hi, I have looked everywhere for a way to do this and would be grateful for a pointer in the right direction. I want to change the background color of the div with the id of "loggedin", depending on the status. The status currently echos text depending on the value. Code: [Select] <div class="grid_12" id="loggedin"> <div> <ul id="loggedin"> <li><?php global $current_user; get_currentuserinfo(); echo 'Status : ' . $current_user->status . "\n";?></li> <li> <?php if ($current_user->status=="RED") echo "Red info"; ?> <?php if ($current_user->status=="GREEN") echo "Green info"; ?> <?php if ($current_user->status=="BLUE") echo "Blue info"; ?> <?php if ($current_user->status=="YELLOW") echo "Yellow info"; ?> </li> </div> </div> Thanks Hi, I wonder if I somebody can help me I have three drop down lists which are populated from data thats stored in my sql database. I also have a table which is echoed from the database all on the same page. What i want to do is allow users to select values in the drop down lists which will then echo different results below in the table. Is this possible. So on page load it will show all of the info in the html table. Then when the user changes the drop downs and clicks submit it will change the query to something SELECT dropdown1 value FROM users where Dropdown2 = 1 AND Dropdown3 = 2 etc. Is this possible to display this information on the same page using the dropdown list or does it need to load a different page as the client side is trying to edit a server side script? If its possible please can someone give me an example. Thanks in advance Edd Folks, I need help (Php code ) to generate a Dynamic Text on a Base Image. What i want to do is, to make this Image as header on my Site and to make this Header Specific to a Site, i want to Add the Domain Name on the Lower Left of the Image. Got the Idea? Here is the Image link: Quote http://img27.imageshack.us/i/shoppingheader1.jpg/ PHP Variable that holds the Domain name is: $domain All i need the Dynamic PHP Codes that i can put on all my sites to generate this Text on Image (Header) Dynamically... May Anyone Help me with this Please? Cheers Natasha T. |