duanbin198788 2016-10-06 13:57
浏览 130
已采纳

Ajax从PHP接收错误的错误

I am attempting to set up modal for a project in which a client would be able to update their favorite team, city of birth and their size. Upon not entering some of the information I would like an error message to be returned to them but the only error message being echoed is "City was not updated as there was not new information submitted."

the modal code is as follows:

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-  labelledby="myModalLabel" aria-hidden="true">
 <div class="modal-dialog" role="document">
 <div class="modal-content">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
      <span aria-hidden="true">&times;</span>
      </button>
     <h4 class="modal-title" id="myModalLabel">Modal title</h4>
    </div>
     <div class="modal-body">
    <form>
  <div class="form-group">
    <label for="city">You were born in:</label>
   <input type="text" class="form-control" id="city"  placeholder="city">

   </div>
   <div class="form-group">
   <label for="favorite_team">Favorite team:</label>
    <input type="text" class="form-control" id="favoriteTeam"  placeholder="favorite team">

   </div>
   <div class="form-group">
   <label for="size">size:</label>
    <input type="text" class="form-control" id="size"  placeholder="size">

   </div>

    <div class="alert alert-success" id="successAlert" role="alert"  style="display: none"></div>
   <div class="alert alert-danger" id="updateFail" style="display:none" >          </div>

    </form>
    </div>
     <div class="modal-footer">
     <button type="button" class="btn btn-secondary" data-dismiss="modal"  >Close</button>
     <button type="button" class="btn btn-primary" id="saveBtn"  onClick="Changes()">Save changes</button>
   </div>
   </div>
   </div>
   </div>

my ajax code is:

  function Changes(){
   $.ajax({
           type: "POST",
          url: "../php_parsers/update_parse.php",
          data: "city=" + $("#city").val() + "&favoriteTeam=" +    $("#favoriteTeam").val() + "&size=" + $("#size").val(),
            success: function(result) {
              if (result == "success") {

                $("#successAlert").html(result).show();

                } else {

                $("#updateFail").html(result).show();

                }
             }

           })

           }

my php code is update_parse.php:

<?php
// AJAX CALLS THIS UPDATE CODE TO EXECUTE
$error = "";
$success = "";
if(!$_POST['city']){
$error .= "City was not updated as there was not new information submitted.";
} else if(!$_POST['team']){
$error .= "Team was not updated as there was not new information submitted.";
} else if(!$_POST['size']){
    $error.= "size was not updated as there was not new information submitted.";
    } 

    if($error == ""){
        echo $error;
        }else {
            $success = "update successful so far";
        echo $success;


            }


 ?>

All feed back is welcomed.

  • 写回答

3条回答 默认 最新

  • duanmao1872 2016-10-06 14:05
    关注

    You are using if else if construct for you checks which will only report at most one error. Change it to independent if statements

    if(!$_POST['city']){
        $error .= "City was not updated as there was not new information submitted.";
    } 
    if(!$_POST['team']){
        $error .= "Team was not updated as there was not new information submitted.";
    } 
    if(!$_POST['size']){
        $error.= "size was not updated as there was not new information submitted.";
    } 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算