Update variable without page refresh?

Hi there,

i have a page in php, that had a iframe that load's a page in perl. I have a mysql db that have one field call page that could 0 or 1.

If its 0 show the perl page if its 1 show a html page.

How to check this variable in the php page without refreshing the page or the iframe?

Best regards.
AndreMarquesAsked:
Who is Participating?
 
Bruce SmithSoftware Engineer IICommented:
The contents of your ajax.php file should look like this:
 
<?php

$dbVar = null;
mysql_connect('db_server', 'db_user', 'db_pw') OR DIE('Unable to connect to database! Please try again later.');
mysql_select_db('db_name');
$sql = "SELECT perl_or_html FROM table";
$result = mysql_query($sql) or exit("QUERY FAILED!" . mysql_error());
if ($result):
    while ($row = mysql_fetch_array($result)):
        $o = $row['perl_or_html'];
    endwhile;
endif;
echo($dbVar);
?>

Open in new window


The following page checks the db every 10 seconds and stores the variable from the database into a JavaScript variable called 'dbVar' and then shows an alert box of the variable:
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>EE :: AJAX Example</title>
        <script type="text/javascript">
            var dbVar = null;
            setInterval("doAjax()", 10000);
        </script>
    </head>
    <body>
        <h2>An alert box with the variable from the database should pop-up every 10 seconds.</h2>
        <script type="text/javascript">
            function doAjax() {
                var url = "ajax.php";
                var xmlhttp;
                if (window.XMLHttpRequest)
                {
                    // code for IE7+, Firefox, Chrome, Opera, Safari
                    xmlhttp=new XMLHttpRequest();
                }
                else if (window.ActiveXObject)
                {
                    // code for IE6, IE5
                    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
                }
                else
                {
                    alert("Your browser does not support XMLHTTP!");
                }
                xmlhttp.onreadystatechange=function() {
                    if(xmlhttp.readyState==4)
                    {
                        dbVar = xmlhttp.responseText;
                        alert(dbVar);
                    }
                }
                xmlhttp.open("GET", url, true);
                xmlhttp.send(null);
            }
        </script>
    </body>
</html>

Open in new window


Instead of showing the alert box, you'll want to do an IF statement to show the html or perl page.

Hope this is what you need...
0
 
Bruce SmithSoftware Engineer IICommented:
Via AJAX. You want to have a separate php script (not page) that connects to the database and queries for that value and call it ajax.php. Then echo the value at the end of the script. Now in your main php page where you want to check w/o reloading, add this JavaScript at the end of your page just inside the </body> tag:
function doAjax() {
                            var url = "ajax.php";
                            var xmlhttp;
                            if (window.XMLHttpRequest)
                            {
                                // code for IE7+, Firefox, Chrome, Opera, Safari
                                xmlhttp=new XMLHttpRequest();
                            }
                            else if (window.ActiveXObject)
                            {
                                // code for IE6, IE5
                                xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
                            }
                            else
                            {
                                alert("Your browser does not support XMLHTTP!");
                            }
                            xmlhttp.onreadystatechange=function() {
                                if(xmlhttp.readyState==4)
                                {
                                   var val = xmlhttp.responseText;
                                   // val is your variable from the database
                                }
                            }
                            xmlhttp.open("GET", url, true);
                            xmlhttp.send(null);
                        }

Open in new window


On line 21, the variable var is the value from your database. You can return that there and then call the function anywhere in your php page to retrieve that value whenever you want. No page reloading or anything   :)
0
 
Bruce SmithSoftware Engineer IICommented:
Here is a really good reference for what you are doing here: http://www.w3schools.com/PHP/php_ajax_database.asp

HTH
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
AndreMarquesAuthor Commented:
Ok,

but that script in ajax, ill do the refresh in how many minutes? what i have to do in the ajax.php? just check the variable?

0
 
Bruce SmithSoftware Engineer IICommented:
Yeah - when do you want to check? When the user clicks a button - or every so often or what?
0
 
AndreMarquesAuthor Commented:
every 5 seconds per example.
0
 
Bruce SmithSoftware Engineer IICommented:
I'm away from my desk at the moment, but look into the setInterval function for JavaScript. It looks something like "setInterval('doAjax()', 5000);". But I'll post some example code when I get back to my desk. Cheers
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.