• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 315
  • Last Modified:

ajax example

Hi

i am just new in ajax ,and i want to practice a bit ,then i found a good example in the internet,i modified a bit of the script i changed the database ..

but really i cannot make work ..
when i run index.php i have a scroll down employee then when choose an employee i must see his informations it is like index.php calls ajax.php

please find attached two scripts..

Regards

A.S
index.php.txt
ajax.php.txt
0
asaidi
Asked:
asaidi
  • 2
1 Solution
 
EZFragCommented:
Hi there,

I had a look at your ajax.php file, and I noticed that you are not opening a connection to the database like in your index.php file.

<?php
    mysql_connect('localhost:3306','root','hhp');
    mysql_select_db('timesheet');
?>

Also, the current javascript you are using will only be able to work in Internet Explorer, since that specific code is not supported by Fire Fox and some other browsers.

If the connection to the database is made in ajax.php, and you are using IE as your browser, I can't find anything that would suggest why it would not work.
****** Suggested ajax.php contents ******
 
<?php
if (is_numeric($_GET['employ_id'])) {
 
    mysql_connect('localhost:3306','root','hhp');
    mysql_select_db('timesheet');
    
    $query="select * from employee where id=$_GET[employ_id]";
    $result=mysql_query($query);
    $employ=mysql_fetch_array($result);
 
echo "<table border=\"1\">
        <tr>
            <td>Name:</td>
            <td>".$employ[name]."</td>
        </tr>
        <tr>
            <td>Surname:</td>
            <td>".$employ[surname]."</td>
        </tr>
        <tr>
            <td>Price1:</td>
            <td>".$employ[price1]."</td>
        </tr>
        <tr>
            <td>price2:</td>
            <td>".$employ[price2]."</td>
        </tr>
    </table>";
} 
?>

Open in new window

0
 
asaidiAuthor Commented:
Hi

thank you for your answer but still it does not work

in my index.php
i have this what i understand that it calls ajax.php
 var url="ajax.php";
    url=url+"?employ_id="+id;
i am trying both browsers but no data is displayes when i select a name ..
you can try it ..
http://83.71.142.153/ajax/index.php
0
 
EZFragCommented:
Ah, thanks for the URL.

Found the problem. In the "index.php" file. You are calling a Javascript function ("GetXmlHttpObject()") that is supposed to return a XMLHttpRequest object. Thing is, that function does not exist in your Javascript code.

I have added a modified version of index.php, with the missing function included. Update the changes, and see if it is working. If not, just let me know and I'll take another look.

Hope it helps ^_^
<html>
<head>
<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 xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    
    return self.xmlHttpReq;
}
 
</script>
</head>
<body>
<select onchange="display_data(this.value);">
    <option>Select employee</option>
<?php
    mysql_connect('localhost:3306','root','hhp');
    mysql_select_db('timesheet');
    $query="select id, name ,surname from employee order by name asc";
    $result=mysql_query($query);
    while(list($id, $name ,$surname)=mysql_fetch_row($result)) {
        echo "<option value=\"".$id."\">".$name." ".$surname."</option>";
    }
?>
</select>
<div id="employ_data">   
<?php
if (is_numeric($_GET['employ_id'])) {
    $query="select * from employee where id=$_GET[employ_id]";
    $result=mysql_query($query);
    $employ=mysql_fetch_array($result);
 
echo "<table border=\"1\">
        <tr>
            <td>Name:</td>
            <td>".$employ[name]."</td>
        </tr>
        <tr>
            <td>Surname:</td>
            <td>".$employ[surname]."</td>
        </tr>
        <tr>
            <td>Price1:</td>
            <td>".$employ[price1]."</td>
        </tr>
        <tr>
            <td>price2:</td>
            <td>".$employ[price2]."</td>
        </tr>
    </table>";
} 
?>
 
</div>
</body>
</html>

Open in new window

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now