gpt4 book ai didi

php - 将 php 变量显示到 JavaScript 弹出框中

转载 作者:行者123 更新时间:2023-11-29 01:44:53 25 4
gpt4 key购买 nike

我有一个 php 文件,它连接到 MySql 数据库并从特定表中读取最后一个条目。我想要做的是使用 JavaScript 弹出框将表中的最后一个条目显示(回显)到外部 html 文件中下面我有 PHP 文件的代码(工作正常)和 html 文件,但不幸的是我无法弄清楚如何将 PHP 变量传递给 JavaScript 函数。

非常感谢。

php 文件应该是这个:

    <?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);
?>

html文件:

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

最佳答案

你可以回显到 JavaScript 中:

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

另外,不建议在生产环境中使用 die()。

关于php - 将 php 变量显示到 JavaScript 弹出框中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9772018/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com