Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Feb 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    PHP MySQL Ajax it works but have a problem

    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

    Code:
    <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

    Code:
    <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

  • #2
    New to the CF scene
    Join Date
    Apr 2008
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'd say your main problem is you're creating multiple functions with the same names.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •