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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。