duan2477 2017-06-30 16:24
浏览 67
已采纳

如果AJAX成功中的else语句

This is how i am trying to check json data. If the data inserts correctly i want the mentioned jquery in success code. But if it is not inserted then i want else code to run. But the if else conditions are not working properly.I am including php code and ajax code which i have tried. Am i doing it right?

AJAX

$( "#frm_add" ).on('submit',(function(e) {

                e.preventDefault();
                var  img= new FormData(this);
                datas = $("#frm_add").serializeArray();
                $.each(datas,function(key,input){
                    img.append(input.name,input.value);
                });
                $.ajax({
                    url: "response_categories.php", // Url to which the request is send
                    type: "POST",             // Type of request to be send, called as method
                    //data:new FormData(this),
                    data:img,
                    // data: {img:img,datas:datas}, // Data sent to server, a set of key/value pairs (i.e. form fields and values)
                    contentType: false,       // The content type used when sending data to the server.
                    cache: false,             // To unable request pages to be cached
                    processData: false,        // To send DOMDocument or non processed data file it is set to false
                    success: function (data)   // A function to be called if request succeeds
                    {

                        if(data == true)
                        {
                            $('#add_model').modal('hide');
                            $("#categories_grid").bootgrid('reload');
                        }
                        else
                        {
                            $("#nameerr").html("<p id='error' style='color:red'>"+data+"</p>");
                        }

                    }

                });
            }));

php

  function insertCategories($params)
    {

        $fileName = $_FILES['cat_image']['name'];
        $name = $params['cat_name'];
        $type = $params['cat_type'];
        $switch = $params['cat_switch'];
        $chk=mysqli_query($this->conn,"select * from categories where cat_name='$name'");
       if(mysqli_num_rows($chk)==0)
       {
           $sql = "INSERT INTO `categories` (cat_name,cat_image, cat_type, cat_switch) VALUES('$name','$fileName', '$type','$switch');  ";
           echo $result = mysqli_query($this->conn, $sql) or die("error to insert Categories data");


           if ($result) {
               if (file_exists("images/" . $_FILES["cat_image"]["name"])) {
                   echo  $fileName . " <span id='invalid'><b>already exists.</b></span> ";
               } else {
                   $sourcePath = $_FILES['cat_image']['tmp_name']; // Storing source path of the file in a variable
                   $targetPath = "images/" .$fileName; // Target path where file is to be stored
                   move_uploaded_file($sourcePath, $targetPath); // Moving Uploaded file
               }

           }
           echo json_encode($result);

       }



    }
  • 写回答

2条回答 默认 最新

  • dpp89959 2017-06-30 16:59
    关注

    Add the error command in your ajax call to execute if the command fails or returns no data in general

    $.ajax({
      url: "response_categories.php", // Url to which the request is send
      type: "POST",             // Type of request to be send, called as method
      data:img,
      contentType: false,       // The content type used when sending data to the server.
      cache: false,             // To unable request pages to be cached
      processData: false,        // To send DOMDocument or non processed data file it is set to false
      success: function (data),   // A function to be called if request succeeds
        {
          if(data)
            {
              $('#add_model').modal('hide');
              $("#categories_grid").bootgrid('reload');
            }
            else
              {
                $("#nameerr").html("<p id='error' style='color:red'>"+data+"</p>");
              }
    
        }
      error: function (data)
        {
          $("#namerr".html("<p id='error' style='color:red'>"+data+"</p>");
        }
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名