Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用AJAX将数组传递给php脚本_Php_Mysql_Ajax - Fatal编程技术网

使用AJAX将数组传递给php脚本

使用AJAX将数组传递给php脚本,php,mysql,ajax,Php,Mysql,Ajax,我有一个表单,我想在不刷新页面的情况下提交,我是AJAX新手,虽然我通过一个值来管理这个任务,但我不知道如何使用数组来完成 表单输入(使用while循环显示) 在做了一些研究之后,我认为使用datastring是前进的方向,但我不确定如何将其与数组(reason)和用户ID(user)一起使用,然后在PHP脚本中使用。将数据作为json发送 var myobj = { this: 'that' }; $.ajax({ url: "my.php", data: JSON.stringify

我有一个表单,我想在不刷新页面的情况下提交,我是
AJAX
新手,虽然我通过一个值来管理这个任务,但我不知道如何使用数组来完成

表单输入(使用
while
循环显示)


在做了一些研究之后,我认为使用datastring是前进的方向,但我不确定如何将其与数组(
reason
)和用户ID(
user
)一起使用,然后在
PHP
脚本中使用。

将数据作为json发送

var myobj = { this: 'that' };
$.ajax({
  url: "my.php",
  data: JSON.stringify(myobj),
  processData: false,
  dataType: "json",
  success:function(a) { },
  error:function() {}
});
在服务器端脚本上

<?php
  $array = json_decode(file_get_contents("php://input"), true);
?>


将true作为第二个参数传递给json_decode会使其返回一个数组,因此不需要将其强制转换为数组;实际上是一个json对象,用于存储javascript数组进行处理。在本例中,它将被解释为类似php的数组(“this'=>“that”);所以我不必用自己的值来更改它?使用$(this).serializeArray()将数组序列化为json,这样就可以将其作为json字符串传递。var dataString=$('#yourfield').serializeArray();谢谢,我将如何在php页面上使用它?(我对AJAX非常陌生)没关系,使用它,我认为我必须在使用它之前以某种方式对数据进行非序列化,但一点研究表明不是这样,感谢您提供了一个简单的解决方案!没问题:)。很乐意帮忙。
   if(isset($_POST['save_reasons'])){
      $user_id = $_POST['user'];
       foreach($_POST['reason'] as $item=>$value)
     {
   if(is_array($value)){
       foreach($value as $ID=>$reason)
        {
        $sql = "UPDATE gradeReason SET reason_name = '$reason' WHERE reasonID = $ID";
        $result = mysqli_query($mysqli,$sql) or die(mysqli_error($mysqli));
        }   
         }
      else{
        if($value !=''){
              $sql = "INSERT INTO gradeReason (reason_userID, category, reason_name) VALUES ($user_id, 'positioning', '$value')";
              $result = mysqli_query($mysqli,$sql) or die(mysqli_error($mysqli));
           }
        }
         } 
     }
var myobj = { this: 'that' };
$.ajax({
  url: "my.php",
  data: JSON.stringify(myobj),
  processData: false,
  dataType: "json",
  success:function(a) { },
  error:function() {}
});
<?php
  $array = json_decode(file_get_contents("php://input"), true);
?>
  $.ajax({
    type: "POST",
    url: "save_reasons.php",
    data: $('#myForm').serialize(),
    success: function(){
    alert('yay');
     }
  });