douyun8901 2012-03-19 14:29
浏览 447
已采纳

将php变量显示在JavaScript弹出框中

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>
  • 写回答

4条回答 默认 最新

  • dongpo8250 2012-03-19 15:08
    关注
    <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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?