$ .post函数(响应)未成功返回

时间:2017-09-15 03:33:44

标签: javascript php jquery ajax

我使用select2在一个输入元素中获取多个成员名称。这是我的ajax代码:(在我的form.php中)

      $('#student_vgroup').submit(function(event){
      event.preventDefault(); 

      var name = $('#select2_demo_2').val();
      var day = $('#day_vgroup').val();
      var venue = $('#venue_vgroup').val();
      var time = $('#time_vgroup').val();

        $.post(
          'includes/vgroup-func.php',
          {
            member_name: name,
            day: day,
            venue: venue,
            time: time,
            leader_id: id
          },
          function(result){
            if (result == "success") {
              location.reload();
            } else {
                $('.error-message-vgroup').load("includes/vgroup-func.php", {
                member_name: name,
                day: day,
                venue: venue,
                time: time,
                leader_id: id
              });
            }
          }
        );
     });

我的服务器端有这个代码:(我会写这个想法,因为它太长了)

validation.php

 <?php
    $day =  mysqli_real_escape_string($conn, $_POST['day']);
    $venue = mysqli_real_escape_string($conn, $_POST['venue']);
    $time = mysqli_real_escape_string($conn, $_POST['time']);

    if(!isset($_POST['member_name'])) {
       echo "enter member names";
    } else {
        foreach($_POST['member_name'] as $selectedOptions) {

          *query data from database to check if one of the members selected 
           already exists in my group.
          *query data form database to check if one of the members selected is 
           my leader.
          *query data from database to check if one of the members selected 
           already belongs to someone else's group.

          if (already exists in my group) {
             echo "Cannot add. Already in your group";
          } elseif (is my leader) {
             echo "Cannot add leader";
          } elseif (belongs to someone else) {
             echo "Cannot add from someone else's group";
          } elseif (empty($day)) {
             echo "Enter day";
          } elseif (empty($venue)) {
             echo "Enter venue";
          } elseif (empty($time)) {
             echo "Enter time";
          } else {
             *insert data to the database
             echo "success";
          }
        }
    }

?>

我的问题是,验证工作正常。但它似乎没有在我的ajax函数(结果)调用中进入“成功”状态。成功将在div标签中回显出来,其中包含我的错误消息所在的类“error-message-vgroup”,而不是重新加载页面。有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:1)

请你尝试以这种方式从循环外部返回输出。

$output = "";
foreach($_POST['member_name'] as $selectedOptions) {

      *query data from database to check if one of the members selected 
       already exists in my group.
      *query data form database to check if one of the members selected is 
       my leader.
      *query data from database to check if one of the members selected 
       already belongs to someone else's group.

      if (already exists in my group) {
         $output = "Cannot add. Already in your group";
      } elseif (is my leader) {
         $output ="Cannot add leader";
      } elseif (belongs to someone else) {
         $output ="Cannot add from someone else's group";
      } elseif (empty($day)) {
         $output ="Enter day";
      } elseif (empty($venue)) {
         $output ="Enter venue";
      } elseif (empty($time)) {
         $output ="Enter time";
      } else {
         *insert data to the database
         $output ="success";
      }
    }

    echo $output;
    exit;