...

View Full Version : PHP MySQL Ajax it works but have a problem



chrispos
02-12-2011, 11:34 PM
I am using ajax with php works fine when I use one query but not the second

Here is the script that works but when I add the second script I add it using include with php it posts the same id

What i am trying to do is add both scripts to the same page one with Job sector showing IT SALES then that runs the query and I get the results but when I try to add region that shows but select town from region it takes the id from the first query. I know it is a problem with the id but I have no idea. I do more PHP so it is a bit new to me.

First script that works fine


<script language="JavaScript" type="text/javascript">
function display_data(id) {
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null) {
alert ("Your browser does not support AJAX!");
return;
}
var url="ajax.php";
url=url+"?employ_id="+id;
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 || xmlhttp.readyState=="complete") {
document.getElementById('employ_data').innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}
function GetXmlHttpObject() {
var xmlhttp=null;
try {
// Firefox, Opera 8.0+, Safari
xmlhttp=new XMLHttpRequest();
}
catch (e) {
// Internet Explorer
try {
xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlhttp;
}
</script>
</head>

<body>
<table width="400" cellspacing="0" cellpadding="0">
<tr>
<td><select onchange="display_data(this.value);">
<option>Select Sector</option>
<?php
include 'config.php';
$query = "SELECT * FROM `employ`";
$result = mysql_query($query) or die (mysql_error());
if (mysql_num_rows($result)>0)
{
echo'<select name = "employ">';
while ($row = mysql_fetch_array($result))
{
$id=$row[0];
$depart=$row[1];
echo "<option value=\"$id\">$depart</option>\n";
}
echo'</select>';
}
?>
</select>
<div id="employ_data"><div>
</td>
</tr>
<tr>
<td><?php
echo'tabel';
?>

The ajax.php page

<?php
$id=$_GET[employ_id];
echo"<br>". 'Please Select Your Sector Below'."<br><br>";
include 'config.php';
$query = "SELECT * FROM `sector`WHERE `id` = '$id' ";
$result = mysql_query($query) or die (mysql_error());
if (mysql_num_rows($result)>0)
{
echo'<select name = "depart">';
while ($row = mysql_fetch_array($result))
{
$sid=$row[0];
$id=$row[1];
$sector=$row[2];
echo "<option value=\"$id\">$sector</option>\n";
}
echo'</select>';
}
?>

When i Try to add this i get the problem


<script language="JavaScript" type="text/javascript">
function display_data(id) {
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null) {
alert ("Your browser does not support AJAX!");
return;
}
var url="ajax1.php";
url=url+"?region_rid="+id;
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 || xmlhttp.readyState=="complete") {
document.getElementById('region_data').innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}
function GetXmlHttpObject() {
var xmlhttp=null;
try {
// Firefox, Opera 8.0+, Safari
xmlhttp=new XMLHttpRequest();
}
catch (e) {
// Internet Explorer
try {
xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlhttp;
}
</script>
</head>

<body>
<select onchange="display_data(this.value);">
<option>Select Region</option>
<?php
include 'config.php';
$query = "SELECT * FROM `region`";
$result = mysql_query($query) or die (mysql_error());
if (mysql_num_rows($result)>0)
{
echo'<select name = "region">';
while ($row = mysql_fetch_array($result))
{
$rid=$row[0];
$region=$row[1];
echo "<option value=\"$rid\">$region</option>\n";
}
echo'</select>';
}
?>
</select>
<div id="region_data"><div>
</body>

This is the ajax1.php page

<?php
$rid=$_GET[region_rid];
echo"<br>". 'Please Select Your Town / City Below'."<br><br>";
include 'config.php';
$query = "SELECT * FROM `town`WHERE `rid` = '$rid' ";
$result = mysql_query($query) or die (mysql_error());
if (mysql_num_rows($result)>0)
{
echo'<select name = "depart">';
while ($row = mysql_fetch_array($result))
{
$tid=$row[0];
$rid=$row[1];
$town=$row[2];
echo "<option value=\"$rid\">$town</option>\n";
}
echo'</select>';
}
?>

I make this 4 pages and include the second page with ajax1.php

any help would be great many thanks

mrgstiffler
02-22-2011, 03:49 AM
I'd say your main problem is you're creating multiple functions with the same names.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum