Reputation: 83
I created a php page that get data from a table from Database then html page that get the php file and refresh it using AJAX, But I need to put the php code in html file and refresh the code, this is my code that refresh a div tag from an external file :
<script>
function Ajax(){
var xmlHttp;
try{
xmlHttp=new XMLHttpRequest();
}
catch (e){
try{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e){
try{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e){
alert("Oops!");
return false;
}
}
}
xmlHttp.onreadystatechange=function(){
if(xmlHttp.readyState==4){
document.getElementById('ReloadThis').innerHTML=xmlHttp.responseText;
setTimeout('Ajax()',2000);
}
}
xmlHttp.open("GET","see.php",true);
xmlHttp.send(null);
}
window.onload=function(){
setTimeout('Ajax()',2000);
}
</script>
<div id="ReloadThis"></div>
but I want to refresh a specific PHP code in the same page for example:
<script>
function Ajax(){
var xmlHttp;
try{
xmlHttp=new XMLHttpRequest();
}
catch (e){
try{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e){
try{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e){
alert("Oops!");
return false;
}
}
}
xmlHttp.onreadystatechange=function(){
if(xmlHttp.readyState==4){
document.getElementById('ReloadThis').innerHTML=xmlHttp.responseText;
setTimeout('Ajax()',2000);
}
}
xmlHttp.open("GET","see.php",true);
xmlHttp.send(null);
}
window.onload=function(){
setTimeout('Ajax()',2000);
}
</script>
<?php
$txt = $_POST['text'];
$sql = mysql_query("SELECT * FROM `table`WHERE `id` = '$txt'");
while($fetch = mysql_fetch_array($sql)){
$names = $fetch['name'];
echo $names;
echo "</br>";
}
?>
How can I refresh the php code while it is in the same page?
Upvotes: 0
Views: 1243
Reputation: 5778
Move the PHP code to a separate file and call it using AJAX. The PHP code can respond with a list of names. The AJAX callback function should handle repopulating the portion of the page that needs updated.
Your code, as it stands, is open to a SQL injection attack. Please see How can I prevent SQL injection in PHP? Furthermore, mysql_* functions have been deprecated, so it is recommended that you migrate to mysqli functions or PDO.
Example PHP code (untested):
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$txt = $mysqli->real_escape_string($_POST['text']);
$mysqli->query("SELECT * FROM `table` WHERE `id` = '$txt'");
$names = array();
while($fetch = mysqli->fetch_array($sql)) {
$names[] = $fetch['name'];
}
echo header('Content-type: text/plain');
echo implode(',', $names);
This returns a simple comma separated list of names.
Example AJAX callback routine (untested):
xmlHttp.onreadystatechange=function(){
if(xmlHttp.readyState==4){
var resp = xmlHttp.responseText;
var names = resp.split(",");
var htmlString = "";
for (var i = 0; i < names.length; i++) {
htmlString += names[i] + "<br/>";
}
document.getElementById('ReloadThis').innerHTML=htmlString;
setTimeout('Ajax()',2000);
}
}
DO NOT COPY & PASTE! Please note, that I haven’t used raw Javascript for AJAX calls since the invention of jQuery so my code may be less than ideal or down right incorrect. Nevertheless, it should give you the general idea. I would also use JSON instead of a comma separated list.
I hope this answers your question. If I misunderstood something in your question, please let me know.
Upvotes: 0
Reputation: 7795
The PHP code is executed by the server. It never arrives to the browser, so there is no way to refresh it. You would have to reexecute the PHP on the server side and reload the page. If you can't accomplish what you want with JavaScript, a page reload is your only option.
Upvotes: 1