php array_push没有在ajax调用后追加数组

php array_push not appending the array after the ajax call

本文关键字:调用 ajax 追加 数组 array push php      更新时间:2024-02-14

我使用的是ajax和php,每次调用ajax时,我都想将我的数组分别追加一个。但它不起作用。这些是我的代码:

             $('#multiple_upload_form' +count).ajaxForm({
                    target:'#images_preview'+count,
                    beforeSubmit:function(e){
                    console.log("gud to go");
             },
            success:function(data){
                console.log(data);
                    console.log("succeded");
            },
            error:function(data){
                    console.log("failde");
            }
           }).submit();

PHP这是它的PHP方面

<?php
      $questionArr = array();
     if($_POST['image_form_submit']){
             array_push($questionArr,$questionNum );
            if(is_array($questionArr)){
                  foreach($questionArr as $val) {
                    if ($val == $questionNum){
                        $response['response']= "exist";
                          echo json_encode($questionArr);
                    }else{
                       $response['response']= "question does not exist";
                       echo json_encode($response);
                       }
                   }    
           }else{
                       $response['response']= "not array";
                       echo json_encode($response);
           }    
     }
    ?>

这是我的HTML

         <form             method="post"name="multiple_upload_form"id="multiple_upload_form" enctype="multipart/form-data" action="php_work/test.php">
      <input type="hidden" name="image_form_submit" value=""/>
      <input type="file"  name="images[]" id="images" multiple > 
       </form>

它可能会帮助您:

<?php
 $questionNum = 1; // Or from anywhere you are getting data into it
 $questionArr = array();
 $response['response']= "not array"; // Giving Default value
 if($_POST['image_form_submit']){
  $questionArr[] = $questionNum;
  if(!empty($questionArr)){
    foreach($questionArr as $val) {
      if ($val == $questionNum){
        $response['response'] = "exist";
      }else{
        $response['response']= "question does not exist";
      }
    }
  }
 }
 echo json_encode($response);
 exit;
?>

第一:PHP中有一个if语句不会返回值,因为没有else。因此,如果$_POST['image_form_submit']为假,则没有任何内容是echo编辑的。

第二:array_push($questionArr, $questionNum);你在PHP脚本中在哪里定义了$questionNum

第三:在你的JS:$('#multiple_upload_form' +count)中,你试图引用一个id为数字的元素?我在你的HTML中没有看到这样的元素。我也没有在JS代码中看到您定义count的地方。

长话短说:检查PHP和JS代码中的错误。