Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 984
  • Last Modified:

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.
0
AndreMarques
Asked:
AndreMarques
  • 5
  • 2
1 Solution
 
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
 
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now