将javascript变量(数组)传递给php [英] Passing javascript variables(array) to php

查看:89
本文介绍了将javascript变量(数组)传递给php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数,用户输入的内容存储在一个javascript变量中。



 <$点击(function(){var seat = [],item; $ .each($('#place li。'+ settings.selectingSeatCss +'a'),function(索引,值){item = $(this).attr('title'); seat.push(item);}); var bookseats = seat; $ .ajax({type:'POST',url:'confirm。 php',data:{'bookseats':bookseats},});});  

b $ b

当用户点击 #btnsubmit 按钮时,我想将这个变量(实际上是一个数组)发送到一个名为confirm.php的PHP文件。



 < form method =POSTaction =confirm.php> < div align =center>< input type =Submitid =btnsubmitvalue =Submit/>< / div>< / form>  

在我的PHP文件中,我编写了代码来获取发送的变量,如下所示。

  $ bookseats =; 

if(isset($ _ POST ['bookseats']))
{
$ bookseats = $ _POST [bookseats];
print_r($ bookseats);
}

执行时,PHP文件中没有任何反应(不会打印<

解决方案

不要使用成功回调来获取PHP代码的输出。请参阅 成功回拨

  $。ajax({
type:'POST',
url:'confirm.php',
data:{'bookseats':bookseats},
success:function(data){
console.log(data); //或alert(data);
}
});

另外,我认为您应该停止传播按钮的默认行为,以防止浏览器将页面重定向到表单的动作URL:

  $('#btnsubmit')。click(function(ev){
ev.preventDefault();

正如@Malovich指出的那样,从jQuery 1.8开始,也可以使用 .then()

  $。ajax({
类型:'POST',
url:'confirm。 php',
data:{'bookseats':bookseats}
})。then(function(data){
console.log(data); //或alert(data);
},function(){
console.log(Error);
});


I have a function where the user inputs are stored in a variable in javascript.

$('#btnsubmit').click(function() {
    var seat = [], item;
    $.each($('#place li.' + settings.selectingSeatCss + ' a'), function (index, value) {
        item = $(this).attr('title');                   
        seat.push(item);					
    });
	var bookseats = seat;
	$.ajax({
	  type: 'POST',
	  url: 'confirm.php',
	  data: {'bookseats': bookseats},
	});
});

When the user clicks on the #btnsubmit button, I want to send this variable(actually an array) to a PHP file named confirm.php.

<form method="POST" action="confirm.php"> 
<div align="center"><input type="Submit" id="btnsubmit" value="Submit" /></div>
</form>

In my PHP file, I've written the code to get the sent variable as follows.

$bookseats = "";

if(isset($_POST['bookseats']))
{
    $bookseats = $_POST["bookseats"];
    print_r($bookseats);
}

When executed, nothing happens in the PHP file(doesn't print the bookseats).Is there something wrong with this code?

解决方案

You're not using a "success" callback to get the output of the PHP code. See success callback

$.ajax({
  type: 'POST',
  url: 'confirm.php',
  data: {'bookseats': bookseats},
  success: function(data) {
     console.log(data); // or alert(data);
  }
});

Also, I think you should stop the propagation of the default behavior of the button, to prevent the browser to redirect the page to the form's action URL:

$('#btnsubmit').click(function(ev) {
    ev.preventDefault();

As @Malovich pointed out, as of jQuery 1.8, you could also use .then():

$.ajax({
  type: 'POST',
  url: 'confirm.php',
  data: {'bookseats': bookseats}
}).then(function(data) {
  console.log(data); // or alert(data);
}, function(){ 
  console.log("Error");
});

这篇关于将javascript变量(数组)传递给php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆