Javascript 在Javascript函数下调用php页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7157029/
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
call php page under Javascript function
提问by John
Is it Possible to call php page under Javascript function? I have an javascript function and I want to call php page if someone press okk
是否可以在Javascript函数下调用php页面?我有一个 javascript 函数,如果有人按 okk,我想调用 php 页面
Here is my code so far
到目前为止,这是我的代码
function show_confirm()
{
var r=confirm("Do You Really want to Refund money! Press ok to Continue ");
if (r==true)
{
alert("You pressed OK!");
return true;
}
else
{
alert("You pressed Cancel!");
}
}
and this Js function i m using here
我在这里使用的这个 Js 函数
<td align="center"><input name="Refund" onclick="show_confirm()" type="submit" value="Refund" /></td>
now i want if user press okk then call other php page
...
现在我想如果用户按确定然后call other php page
...
回答by mithunsatheesh
if you want to redirect to a page:yourphppage.php if the user pressed ok.
如果你想重定向到一个页面:yourphppage.php 如果用户按下确定。
function show_confirm()
{
var r=confirm("Do You Really want to Refund money! Press ok to Continue ");
if (r==true)
{
window.location="yourphppage.php";
return true;
}
else
{
alert("You pressed Cancel!");
}
}
回答by ChrisK
If you only want to redirect the page to the "refund" page without using AJAX you can do something along the lines of the following:
如果您只想在不使用 AJAX 的情况下将页面重定向到“退款”页面,您可以执行以下操作:
(assumes $transaction_id variable is the id number you are looking to refund)
(假设 $transaction_id 变量是您要退款的 ID 号)
HTML Lines:
HTML 行:
<td align="center">
<input name="Refund" onclick="show_confirm(<?php echo $transaction_id ?>)" type="submit" value="Refund" />
</td>
Javascript lines:
Javascript 行:
function show_confirm(transaction_id)
{
var r = confirm("Do You Really want to Refund money! Press ok to Continue ");
if (r == true)
{
alert("You pressed OK!");
location.href = "refund.php?transaction_id="+transaction_id;
return true;
}
else
{
alert("You pressed Cancel!");
}
}
回答by Dongsheng Cai
If you are looking for page direction:
如果您正在寻找页面方向:
window.location="index.php";
There are plenty other methods to redirect: http://grizzlyweb.com/webmaster/javascripts/redirection.asp
还有很多其他的重定向方法:http: //grizzlyweb.com/webmaster/javascripts/redirection.asp
回答by DenisPostu
Place this where you want your redirect to happen:
将此放在您希望重定向发生的位置:
location.href = "newpage.php";
回答by James
Checkout
http://www.w3schools.com/xml/xml_http.asp
Then have a look at
http://mootools.net/docs/core/Request/Request
Any of the JS libraries prototype Jquery etc. have a wrapper to make requests and your life easier.
Then just have a button and set its onClick to be the request call to the server and listen for the response.
结帐
http://www.w3schools.com/xml/xml_http.asp
然后看看
http://mootools.net/docs/core/Request/Request
任何 JS 库原型 Jquery 等都有一个包装器来制作请求和您的生活更轻松。
然后只需有一个按钮并将其 onClick 设置为对服务器的请求调用并侦听响应。
Some tips you can append data to the request URL to pass your PHP script variables.
Checkout
http://www.w3schools.com/tags/ref_urlencode.asp
一些提示,您可以将数据附加到请求 URL 以传递您的 PHP 脚本变量。结帐
http://www.w3schools.com/tags/ref_urlencode.asp