php Javascript函数发布和调用php脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16834138/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript function post and call php script
提问by DaViDa
In html I have several buttons which are automatically made for each object in the database with a particular status. Each button gets its own id.
在 html 中,我有几个按钮,它们是为数据库中具有特定状态的每个对象自动生成的。每个按钮都有自己的 id。
echo '<Button id="button'.$counter.'" onClick="clickedbutton('.$counter.', '.$row['OrderID'].')" >'."<center>".$row['OrderID']."<br>"."</center>".'</Button>';
The button calls the javascript function clickedbutton and gives it the number of the button and the orderid of that button.
该按钮调用 javascript 函数 clickedbutton 并为其提供按钮编号和该按钮的 orderid。
function clickedbutton(buttonid,orderid){
buttonid = "button" + buttonid;
}
This function loads in the number of the button and makes it button0, button1 etc. The orderid is also succesfully passed through. Now in the function I want to call an external php script, but also orderid must be passed through to the script.
此函数加载按钮的编号,并使其成为 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();
?>
I know about the mysqli protection and all, I will adjust that later. Now I want to focus on the question above, how to call and pass through the variable orderid to the phpscript.
我知道 mysqli 保护等等,我稍后会调整。现在我想重点讨论上面的问题,如何调用变量orderid并将其传递给phpscript。
回答by Savas Vedova
EDIT 2018
编辑 2018
Yeah, I am still alive. You can use the fetch
API instead of jQuery
. It is widely supported except (guess who?...) IE 11 and below but there is a polyfill for that. Enjoy modern coding.
是的,我还活着。您可以使用fetch
API 代替jQuery
. 它得到广泛支持,除了(猜猜是谁?...)IE 11 及更低版本,但有一个 polyfill。享受现代编码。
OLD ANSWER
旧答案
You will have to use AJAX.
您将不得不使用 AJAX。
Javascript alone cannot reach a php script. You will have to make a request, pass the variable to PHP, evaluate it and return a result. If you'are using jQuery sending an ajaxrequest is fairly simple:
单独的 Javascript 无法访问 php 脚本。您必须发出请求,将变量传递给 PHP,对其进行评估并返回结果。如果您使用 jQuery 发送ajax请求是相当简单的:
$.ajax({
data: 'orderid=' + your_order_id,
url: 'url_where_php_is_located.php',
method: 'POST', // or GET
success: function(msg) {
alert(msg);
}
});
and your php script should get the order id like:
并且您的 php 脚本应该获得如下订单 ID:
echo $_POST['orderid'];
The output will return as a string to the success function.
输出将作为字符串返回到成功函数。
EDIT
编辑
You can also use the shorthand functions:
您还可以使用速记函数:
$.get('target_url', { key: 'value1', key2: 'value2' }).done(function(data) {
alert(data);
});
// or eventually $.post instead of $.get
回答by Ivo Pereira
By using Ajax.
通过使用 Ajax。
function clickedbutton(buttonid,orderid){
$.post("page.php", { buttonid: buttonid })
.done(function(data) {
alert("Data Loaded: " + data);
});
}
In php you get it with $_POST.
在 php 中,您可以通过 $_POST 获得它。
//[..] previous php code
$strSQL = "update orders set OrderStatus = 'In Progress' where OrderID = '" + $_POST['buttonid'] + "'";
//[..] rest of php code
Watch out for SQL injection. Don't take this advice as written.
注意 SQL 注入。不要把这个建议当成书面的。
回答by Jesús Carrera
Assuming you don't want to use AJAX, you can do something like this in your clickedbutton
function:
假设你不想使用 AJAX,你可以在你的clickedbutton
函数中做这样的事情:
window.location.replace('path/to/page.php?orderid=' + orderid);
and then in your page.php
然后在你的 page.php
"...where OrderID = '" . $_GET('orderid') . "'";
(note the dots to join strings)
(注意连接字符串的点)
回答by HarryFink
The easiest way is to build a query string and attach it to the end of the php script's url.
最简单的方法是构建一个查询字符串并将其附加到 php 脚本 url 的末尾。
function clickedbutton(buttonid,orderid){
var url = 'script.php?';
var query = 'buttonid=' + buttonid + '&orderid=' + orderid;
window.location.href = url + query
}
In the php script you can access the parameters via like this:
在 php 脚本中,您可以通过如下方式访问参数:
<?php
echo $_GET['buttonid'];
echo $_GET['orderid'];
回答by sAnS
you can try like this
你可以这样试试
var url = myurl +'?id=' + orderid;
window.location.href = url;
and in php page
并在 php 页面中
$strSQL = "update orders set OrderStatus = 'In Progress' where OrderID = '".mysql_real_escape_string($_GET['id'])."'";
Edit
编辑
If you want to load the php file without page refresh then you can try as fellow friends suggested..
如果您想在不刷新页面的情况下加载php文件,那么您可以按照朋友的建议尝试..