Click here to Skip to main content
15,899,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi
when I change data in Mysql with another machine,I want change data in page without refreshing,For example I put "1" and I want show immediately without refresh ! I think can do with Ajax, any Idea?? please help me. thank you !
Posted
Comments
Sergey Alexandrovich Kryukov 26-Nov-11 20:36pm    
You are right, you will need AJAX. Now, what's the problem?
--SA
Mohammad Ali_7 26-Nov-11 21:32pm    
I want sample , I don't know how can I use Ajax in PHP !
Mohammad Ali_7 26-Nov-11 22:04pm    
when I update Mysql, show data in page without in refresh or click on any Button!

1 solution

Hi Mohammad Ali
Maybe this sample is useful for you:

HTML
{// 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("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>

<form>
<select name="users" onchange="showUser(this.value)">
<option value="">Select a person:</option>
<option value="1">Peter Griffin</option> 
<option value="2">Lois Griffin</option>
<option value="3">Glenn Quagmire</option>
<option value="4">Joseph Swanson</option>
</select>
</form>
<br />
<div id="txtHint">Person info will be listed here.</div>
</body>
</html> 


SECOND FILE NAME IS getuser.php

PHP
$q=$_GET["q"];
$con = mysql_connect('localhost', 'peter', 'abc123');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("ajax_demo", $con);
$sql="SELECT * FROM user WHERE id = '".$q."'";
$result = mysql_query($sql);
echo "<table border="1">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "<td>" . $row['Age'] . "</td>";
echo "<td>" . $row['Hometown'] . "</td>";
echo "<td>" . $row['Job'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>  


this is example of listbox when u select list box related information is display using Ajax. Just u have to create database name is ajax_demo.
You can extend this sample for your code.
 
Share this answer
 
Comments
Mohammad Ali_7 26-Nov-11 23:26pm    
Salam, Omid aziz, thank you for this code, It's useful, I have one page just show some Image, and I don't have any button or other action in page, I want use Ajax when change data in Mysql changing picture in my page immediately without refresh! any Idea? I just show data from mysql without refresh and without click on any Button ! thank you for helping.
omid.nazifi 26-Nov-11 23:58pm    
Hi, Mohammad jan
I have a suggest and hope useful for you.
you can define function (with JavaScript/JQuery not different) that call it every time (for example once every 10 seconds). function call a php file when change data in mysql, return a value(for instance picture's new address) then you can change your picture.
Mohammad Ali_7 26-Nov-11 23:54pm    
Thank you Omid with a little changing I can do it. Thank YOUUUUUUUUUUU
omid.nazifi 27-Nov-11 0:01am    
Your welcome
Good luck
RaisKazi 27-Nov-11 0:23am    
Perfect Answer. 5ed.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900