![](/img/trans.png)
[英]How to pass variable from JavaScript to PHP using jQuery POST
[英]How to post variable using jQuery to PHP?
我想使用jQuery将变量发布到PHP页面,然后在该PHP页面上重定向并回显发布的变量。
HTML:
$(document).ready(function() {
$('.restauarnt_result').click(function() {
var RestName = $('#restauarnt_name', this).html();
$.post('restauarnt.php', { RestName : RestName } );
window.location = 'restauarnt.php';
});
});
单击$('.restauarnt_result')
,自动重定向到'restauarnt.php'URL并回显发布的数据。
PHP:
<?php
echo $_POST['RestName'];
Ajax的重点是在不离开当前页面的情况下与服务器通信。
由于您想离开当前页面,所以不要使用Ajax。
创建一个表单,然后提交。
var f = jQuery("<form>", { action: 'restauarnt.php', method: 'post' });
f.append(
jQuery("<input>", { type: "hidden", name: "RestName", value: RestName })
);
jQuery(document.body).append(f);
f.submit();
如果您要立即重定向,那么使用ajax没有什么意义
<form method="post" action="restauarnt.php">
<input name="RestName"/>
<input type="submit"/>
</form>
restauarnt.php
<?
echo $_POST['RestName'];
在PHP端使用会话:
<?php
session_start();
// store session data
$_SESSION['RestName']= "RestName";
?>
重新加载后,此值将在新的会话变量中。
呼叫完成后,您应该重定向。 所以试试这个
$.post('restauarnt.php', { RestName : RestName }, function(data){
// here the call is completed
console.log(data); // this will print the values echo by the php
window.location = 'restauarnt.php';
} );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.