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

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化