在 html 中,我有几个按钮,它们是为数据库中具有特定状态的每个对象自动创建的。每个按钮都有自己的 id。
echo '<Button id="button'.$counter.'" onClick="clickedbutton('.$counter.', '.$row['OrderID'].')" >'."<center>".$row['OrderID']."<br>"."</center>".'</Button>';
该按钮调用 javascript 函数 clickedbutton 并为其提供按钮编号和该按钮的 orderid。
function clickedbutton(buttonid,orderid){
buttonid = "button" + buttonid;
}
该函数加载按钮的编号并使其成为button0、button1等。orderid也成功传递。现在在函数中我想调用外部 php 脚本,但 orderid 也必须传递给脚本。
<?php
//connect to database
include_once('mysql_connect.php');
// Select database
mysql_select_db("test") or die(mysql_error());
// SQL query
$strSQL = "update orders set OrderStatus = 'In Progress' where OrderID = '" + orderid + "'";
mysql_close();
?>
我了解 mysqli 保护等所有内容,稍后我会进行调整。现在我想重点讨论上面的问题,如何调用并传递变量orderid给phpscript。
编辑2018
是的,我还活着。您可以使用fetch
API 代替jQuery
。它得到了广泛的支持,除了(猜猜是谁?...)IE 11 及更低版本,但有一个填充程序可以支持它。享受现代编码。
支持获取API https://caniuse.com/#feat=fetch
旧答案
您将必须使用 AJAX。
单独使用 Javascript 无法访问 php 脚本。您必须发出请求,将变量传递给 PHP,对其进行评估并返回结果。如果您使用 jQuery 发送ajax http://api.jquery.com/jQuery.ajax/请求相当简单:
$.ajax({
data: 'orderid=' + your_order_id,
url: 'url_where_php_is_located.php',
method: 'POST', // or GET
success: function(msg) {
alert(msg);
}
});
你的 php 脚本应该获取订单 ID,如下所示:
echo $_POST['orderid'];
输出将以字符串形式返回到 success 函数。
EDIT
您还可以使用简写函数:
$.get('target_url', { key: 'value1', key2: 'value2' }).done(function(data) {
alert(data);
});
// or eventually $.post instead of $.get
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)