Sunday, 8 September 2013

PHP and Ajax a onclick function

PHP and Ajax a onclick function

I have a menu, I want when select item from menu, the content displays,
but I can't call ajax function. When I click in item from menu, in address
line was appeared only id of selected item:
http://localhost:8080/treeview/#2
HTML code from a browser:
<a onclick="callPage(1)"; href="#1 "> <span
style="color:yellow";>item1</span></a>
The PHP code, where I form html code:
$out .= '<li id="'.$item['id'].'"> <label for="subfolder2">';
$out .= '<a onclick="callPage('.$item['id'].')"; href="#'.$item['id'].'
"> <span
style="color:'.$item['color'].'";>'.$item['title'].'</span></a>';
$out .= ' </label> <input type="checkbox" id="subfolder2"/>';
AJAX function:
// JavaScript Document
function callPage(id)
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
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("pageContent").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","content.php?id="+id,true);
xmlhttp.send();
}
A Content file, I was tested, he work well, when I take him in address bar
in browser:
<?php function __autoload($class){
include_once($class.".php");
}
$id=$_REQUEST['id'];
$db = new oopCrud();
$rty = $db->getById($id);
echo $rty['title'];
echo $rty['color'];
?>

No comments:

Post a Comment