Hi,

I am trying to use AJAX to do a simple thing of displaying the results in the same page. Clicking on the <a href tag should display the results in the same page. This is working correctly in IE but the onclick() function is not working in firefox and safari. Below is the code, any help appreciated.

index.php

<?php
include "dbconnect.php";
?>
<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="ajax2.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>

<?php
$query="SELECT AGE, experiment_name XENOID FROM Donor_Experiment limit 10";
    $result=mysql_query($query);
    while(list($AGE, $XENOID)=mysql_fetch_row($result)) {
		echo '<a href="#" value="'.$XENOID.'" onclick="display_data(this.value)">'.$XENOID.'</a>';
	}
?>

<div id="employ_data"><div>
</body>
</html>

ajax2.php

<?php
include "dbconnect.php";
    
    $query="select DONOR, SEX, DIAG, SITE from Experiment where experiment_name = '$_GET[employ_id]'";
    $result=mysql_query($query);
    $employ=mysql_fetch_array($result);
    echo "<table border=\"1\">
        <tr>
            <td>Name:</td>
            <td>".$employ[DONOR]."</td>
        </tr>
        <tr>
            <td>Appoint Date:</td>
            <td>".$employ[SEX]."</td>
        </tr>
        <tr>
            <td>Country:</td>
            <td>".$employ[DIAG]."</td>
        </tr>
        <tr>
            <td>City:</td>
            <td>".$employ[SITE]."</td>
        </tr>
        
    </table>";

?>

Sanchow,

Can only think that Moz browsers aren't reading the value attribute in the <a> tags.

Suggest you try this:

//-- replace
//echo '<a href="#" value="'.$XENOID.'" onclick="display_data(this.value)">'.$XENOID.'</a>';
//-- with
echo "<a href=\"\" onclick=\"display_data('$XENOID');return false\">$XENOID</a>";

May be enough to make it work.

Airshow

commented: Thank you very much...it worked perfectly. +0

Another thing.

In ajax2.php you need to sanitize $_GET[employ_id] , otherwise your database is open to a code injection attack.

If your ids are integers then use intval($_GET[employ_id], 10) (but note that intval() returns zero on failure). Othersise, devise a regular expression to check that $_GET[employ_id] is of the right format.

Airshow

Thank you very much...it worked perfectly in ie, firefox and safari.

Hi pals,
I am also in onclick problem, my code works fine in FF,OPERA,Chrome but not in Safari.
My problem is this onclick.

<?php if(has_access($userarray,'view_serverdet','server')){
	if (('Y' == $webpanel_ajax) && ('N' == $server_details || $usergroupid == "1")) { ?>
		<span class="expand" id="exp<?php echo $server_row['id']?>" onclick="view_details('<?php echo $server_row['id'];?>','page','<?php echo $shade;?>')">+</span>
<?php }
} ?>

The corresponding function like:

function view_details(id,from,class) {
  	 alert("id is:"+id);
  	 alert("from is:"+from);
  	 alert("class:"+class);
          //some other codes
}

In other Browsers these values alert, but in Safari not. Why Pls Give me a solution
ASAP.

Thankfully
Anes P.A

This should be a new topic.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.