Solved

Update variable without page refresh?

Posted on 2011-03-14
7
973 Views
Last Modified: 2013-12-13
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
Comment
Question by:AndreMarques
  • 5
  • 2
7 Comments
 
LVL 11

Expert Comment

by:Bruce Smith
ID: 35130891
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
 
LVL 11

Expert Comment

by:Bruce Smith
ID: 35130934
Here is a really good reference for what you are doing here: http://www.w3schools.com/PHP/php_ajax_database.asp

HTH
0
 

Author Comment

by:AndreMarques
ID: 35133060
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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 11

Expert Comment

by:Bruce Smith
ID: 35133122
Yeah - when do you want to check? When the user clicks a button - or every so often or what?
0
 

Author Comment

by:AndreMarques
ID: 35133681
every 5 seconds per example.
0
 
LVL 11

Expert Comment

by:Bruce Smith
ID: 35133930
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
 
LVL 11

Accepted Solution

by:
Bruce Smith earned 500 total points
ID: 35143743
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Find out what you should include to make the best professional email signature for your organization.
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question