I have a php file which connects to a MySql db and read the last entry from a specific table. What I am trying to do, is to display(echo) the last entry from the table using a JavaScript popup box into the external html file Below I have the code for the PHP file (which is working fine) and the html one but unfortunately I can't figure out how to pass the PHP variable to JavaScript function.

Many thanks in advance.

The php file would be this one:

    <?php

    // 1. Create a database connection
    $connection = mysql_connect("localhost","root","password"); 
    if (!$connection) {
        die("Database connection failed: " . mysql_error());
    }

    // 2. Select database to use 
    $db_select = mysql_select_db("manage_projects",$connection);
    if (!$db_select) {
        die("Database selection failed: " . mysql_error());
    }

    // 3. Perform database query
    $result = mysql_query("SELECT survey_desc FROM subjects ORDER BY id DESC LIMIT 0,1", $connection);
    if (!$result) {
        die("Database query failed: " . mysql_error());
    }

    // 4. Use returned data
    while ($row = mysql_fetch_array($result)) {
        echo $row["survey_desc"]."<br />";
    }

    // 4.1 Alternative way to use returned data
    /* $row = mysql_fetch_array($result);
    echo $row["survey_desc"]."<br />";
    */

    // 5. Close connection
    mysql_close($connection);
?>

The html file:

    <html>
<head>
<script type="text/javascript src="myscript.php"">

    //if clicked Yes open new page if Cancel stay on the page 
    function popup(){
    var r=confirm("echo the php query here");
        if (r==true)
            {
            window.location = "http://example.com";
            }       
}
</script>
</head>
<body onload ="popup()">

</body>
</html>
share|improve this question

4 Answers

up vote 0 down vote accepted
<head>
<script type="text/javascript">
function popup(){
    var xhr=null;

    if (window.XMLHttpRequest) { 
        xhr = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) {
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xhr.onreadystatechange = function() {
      if(xhr.readyState == 4){ alert_ajax(xhr); }
     }
    xhr.open("GET", "myscript.php", true);
    xhr.send(null);
}
function alert_ajax(xhr){
   var docAjax= xhr.responseText;
   r=confirm(docAjax);

        if (r==true)
            {
                window.location = "http://example.com";
            }
}
</script>
</head>
share|improve this answer
Hi mgraph, thanks for reply- I have tried your piece of code and I get the popup the only issue is that I have to click the "yes" button twice and only then I get the variable displayed... – Daniel Mar 19 '12 at 15:58
@Daniel see updated script – mgraph Mar 19 '12 at 16:06
thank you very much. is really working now. The only functionality which is gone from my previous code is when I click on the OK button I am not redirected to page I want to send the user. In my previous code when the OK button was clicked the user was redirected to a new page "window.location = "example.com";"; Do you have suggestion on how I can implement that. Many Thanks. – Daniel Mar 19 '12 at 16:24
I figured out how to implement the "window.location" for the ok button. Thanks again for ur help. – Daniel Mar 19 '12 at 19:50

You can echo into JavaScript:

var r=confirm("<?php echo $relevant_variable; ?>");

Also it's not recommended to use die() in a production environment.

share|improve this answer
Why is die() not recommended? – OhCaN Mar 19 '12 at 14:37
Basically if there's a problem, the error will both kill your website, and be visible to all, which is a security problem. You should use trigger_error() or a custom function for error reporting. – Ynhockey Mar 19 '12 at 15:32
Hi Ynhockey, thanks for ur reply - I have tried what you've said before posting my question here but it doesn't work. All I get into the popup is <?php echo $relevant_variable;?>. – Daniel Mar 19 '12 at 15:34
The variable is not escaped. This script will crash if there is a quote (") or a newline (\n) contained in the value. – elusive Mar 19 '12 at 17:06

Use PHP's json_encode()-function:

var r = confirm(<?php echo json_encode("the php query here"); ?>);

It escapes for you and always produces valid JavaScript, since JSON is a subset of the JavaScript syntax.

share|improve this answer
<?php 
    // Your php code;
    $myVar="your value that you want to pass to js";
?>
<html>

    <head>
     <script>
        //if clicked Yes open new page if Cancel stay on the page 
        function popup(){
        var mvar = '<?php echo $myVar ;?>';
        var r=confirm(mvar);
        if (r==true)
        {
            window.location = "http://example.com";
        }       
    }
    </script>
</head>
    <body onload ="popup()">

    </body>
</html>

Just combine your both files in one php file.

share|improve this answer
Hi Sheikh, thanks for your answer- Unfortunately I need to keep those files seprate, however combining both files it will only echo the $myVar ignoring completely JavaScript popup box. – Daniel Mar 19 '12 at 15:33
Same as above: The variable is not escaped. This script will crash if there is a quote (") or a newline (\n) contained in the value. – elusive Mar 19 '12 at 17:07

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.