Third drop down menu does not populate from database - php

I have got the following Index.php:
<script language="javascript" type="text/javascript">
function getXMLHTTP() { //function to return the xml http object
var xmlhttp=false;
try{
xmlhttp=new XMLHttpRequest();
}
catch(e) {
try{
xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e){
try{
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e1){
xmlhttp=false;
}
}
}
return xmlhttp;
}
function getColor(CategoryId) {
var strURL="getColor.php?Category="+CategoryId;
var req = getXMLHTTP();
if (req) {
req.onreadystatechange = function() {
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200) {
document.getElementById('qcolor').innerHTML=req.responseText;
} else {
alert("There was a problem while using XMLHTTP:\n" + req.statusText);
}
}
}
req.open("GET", strURL, true);
req.send(null);
}
}
function getBrand(CategoryId,ColorId) {
var strURL="getBrand.php?Category="+CategoryId+"&Color="+ColorId;
var req = getXMLHTTP();
if (req) {
req.onreadystatechange = function() {
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200) {
document.getElementById('qbrand').innerHTML=req.responseText;
} else {
alert("There was a problem while using XMLHTTP:\n" + req.statusText);
}
}
}
req.open("GET", strURL, true);
req.send(null);
}
}
</script>
</head>
<body>
<div id="Quick_find_2">
<div id="Quick_find_container">
<form action="search2.php" method="get">
<div id="qcategory_1">Product</div>
<div id="qcategory">
<select name="Category" class="dropmenu" id="Category" onChange="getColor(this.value)">
<option value="">Any</option>
<option value="Keyboard">Keyboard</option>
<option value="Piano">Piano</option>
</select>
</div>
<div id="qcolor_1">Colour</div>
<div id="qcolor"><select name="Color" id="Color" class="dropmenu">
<option value="">Select Color</option>
</select>
</div>
<div id="qbrand_1">Brand</div>
<div id="qbrand"><select name="Manufacturer" class="dropmenu">
<option value="">Any</option> </select>
</div>
Please note the form is not ready yet it will need a submit button in the end, however for now I just want to populate the drop down menus
The first two menus work fine I'm having trouble populating the last third one.
The getColor.php contains the following code:
$Category= $_GET['Category'];
mysql_select_db($database_dconn, $dconn);
$query="SELECT DISTINCT Color FROM products WHERE products.Category LIKE '%$Category%' AND Category!= 'Stage Pianos' AND Category!= 'Recent Pianos' AND Category!= 'Recent Keyboards' AND hidden ='no' ORDER BY Color";
$result=mysql_query($query);
?>
<select name="Color" onchange="getBrand(<?=$Category?>,this.value)">
<option value="">Select Color</option>
<? while($row=mysql_fetch_array($result)) { ?>
<option value=<?=$row['Color']?>><?=$row['Color']?></option>
<? } ?>
</select>
The getBrand.php contains this code:
$Category=$_GET['Category'];
$Color=$_GET['Color'];
mysql_select_db($database_dconn, $dconn);
$query="SELECT DISTINCT Manufacturer FROM products WHERE products.Category LIKE '%$Category%' AND Color = '$Color' AND Category!= 'Stage Pianos' AND Category!= 'Recent Pianos' AND Category!= 'Recent Keyboards' AND hidden ='no' ORDER BY Manufacturer";
$result=mysql_query($query);
?>
<select name="Manufacturer">
<option value="">Select Brand</option>
<? while($row=mysql_fetch_array($result)) { ?>
<option value="<?=$row['Manufacturer']?>"><?=$row['Manufacturer']?></option>
<? } ?>
</select>
Please don't worry about SQL injection as I will sort that.
Somehow the values for the $Category and Color are not being passed trough can anybody see where I'm going wrong? Any help welcome

Related

I am getting Notice: Undefined index: select_dept when using ajax call

i want to display doctors name corresponding to the speciality so i have used ajax
when speciality is selected corresponding doctors name will also display
i am getting error like this and doctors list is not showing up
Notice: Undefined index: select_dept in C:\wamp\www\xx\get_specialist.php
on line 2
Call Stack
# Time Memory Function Location
1 0.0012 140616 {main}( ) ..\get_specialist.php:0
my form code
<select name="select_dept" id="select_dept"
class="textBox" style="width:180px" onChange="getSpecialist(this.value)">
<option value="">Select Department</option>
<?php
include("db_conexn.php");
$res=mysql_query("SELECT * FROM imh_departments");
while($rowCat2 = mysql_fetch_array($res)){?>
<option value="<?php echo $rowCat2['depart_id']?>">
<?php echo $rowCat2['department_name'];?></option>
<?php }?>
</select>
<div id="statediv">
<select name="doct" >
<option>Select Doctor</option>
</select>
</div>
My ajax code
<script language="javascript" type="text/javascript">
function getXMLHTTP() { //function to return the xml http object
var xmlhttp=false;
try{
xmlhttp=new XMLHttpRequest();
}
catch(e) {
try{
xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e){
try{
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e1){
xmlhttp=false;
}
}
}
return xmlhttp;
}
function getSpecialist(countryId)
{
var strURL="get_specialist.php?country="+countryId;
var req = getXMLHTTP();
if (req){
req.onreadystatechange = function() {
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200)
{
document.getElementById('statediv').innerHTML=req.responseText;
} else {
alert("Problem while using XMLHTTP:\n" + req.statusText);
}
}
}
req.open("GET", strURL, true);
req.send(null);
}
}
get_specialist.php code
<?php
$country=$_REQUEST['select_dept'];
include("db_conexn.php");
$query="SELECT doct_id,doct_name FROM imh_doctors WHERE depart_id='$country'";
$result=mysql_query($query);
?>
<select name="doct" >
<option>Select Doctor</option>
<?php while ($row=mysql_fetch_array($result)){?>
<option value=<?php echo $row['doct_id'];?>><?php echo $row['doct_name'];?></option>
<?php } ?>
</select>
your
$country=$_REQUEST['select_dept'];
should be
if(isset($_REQUEST['country'])) {
$country=$_REQUEST['country'];
// rest of db code
}
Please note that your code is open to SQL injection and should use mysqli and mysqli_real_escape_string instead.
You are assigning $country to select_dept and you may want to change that.
$country = $_REQUEST['country'];

how can i link two drop down menu box where i can select one menu and it should show my list in other drop down box?

i want to link one drop down box menu and as i select one menu then it should show a list according to the selected menu using php,i used if else but its not working
<?php>
if(value="tata")
{
<option value="vista">vista</option>
<option value="nano">nano</option>
<option value="aria">aria</option>
<option value="manza">manza</option>
}
elseif(value="fiat")
{
<option value="linea">linea</option>
<option value="punto">punto</option>
}
elseif(value="maruti")
{
<option value="swift">swift</option>
<option value="desire">desire</option>
<option value="omni">omni</option>
<option value="maruti 800">maruti 800</option>
}
elseif(value="hundai")
{
<option value="santro">santro</option>
<option value="verna">verna</option>
}
?>
If you want dynamic list using php you shoud use ajax.
Try this Example:
use this ajax script
$(document).ready(function() {
$('#dropdown').change( function() {
$('#myform').submit();
$.ajax({
data: $(this).serialize(),
type: $(this).attr('method'),
url: $(this).attr('action'),
success: function(response) {
$('#output').html(response);
}
});
return false;
});
});
<form id=myform method=POST action="process.php">
<select id="dropdown" name="dropdown">
<option value="tata">TATA</option>
<option value="fiat">FIAT</option>
<option value="maruti">MARUTI</option>
<option value="hundai">HUNDAI</option>
</select>
</form>
<div id="output"></div>
process.php
<?php
$value = $_POST['dropdown'];
$html = "<select name = 'cars' id='cars'>";
if ($value == 'tata') {
$html .= "<option value='vista'>vista</option><option value='nano'>nano</option><option value='aria'>aria</option><option value='manza'>manza</option>";
} elseif($value == 'fiat') {
$html .= "<option value='linea'>linea</option><option value='punto'>punto</option>";
} elseif($value == 'maruti') {
$html .= "<option value='swift'>swift</option><option value='desire'>desire</option><option value='omni'>omni</option><option value='maruti 800'>maruti 800</option>";
} elseif($value == 'hundai') {
$html .= "<option value='santro'>santro</option><option value='verna'>verna</option>";
}
$html .= "</select>";
echo $html;
exit;
PHP, being a server side language will require the page to be refreshed (the form posted) before the value of the first selected drop down is available.
To do this without a page refresh, you will need to do so on the client side, using JavaScript. If the list is generated via PHP then you should look into AJAX.
//make array like this on header.php
$list = array(
'tata' => array(
'vista', 'nano', 'aria'
),
'fiat' => array(
'linea', 'punto'
),
'maruti' => array(
'swift', 'desire'
)
};
// your car.php where you going to do you action..call header.php here
<select name="Manuf" id="Manuf" onchange="show_car(this.value)">
<option value="">/option>
<?php
foreach($list as $x => $x_value)
{
echo '<option value="'.$x.'" >'.$x.'</option>';
}
?>
</select>
<select name="car" id="car"></select>
// Ajax Function For display car
function show_car(manuf)
{
var xmlhttp;
if (manuf.length==0)
{
alert("Select manufacturer");
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("car").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","ajax.php?manuf="+manuf,true);
xmlhttp.send();
}
// code on ajax.php ..call header.php here too
foreach($list as $x => $x_value)
{
if($x == $_GET['manuf'])
{
for($i=0; $i<sizeof($x_value);$i++)
{
echo '<option value="'.$x_value[$i].'" >'.$x_value[$i].'</option>';
}
}
}

listbox not being populated in IE works in FF and Chrome

Hello I am working in php and javascript and have code where a admin can select a name in a dropdown box and it pulls up the locations that name is associated with these are sales guys. Well it works fine in FF and Chrome but in IE the list box just goes blank. hopefully someone can help me out bosses here use IE
Brent
DROPDOWN and LIST code
<label for="firstname"><?php echo ADD_EDIT_SALESREP;?><span class="required">*</span></label>
<select name="sales_rep" id="sales_rep" onChange="findLocation2(this.value)">
<option value="">Select</option>
<?php
$sqlQry1 = "SELECT * FROM ".TABLE_PREFIX."_employee WHERE status='t' AND is_Deleted='N' ORDER BY employee_Id";
$sql_Show1 = $DBObject->db_query($sqlQry1);
while($catArr = $DBObject->db_fetch_array($sql_Show1)){
?>
<div align="center"><br />
<option value="<?php echo $catArr['employee_Id'] ?>"><?php echo SafeOutput($catArr['first_Name']) ?> <?php echo SafeOutput($catArr['last_Name']) ?></option>
</div>
<?php
}
?>
</div>
</div>
</div>
<br />
<p>
</select>
<div id="salesrloc">
<select name="salesr_loc" id="salesr_loc" title="Sales Rep Loc" size="5" multiple="multiple">
<option value="">Select</option>
</select>
<br />
Javascript
<script language="javascript">
dv = document.createElement('div'); // create dynamically div tag
dv.setAttribute('id',"details");
function findLocation2(category)
{
dv.innerHTML='<div id="salesrloc" style="width:auto; height:200px;"><img src="image/loading.gif"></div> <br>';
var url11 = "salesrep2.php";
var qry11="?sales_rep=" + category ;
var result1='salesr_loc';
var ajaxRequest; // The variable that makes Ajax possible!
try{
ajaxRequest = new XMLHttpRequest();
} catch (e){
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
alert("Your browser broke!");
return false;
}
}
}
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){ //alert(ajaxRequest.responseText);
var a=ajaxRequest.responseText;
try{
document.getElementById('salesr_loc').innerHTML=ajaxRequest.responseText;
}catch(e){
dv.innerHTML=ajaxRequest.responseText;
}
}
}
ajaxRequest.open("POST", url11+qry11, true);
ajaxRequest.send(null);
} </script>
and Finally populate listbox php
<?php
include("mastersecure.php");
$emp_id = $_GET['sales_rep'];
$qry= "select * FROM ".TABLE_PREFIX."_location ls, ".TABLE_PREFIX."_customer cs WHERE loc_Salesrep = ".$emp_id." AND ls.customer_Id = cs.customer_Id";
$res = $DBObject->db_query($qry);
?>
<body onLoad="document.getElementById('salesr_loc').focus();">
<select name="salesr_loc[]" id="salesr_loc" title="Sales Rep Loc" size="5" multiple="multiple">
<?php
while($row=$DBObject->db_fetch_array())
{
?>
<option value="<?=$row["location_Id"]?>"><?=$row["customer_Name"]?>, <?=$row["location_Name"]?></option>
<?php
}
?>
</select>
</body>
the IE does not support .innerHTML for < select> elements!!
<select name="salesr_loc" id="salesr_loc"
...
document.getElementById('salesr_loc').innerHTML=ajaxRequest.responseText;
instead, you must do it in a loop:
var d = document.getElementById('salesr_loc');
for(...) {
d.options[x] = new Option(..);
}

Separate ID numbers in a column separated by a ,

I need a bit of help separating id's that are separated by a comma and then displaying them in a dropdown list.
Here's the code below that i am working with, if the clothing_type_id has only 1 number in it, it works fine, if i add more numbers so it's split by a comma i.e 1,2,3,4 it will no longer work, i'm sure i need to use explode but i can not get it to work. Any help will be appreciated.
<? $clothing=intval($_GET['clothing_var']);
include 'classes.php';
mysql_select_db('database');
$query="SELECT ID,Qty_Values FROM Quantity WHERE Clothing_Type_ID='$clothing'";
$result=mysql_query($query);
$test=explode(',', '$result');
?>
<select name="state" onchange="getCity(<?=$clothing?>,this.value)">
<option>Select State</option>
<? while($row=mysql_fetch_array($test)) { ?>
<option value=<?=$row['ID']?>><?=$row['Qty_Values']?></option>
<? } ?>
</select>
UPDATE:
Here's what i'm trying todo.
Hi, pasted your code in and although it works, it only brings up the first selection, if i click on the 2nd id nothing displays. My database table goes like this, the fist selection called Clothing_Type has 3 columns, ID Garment and Price, the second table has 4 columns, ID, Clothing_Type_ID, Qty_Values and price. Basically I am trying to do is make a pricing guide so if i select a t-shirt in the first selection box it will bring up 4 prices at the end of the form, 1 for each item, 2 for a total amount (If selecting a higher quantity) 3 for VAT and 4 Total Amount. Every time selection is selected then it will automatically update the price. After i've selected 3 or 4 selection boxes, i then want a few checkboxes again to update the price if they're ticked.
Update 2: Here's the js and html
JS
<script language="javascript" type="text/javascript">
function getXMLHTTP() {
var xmlhttp=false;
try{
xmlhttp=new XMLHttpRequest();
}
catch(e) {
try{
xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e){
try{
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e1){
xmlhttp=false;
}
}
}
return xmlhttp;
}
function getQty(countryId) {
var strURL="findQty.php?clothing_var="+countryId;
var req = getXMLHTTP();
if (req) {
req.onreadystatechange = function() {
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200) {
document.getElementById('qtydiv').innerHTML=req.responseText;
} else {
alert("There was a problem while using XMLHTTP:\n" + req.statusText);
}
}
}
req.open("GET", strURL, true);
req.send(null);
}
}
function getCity(countryId,stateId) {
var strURL="findCity.php?clothing_var="+countryId+"&qty_var="+stateId;
var req = getXMLHTTP();
if (req) {
req.onreadystatechange = function() {
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200) {
document.getElementById('citydiv').innerHTML=req.responseText;
} else {
alert("There was a problem while using XMLHTTP:\n" + req.statusText);
}
}
}
req.open("GET", strURL, true);
req.send(null);
}
}
</script>
HTML
<body>
<form method="post" action="" name="form1">
<?
include 'classes.php';
$query="SELECT ID,Garment FROM Clothing_Type";
$result=mysql_query($query);
?>
<select name="clothing_type" onChange="getQty(this.value)">
<option>Select Clothing Type</option>
<? while($row=mysql_fetch_array($result)) { ?>
<option value=<?=$row['ID']?>><?=$row['Garment']?></option>
<? } ?>
</select>
<p id="qtydiv">
<select style="width: 150px;" name="qty" disabled='disabled'>
<option>Select Country First</option>
</select>
</p>
<p id="citydiv">
<select style="width: 150px;" name="city" disabled='disabled'>
<option>Select State First</option>
</select>
</p>
<input type="checkbox" name="vehicle" value="Bike" /> I want to live here<br />
<form>
<input type='checkbox' name='1' value='1'id='checkbox_1' />1<br>
<input type='checkbox' name='2' value='2'id='checkbox_2' />2<br>
<input type='checkbox' name='3' value='3'id='checkbox_3' />3<br>
</form>
</body>
Try this,
$clothing = "1,2,3,4";
$query="SELECT ID,Qty_Values FROM Quantity WHERE Clothing_Type_ID IN ($clothing)";
I think you should use the IN() operator instead of just comparing to a comma-separated string:
$query = "SELECT ID, Qty_Values FROM Quantity WHERE Clothing_Type_ID IN($clothing)";
Update your SQL to this..
$query="SELECT ID,Qty_Values FROM Quantity WHERE Clothing_Type_ID in ($clothing)";
Get rid of the explode. This will return a result set of the clothes that have a clothing type Id that is listed in your $clothing list.
Part of me thinks you are doing this backwards. I'm assuming each item in the DB only has 1 clothing type ID and the list of ids is in your $clothing string.
If this isn't the case give me an example look at your DB table.

PHP - Php script overwriting original interface in html

I created 3 drop down list in my html. For the first drop down, once user selects an option, a function is called on onchange. This runs a php script on the server and then updates the second drop down list. However, After it updates the second drop down list, the third drop down list disappears. Is there anything wrong with my code? If so, how should I change it to?
.html
<script type="text/javascript">
function showApplications(str)
{
var xmlhttp;
if (str=="")
{
document.getElementById("appname").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("appname").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getApplications.php",true);
xmlhttp.send(null);
}
</script>
<script type="text/javascript">
function showTargets(str)
{
var xmlhttp;
if (str=="")
{
document.getElementById("target").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("target").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getTargets.php",true);
xmlhttp.send(null);
}
</script>
</head>
<body>
<form action="post">
Environment:
<select name="customers" onchange="showApplications(this.value)">
<option value="Environment">Select an environment:</option>
<option value="SandBox">Sandbox</option>
<option value="Production">Production</option>
</select>
</br>
</br>
Application Name: <div id="appname">
<select name="customers" onchange="showTargets(this.value)">
<option value="">Select application:</option>
</select>
Target: <div id="target">
<select name="select">
<option>Select one option</option>
</select>
</div>
</form>
</br>
</body>
</html>
.php
<?
$link = mysql_connect("127.0.0.1:3306", "root", "");
if(!$link){
die('Could not connect: ' . mysql_error());
}
mysql_select_db("PushApplication");
$query="SELECT AppName FROM Applications";
$result=mysql_query($query);
?>
<select name="state" onchange="showTargets(this.value)">
<option>Select application</option>
<? while($row=mysql_fetch_array($result))
{
?>
<option value=<?=$row['AppName']?>><?=$row['AppName']?></option>
<?
}
?>
</select>
You're missing a close tag for your element <div id="appname">. Close it up after your select element and your script should work just fine.

Categories