PHP - Dynamic Dependent Select Boxes With Php & Mysql
Dear 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. Similar TutorialsBrand 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> 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 Hey, I'm new to this stuff. But I'm calling a Table with a list of products, I want people to be able to compare the products they checked. what is the best way of doing this with the code below? I put "blah" for everything except for the compare table and calls. This code seemed to work the best for the look I wanted for it. They all call the checkbox from 'Compare' in the mysql table. Thanks! $result = mysql_query("SELECT * from Compare_Tool ORDER BY Blah ASC"); //Table starting tag and header cells echo "<table border='1'><tr><th>Compare</th><th>blah</th><th>blah</th><th>blah</th><th>blah</th><th>blah</th><th>blah</th><th>blah</th><th>blah</th><th>blah</th><th>blah</th><th>blah</th></tr>"; while($row = mysql_fetch_array($result)){ ?> <tr> <td align="center"><input name="checkbox[]" type="checkbox" Compare="checkbox[]" value="<? echo $row['Compare']; ?>"></td> <td ><? echo $row['blah']; ?></td> <td ><? echo $row['blah']; ?></td> <td ><? echo $row['blah']; ?></td> <td ><? echo $row['blah']; ?></td> <td ><? echo $row['blah']; ?></td> <td ><? echo $row['blah']; ?></td> <td ><? echo $row['blah']; ?></td> <td ><? echo $row['blah]; ?></td> <td ><? echo $row['blah]; ?></td> <td ><? echo $row['blah]; ?></td> <td ><? echo $row['blah]; ?></td> </tr> <?php } echo "</table>"; ?> im developing a voting system where you can vote for multiple choices using checkboxes.. upon submitting the vote form, votecounts must automatically be updated.. values for votecount will come from the checkbox value 1. ive been having a hard time trying to figure out hot to update multiple rows with checkbox values..any help would be appreciated..thanks in advance! here's the code: Code: [Select] $editFormAction = $_SERVER['PHP_SELF']; if (isset($_SERVER['QUERY_STRING'])) { $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']); } if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form_vote")) $i=1;{ while ($i<= $totalRows_rs_candi) { $updateSQL = "UPDATE tbl_candidates SET votecount={$_POST['votecount']} WHERE user_id={$_POST['candi']}"; mysql_select_db($database_organizazone_db, $organizazone_db); $Result1 = mysql_query($updateSQL, $organizazone_db) or die(mysql_error()); $i++; } if (isset($_SERVER['QUERY_STRING'])) { $updateGoTo = "vote_success.php"; } header(sprintf("Location: %s", $updateGoTo)); } Code: [Select] <form id="form_vote" name="form_vote" method="POST" action="<?php echo $editFormAction; ?>"> <table border="1"> <tr> <td> </td> <td>user_id</td> <td>l_name</td> <td>course_id</td> <td>yearlevel</td> <td>about</td> </tr> <?php $i=1; do { ?> <tr> <td><span id="sprycheckbox1"> <input name="votecount[<?php echo $i ?>]" type="checkbox" id="votecount" value="1" /> <span class="checkboxMinSelectionsMsg">Minimum number of selections not met.</span></span> <label for="votecount[]"></label></td> <td><label for="candi"></label> <input name="candi" type="text" disabled="disabled" id="candi" value="<?php echo $row_rs_candi['user_id']; ?>" readonly="readonly" /></td> <td><?php echo $row_rs_candi['l_name'].' ,'.$row_rs_candi['f_name'].' '.$row_rs_candi['m_name']; ?></td> <td><?php echo $row_rs_candi['course_id']; ?></td> <td><?php echo $row_rs_candi['yearlevel']; ?></td> <td><?php echo $row_rs_candi['about_me']; ?></td> </tr> <?php $i++; ?> <?php } while ($row_rs_candi = mysql_fetch_assoc($rs_candi)); ?> </table> I have a simple (1 table, 14 fields) database. I'm using 9 separate search boxes from my search page : http://www.cinemaposter.com/database.html There are currently 162 entries. Hitting "submit" throws them all out, same with valid input into any box, with the exception of "country" - example : entering "F" gives 20results; "PL" gives 34 results. Here's my code (to simplify, I only went down to the 4th box ('country' - the last and only one giving correct results). I'm one sorry newbie.... please help ! Here's the code : ------------------ <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/1998/REC-html40-19980424/loose.dtd"> <html> <head> <title>Search Results</title> <meta name="generator" content="BBEdit 6.5"> <style type="text/css"> td { color: #000000; font-family: arial; font-size: 10px; } </style> </head> <body> <?php $link = mysql_connect("xxxx", "xxxx", "xxxx") or die("Could not connect"); ///print "Connected successfully<BR>"; mysql_select_db("xxxx") or die("Could not select database"); { $Author = $_POST['Author']; $sql = "SELECT * FROM posters WHERE Author LIKE '%$Author%'"; } { $Title = $_POST['Title']; $sql = "SELECT * FROM posters WHERE Title LIKE '%$Title%'"; } { $Originaltitle =$_POST['Originalitle']; $sql="SELECT * FROM posters WHERE Originaltitle LIKE '%$Originaltitle%'"; } { $Country =$_POST['Country']; $sql="SELECT * FROM posters WHERE Country LIKE '%$Country%'"; } $result = mysql_query ($sql) or die("Query failed : " . mysql_error()); $numrs = mysql_num_rows($result); if ($numrs > 0){ if ($numrs==1){ print ("<p><font face=\"Arial\"><b>There is $numrs record in our database.</B> <BR><BR>"); }elseif($numrs>1){ print ("<p><font face=\"Arial\"><b>There are $numrs records in our database. </B><BR><BR>");//Apparently, he wan't to be able to search again...</b><form method \"POST\" action=\"$PHP_SELF\">Search Again:"); } echo("<a href='javascript:history.go(-1)'>Go Back</a></p>"); print("<P align=center>Click on the column heading links to sort the results.<br><br />"); print ("<TABLE CELLPADDING=\"1\" CELLSPACING=\"2\" BORDER=\"0\">\n"); PRINT ("<TR BGCOLOR=\"#FFFF77\"> <TD COLSPAN=2><P align=center>Author</td> <TD><P align=center>Title</TD> <TD><P align=center>Original Title</TD> <TD><P align=center>Country</TD> <TD><P align=center>Director</TD> <TD><P align=center>Cast</TD> <TD><P align=center>Year</TD> <TD><P align=center>Original Year</TD> <TD><P align=center>Photo</TD> <TD><P align=center>Producer</TD> <TD><P align=center>Circulation</TD> <TD><P align=center>Size</TD> <TD><P align=center>Print type</TD> <TD><P align=center>Remarks</TD></TR>"); $z=1; while ($line = mysql_fetch_array($result)) { $num=$z++; //Note here that I'm assigning the found objects new names (indicated by lowercase) to that php wont get confused..... $auth=$line['Author']; $title=$line['Title']; $originaltitle=$line['Originaltitle']; $country=$line['Country']; $director=$line['Director']; $cast=$line['Cast']; $year=$line['Year']; $originalyear=$line['Originalyear']; $producer=$line['Producer']; $circ=$line['Circulation']; $size=$line['Size']; $print=$line['Print']; $remarks=$line['Remarks']; $pict=$line['Picture']; //Set the formatting $bgcolor = ($i++ & 1) ? '#d3d3d3' : '#c0c0c0'; PRINT("<TR BGCOLOR=$bgcolor> <TD>$num.</TD> <TD>$auth</td> <TD>$title</TD> <TD>$originaltitle</TD> <TD><P align=center>$country</TD> <TD>$director</TD> <TD>$cast</TD> <TD><P align=center>$year</TD> <TD><P align=center>$originalyear</TD><TD>"); // This is the test to find do we have a picture if ($pict) { // If there is a picture, display it: print ("<P align=center><a href='zdjecie.php?zdj= $pict&autor=$auth&opis=$title&orig=$originaltitle&rok=$year' target='_blank'> <img src=$pict height=\"150\"></a>"); } else { // We don't have a picture, so we print some text instead: print ("<P align=center>no picture available"); } print ("</TD><TD>$producer</TD> <TD><P align=center>$circ</TD> <TD>$size</TD> <TD>$print</TD> <TD>$remarks</TD></TR>"); } PRINT("</TABLE><BR><font face=\"Arial\"><a href=\"javascript:history.go(-1);\">Search Again</a></font>"); }else { print ("<font face=\"Arial\">Sorry, there were no results for your search. <a href=\"javascript:history.go(-1);\">Please try again</a></font>"); } //mysql_close($link); ?> </body> </html> Hey! I have it so users can submit info to my site. They have to check some check boxes and radio buttons. I want them to be able to edit this information, so I want to have the data pulled from the MySQL database and have the correct radio button selected (and the text bolded). So I'm thinking some sort of array to check for each value in the database... but I don't really know. There are a few groups of radio boxes and check boxes but here is an example of one. Code: [Select] <input type="radio" name="stage" value="Stage 1" /> Stage 1 <input type="radio" name="stage" value="Stage 2" /> Stage 2 <input type="radio" name="stage" value="Stage 3" /> Stage 3 The information is pulled from the database like this: Code: [Select] $res=mysql_query("SELECT * FROM ACTIVE WHERE INDEX_ID=$id"); if(mysql_num_rows($res)==0) echo "There is no data in the table <br /> <br />"; else { for($i=0;$i<mysql_num_rows($res);$i++) { $row=mysql_fetch_assoc($res); } } So when I pull information (lets say I want to echo it) it'd look like this: Code: [Select] $row[STAGE] The value for the stage selection can only be Stage 1, Stage 2 or Stage 3 so I need it to be selected when the page loads. Thanks! So I'm trying to put a user selected date into a mysql query, but it seems like no matter what I do I get 0 results even though I already know there's quite a few. The way I have it now 'echo $query;' puts out the expected results, but I'm guessing it's because I'm trying to match a date to a string. I tried strtotime but I'm not sure if I was doing it right.. I also tried sprintf() just in case, but that doesn't seem to have a date feature. The variable $realdate is just to show how I was attempting to use strtotime, it's not an actualy part of the script at this time... $searchdate = $_GET['searchdate']; //$realdate = strtotime($searchdate) //connects to db dbconnect(); $query="SELECT `name`, `date`, SUM(`amount`) as `total_money`, SUM(`bricks`) as `total_bricks` FROM Aces_Donations WHERE `date`= $searchdate ORDER BY `total_money`"; echo $query; $result=mysql_query($query); //Opens the table echo "<table border = '3' width = 40% bgcolor=#A85045><caption><center>Rankings - ".$searchdate.'</center></caption>'; //Loop to run until eof while($row = mysql_fetch_array($result)){ echo "<tr><td>".$rank."</td> <td>".$row['name']. "</td><td>". $row['total_money']."</td><td>".$row['total_bricks'].'</td></tr>'; $rank++; } echo '</table>'; |