如何使用jQuery通过ajax提交表单?

时间:2022-09-25 15:51:22

I need to submit an html form via .ajax with jQuery. When I submit it normally, not via ajax, everything works great. It is in switching to ajax that I encounter problems.

我需要通过jQuery通过.ajax提交一个html表单。当我正常提交,而不是通过ajax,一切都很好。我正在转向ajax遇到问题。

The form has changing number of inputs. (One each for each user in a particular group, groups can have a differing number of users). So I name the inputs like so:

表单的输入数量正在变化。 (对于特定组中的每个用户各一个,组可以具有不同数量的用户)。所以我将输入命名如下:

<input type="hidden" name="<?php echo 'sampleA['. $i .']'; ?>" value="sampleValue" />

Where $i is a counter so the inputs are sampleA['1'] and sampleA['2'] and so on.

其中$ i是一个计数器,因此输入是sampleA ['1']和sampleA ['2'],依此类推。

I have another input for each user that is done the same way, which we can call sample2['1'] and so forth.

我为每个用户提供了另一个输入,以相同的方式完成,我们可以将其称为sample2 ['1'],依此类推。

So with php I just get these 2 arrays and run through them with a similar counter and insert them to my db as need. This all works well. But I need to make it ajax.

所以使用php我只需要获得这两个数组并使用类似的计数器运行它们并根据需要将它们插入到我的数据库中。这一切都运作良好。但我需要把它变成ajax。

How do I pass those two arrays via jQuery's .ajax function to a php script, and then how do I handle it in php?

如何通过jQuery的.ajax函数将这两个数组传递给php脚本,然后如何在php中处理它?

I've tried using .serialize on my form, but that serializes both inputs on all the users into one array and I have been unable to de-serialzie that correctly on the php side of things.

我已经尝试在我的表单上使用.serialize,但是将所有用户的两个输入序列化为一个数组,并且我无法在php方面正确地解串行。

The following doesn't work, but I will include it here to show you what I've been doing:

以下不起作用,但我会在此处包含它以向您展示我一直在做的事情:

function updateOnCourt() {             
    var thearray = $('#sub-form').serialize();
    $.ajax({
      url: 'sub-update.php',
      method: 'POST',
      data: thearray,
      success: function() {
        alert('Success: '+thearray);
      }
    });
  }

and then in my PHP script:

然后在我的PHP脚本中:

<?php 
$size = count($_POST['thearray']);
$finally = json_decode($_POST['thearray'], true);

// start a loop in order to update each record
$i = 1;

while ($i < ($size+1)) {


// define each variable
$pid = $finally[$i];
$playing = $finally[$i];


$query = "UPDATE players SET 
    on_court = '{$playing}'
WHERE id = {$pid}";

mysql_query($query, $connection);

$i = $i + 1;

} ?>    

What am I doing wrong?

我究竟做错了什么?

1 个解决方案

#1


1  

thearray is only a variable name in javascript, so there is no $_POST['thearray'] and the data is not sent as JSON so no need to use json_decode

thearray只是javascript中的一个变量名,所以没有$ _POST ['thearray']并且数据不是作为JSON发送的,所以不需要使用json_decode

You want to access the name attributes in the form controls:

您想要访问表单控件中的名称属性:

Try looking at $_POST['sampleA'].

试着看$ _POST ['sampleA']。

Look at docs for serialize() will see that it sends the data in same format as if form was submitted without AJAX

查看serialize()的文档将看到它以相同的格式发送数据,就像在没有AJAX的情况下提交表单一样

API Reference: http://api.jquery.com/serialize/

API参考:http://api.jquery.com/serialize/

#1


1  

thearray is only a variable name in javascript, so there is no $_POST['thearray'] and the data is not sent as JSON so no need to use json_decode

thearray只是javascript中的一个变量名,所以没有$ _POST ['thearray']并且数据不是作为JSON发送的,所以不需要使用json_decode

You want to access the name attributes in the form controls:

您想要访问表单控件中的名称属性:

Try looking at $_POST['sampleA'].

试着看$ _POST ['sampleA']。

Look at docs for serialize() will see that it sends the data in same format as if form was submitted without AJAX

查看serialize()的文档将看到它以相同的格式发送数据,就像在没有AJAX的情况下提交表单一样

API Reference: http://api.jquery.com/serialize/

API参考:http://api.jquery.com/serialize/