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'];
Related
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
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(..);
}
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.
I got a simple AJAX demo code from internet which fills one select listbox dynamically with values from database. It contains a html file with AJAX code embedded in it and a PHP file.
The problem is this code works fine in IE, Chrome, FireFox4 but its not works in FireFox3. Please any one explain this and tell me the solution. The code is as follows for reference
The database schema for city table is as follows
id tinyint(4) primary key not null
city varchar(50)
countryid tinyint(4)
HTML File
<html>
<head>
<script type="text/javascript">
function getCity(strURL)
{
alert("inside getCity function");
//var req = getXMLHTTP(); // function to get xmlhttp object
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
//xmlhttp=new XMLHttpRequest();
req=new XMLHttpRequest();
}
else
{// code for IE6, IE5
//xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
req=new ActiveXObject("Microsoft.XMLHTTP");
}
if (req)
{
req.onreadystatechange = function()
{
if (req.readyState == 4) { //data is retrieved from server
if (req.status == 200) { // which reprents ok status
document.getElementById('citydiv').innerHTML=req.responseText;
}
else
{
alert("There was a problem while using XMLHTTP:\n");
}
}
}
//alert(srtURL);
var sURL="findcity.php?country="+strURL;
req.open("GET", sURL, true); //open url using get method
req.send();
}
}
</script>
</head>
<body>
<form method="post" action="" name="form1">
Country : <select name="country" onChange="getCity(this.value)">
<option value="">Select Country</option>
<option value="1">india</option>
<option value="2">usa</option>
</select>
<br />City : <div id="citydiv">
<select name="select">
<option>Select City</option>
</select>
</div>
</form>
</body>
</html>
PHP file
<?
echo $_GET['country'];
echo "<br>";
$country=intval($_GET['country']);
echo $country;
echo "<br>";
$link = mysql_connect('localhost', 'root', 'mark'); //change the configuration if required
if (!$link) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('querytest'); //change this if required
$query="select city from city where countryid=$country";
echo "<br>";
echo $query;
echo "<br>";
$result=mysql_query($query);?>
<select name="city">
<option>Select City</option>
<? while($row=mysql_fetch_array($result)) { ?>
<option value><?=$row['city']?></option>
<? } ?>
</select>
Please guide me friends to make this code working in FireFox 3
Change
req.send();
to
req.send(null);
<script type="text/javascript">
function showUser(str) {
if (str == "") {
document.getElementById("txtHint").innerHTML = "";
return;
}
// code for IE7+, Firefox, Chrome, Opera, Safari
if (window.XMLHttpRequest) {
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("txtHint").innerHTML = xmlhttp.responseText;
//document.myForm.text1.value=.innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET", "getuser.php?q=" + str, true);
xmlhttp.send();
}
</script>
</head>
<body>
<form name="myForm">
<p>Selec the Menu :
<select name="users" onchange="showUser(this.value)">
<option value="">Select ID</option>
<?php do { ?>
<option value="<?php echo $row_rscustomer['customer_number']?>"><?php echo $row_rscustomer['customer_number']?></option>
<?php } while ($row_rscustomer = mysql_fetch_assoc($rscustomer));
$rows = mysql_num_rows($rscustomer);
if ($rows > 0) {
mysql_data_seek($rscustomer, 0);
$row_rscustomer = mysql_fetch_assoc($rscustomer);
} ?>
</select>
</p>
<p> </p>
<p>
<input name="text1" type="text"
value="<?php include (" getuser.php");?>"/>
<p>
</form>
<div id="txtHint"></div>
Hey - I noticed that, inside your AJAX onreadystatechange function, you have added xmlhttp.status==200 to your conditional. In some browsers, as AJAX is a client-side language, this isn't supported. If the member status isn't supported by the user's browser, then its default value will either be undefined or 0, either way the value will never be 200, thus the block inside that conditional won't be executed.
I suggest removing the xmlhttp.status==200 condition, and just relying on xmlhttp.readyState. I had this problem when I first started with AJAX.