如何使用从PHP文件中的错误信息到Ajax响应? [英] How to use error messages from PHP file into AJAX response?

查看:87
本文介绍了如何使用从PHP文件中的错误信息到Ajax响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我下面code文件上传: HTML code:

I've following code for file upload : HTML code :

<a href="#" id="promotion_status_1">
                        <button type="button" class="btn btn-default brmodalbtn" data-toggle="modal" data-target="#BrandImageModal" id="1">On</button>
                      </a>

<div class="container">
  <div class="modal fade" id="BrandImgeModaal">
    <div class="modal-dialog">
      <div class="modal-content">
        <form id="form" enctype="multipart/form-data" role="form">
          <input type="text" class="form-control" name="brand_id" id="brand_id" value="{$data.id}">
          <input type="text" name="admin_url" id="admin_url" value="http://localhost/abc.com">    
          <input type="text" name="op" value="upload_brand_image">    
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
            <h4 class="modal-title">Brand Image</h4>
          </div>
          <div class="modal-body">
            <div id="messages"></div>
            <input type="file" name="file" id="file">
          </div>
          <div class="modal-footer">
            <button type="submit" class="btn btn-primary">Save</button>
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
          </div>
        </form>
      </div>
    </div>
  </div>
</div>

jQuery的AJAX code:

jQuery AJAX Code :

$('#form').submit(function(e) {
  var form = $(this);
  var formdata = false;
  if(window.FormData) {
    formdata = new FormData(form[0]);
  }

  var formAction = form.attr('action');

  $.ajax({
    type        : 'POST',
    url         : 'manufacturers.php',
    cache       : false,
    data        : formdata ? formdata : form.serialize(),
    //data      : formdata ? formdata : form.serialize() + '&' + $.param({'op':'upload_brand_image'}),
    contentType : false,
    processData : false,

    success: function(response) {
      if(response != 'error') {
        //$('#messages').addClass('alert alert-success').text(response);
        // OP requested to close the modal
        $('#BrandImgeModaal').modal('hide');
      } else {
        $('#messages').addClass('alert alert-danger').text(response);
      }
    }
  });
  e.preventDefault();
});

现在的PHP code是如下:

Now the PHP code is as follows :

$request = $_REQUEST ;
switch( $op ) {
    case "add": 
case "upload_brand_image":
      //print_d($request);

//here if error comes in validation I've an array of errors which needs to be printed in AJAX response. Following is the array
$error_messages  = array(
    "email"             => "Email Id can't be blank",
    "email_invalid" => "Email Id is not valid"
  );

      die;
      break;
  }

我的问题是我应该如何使用数组 $ error_messages 在Ajax响应,如果错误不来那么成功消息应该传递给AJAX功能,而不是这$ errror_messages阵列。

My issue is how should I use the array $error_messages in ajax response and if the error doesn't come then the success message should be passed to ajax function instead of this $errror_messages array.

如何实现这一目标?是否有人可以帮助我在这方面?

How to achieve this? Can someone please help me in this regard?

感谢。

推荐答案

为什么不使用JSON格式?

why not use json format?

$.ajax({
    dataType    : 'json',
    ........
    ........
    success: function(response) {
      if(response.email_invalid){
         //do something
      }else{
         //do something
      }
    }
  });

然后在你的PHP回声误差

then in your php echo the error

  $error_messages  = array(
     "email"             => "Email Id can't be blank",
     "email_invalid" => "Email Id is not valid"
   );
   echo json_encode($error_messages);

这篇关于如何使用从PHP文件中的错误信息到Ajax响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆