【问题标题】:php array_push not appending the array after the ajax callphp array_push 在ajax调用后不附加数组
【发布时间】:2016-04-07 13:46:06
【问题描述】:

我正在使用 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>

【问题讨论】:

  • 您应该将该代码编辑到问题中,而不是添加评论:)
  • 您的任何console.log() 输出是否有效? ajax 调用是否已提交?您的表单实际上是 POST 还是 GET 表单?
  • 谢谢我已经编辑了。 ajax 调用已提交。我正在收到回复。但数组没有附加。谢谢
  • 好吧,没有相关的 HTML 表单我们只能猜测。

标签: javascript php jquery arrays ajax


【解决方案1】:

它可以帮助你:

<?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;
?>

【讨论】:

  • $questionNum= $_POST['image_form_submit'];这就是问题编号的来源
  • 它应该是动态的。如果数组中不存在新的问题编号,则追加数组。否则返回错误。谢谢
  • @trinidado 您在$_POST['image_form_submit'] 中收到了哪种类型的数据,您可以在评论中粘贴吗?
【解决方案2】:

首先:PHP 中有一个if 语句不会返回值,因为没有else。所以如果$_POST['image_form_submit'] 是假的,那么echoed 什么都不是。

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

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

长答案短:检查 PHP 和 JS 代码是否有错误。

【讨论】:

  • $questionNum= $_POST['image_form_submit'];这就是问题编号的来源
  • 对于 JS ,不要介意计数。它把它拿出来了。谢谢
猜你喜欢
  • 2011-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多