hey guys, i on working on a php/sql/ajax script which is like in short, will give me all users registered at site, and when i click on their name, it will give it their rest of details..
so at first i would explain u bit of stuff,
1) i have made a global config and settins file which got almost all of my important stuff including database connectivity. "common.php"
2) one is html file which has javascript embeded in it, and another php file for querry handling..
3) the script works in three steps
i) ONLOAD > it request php files to recieve list of users from database.
then it will load it in the file
ii) onclick > after clicking on user buttons it will send second querry to the php file for rest of details.
iii) displaying them 🙂
now please note 2 things, 1) I AM NEWBIE IN PHP, 2) as this script wasnt working i added few useless codes just so see where it was giving error, so u will find loads of useless (printr, print_f and echo in the php file).
HTML FILE
<html>
<head>
<script language="javascript" type="text/javascript">
// Get the HTTP Object
function getHTTPObject()
{
if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");
else if (window.XMLHttpRequest) return new XMLHttpRequest();
else
{
alert("Your browser does not support AJAX.");
return null;
}
}
// Implement business logic
function doWork()
{
httpObject = getHTTPObject();
if (httpObject != null)
{
httpObject.open("GET", "ajax.php?user="
+document.getElementById('userdetails').value, true);
httpObject.send(null);
httpObject.onreadystatechange = setOutput;
}
}
var httpObject = null;
// Change the value of the outputText field
function setOutput()
{
if(httpObject.readyState == 4)
{
document.getElementById('details').innerHTML = httpObject.responseText;
}
}
// Implement business logic
function getname()
{
httpObject = getHTTPObject();
if (httpObject != null)
{
httpObject.open("GET", "ajax.php?user="
+document.getElementById('username').value, true);
httpObject.send(null);
httpObject.onreadystatechange = setOutput1;
}
}
var httpObject = null;
// Change the value of the outputText field
function setOutput1()
{
if(httpObject.readyState == 4)
{
document.getElementById('usernames').innerHTML = httpObject.responseText;
}
}
</script>
</head>
<body onLoad="getname()" id="username">
<div id="usernames">
<b></b>
</div>
<p><br></p>
<div id="details">
<b>Click on username and info will be displayed here</b>
</div>
</body>
</html>
</html>
PHP FILE
<?php
//including connection File
include_once("../common.php");
$user_get_id = $_GET['user'];
echo (" userid $user_get_id <br> ");
if ($user_get_id=='undefined')
{
//getting user name from database
$sql = "SELECT username from users";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result, MYSQL_BOTH))
{
printf ('<input type="button" value="%s" onMouseUp="doWork()" id="userdetails"> <br> ', $row[0], $row[1]);
echo ("<br>");
print_r($row);
echo ("<br>");
}
/* Now we free up the result and continue on with our script */
mysql_free_result($result);
}
echo ("<br>");
echo ("<br>");
echo ("<br>");
echo ("<br>");
echo ($user_get_id);
?>
plaese help me in this.
please and thank you