weixin_33682719 2017-02-03 09:48
浏览 23

ajax跳过php的响应

I am trying to pass PHP echo values through AJAX, but on success function AJAX skips the if and else conditions and executes the else statements even when the if condition is met.

Below is my php and ajax code

process_postadd.php

if(isset($_POST['btn-postAdd'])){
$istate = strip_tags($_POST['txt_istate']);
$iname = strip_tags($_POST['txt_iname']);   
if(empty($iname)){
    echo '1'; //check if iten name is empty
}
else{
try
    {   
        if($insertItem = $postItem->postAdd($istate,$iname)){
            echo "ok"; //sucessfully inserted
        }           
    }
    catch(PDOException $e)
    {
        echo $e->getMessage();
    }   
}   
}

script.js

$('document').ready(function()
{ 
 /* validation */
 $("#postAddForm").validate({
  rules:
  {
        txt_istate: {
        required: true
        },
        txt_iname: {
            required: true
        },
   },
   messages:
   {

        txt_istate: {
        required: "state required"
        },
        txt_iname: {
            required:  "item name required"
        },
   },
   submitHandler: submitForm    
   });  
   /* validation */

   /* postadd submit */
     function submitForm()
   {        
        var data = $("#postAddForm").serialize();

        $.ajax({

        type : 'POST',
        url  : 'process_postadd.php',
        data : data,
        beforeSend: function()
        {   
            $("#error").fadeOut();
            $("#btn-postAdd").html('<span class="glyphicon glyphicon-transfer"></span> &nbsp; posting ...');
        },
        success :  function(response)
           {                        
                if(response == "ok"){

                    $("#btn-postAdd").html('<img src="btn-ajax-loader.gif" /> &nbsp; Submiting item ...');
                    setTimeout(' window.location.href = "index.php"; ',4000);
                }
                else if(response == "1"){
                    $("#error").fadeIn(1000, function(){
                        $("#error").html('<div class="alert alert-danger"> <span class="glyphicon glyphicon-info-sign"></span> &nbsp; Sorry, add item name</div>');
                        $("#btn-postAdd").html('<span class="glyphicon glyphicon-log-in"></span> &nbsp; SIGN U');
                        });

                }
                else{

                    $("#error").fadeIn(1000, function(){                        
            $("#error").html('<div class="alert alert-danger"> <span class="glyphicon glyphicon-info-sign"></span> &nbsp; '+response+' !</div>');
                                        $("#btn-postAdd").html('<span class="glyphicon glyphicon-log-in"></span> &nbsp; Submit Item');
                                });
                }
          }
        });
            return false;
    }
   /* postadd submit */
});

HTML code

<div class="col-sm-12">
                        <div id="error">
                        <!-- error will be shown here ! -->
                        </div>
                            <form method= "POST" class="form-horizontal" enctype="multipart/form-data" id="postAddForm">
                                <fieldset>
                                    <div class="form-group">
                                        <label class="col-md-3 control-label">Item condition</label>

                                        <div class="col-md-8">
                                            <label class="radio-inline" for="radios-00">
                                                <input name="txt_istate" id="radios-00" value="New"
                                                       type="radio">
                                                New</label>
                                            <label class="radio-inline" for="radios-11">
                                                <input name="txt_istate" id="radios-11" value="Used" 
                                                        type="radio">
                                                Used </label>
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-md-3 control-label" for="Adtitle">Item Name</label>

                                        <div class="col-md-8">
                                            <input id="Adtitle" name="txt_iname" placeholder="name of item"
                                                   class="form-control input-md" type="text">
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-md-3 control-label"></label>

                                        <button class="btn btn-success" name="btn-postAdd" id="btn-postAdd" type="submit">
                                                 <span class="glyphicon glyphicon-log-in"></span> &nbsp; Submit Item 
                                           </button>
                                    </div>
                                </fieldset>
                            </form>
                        </div>
  • 写回答

2条回答 默认 最新

  • weixin_33744141 2017-02-03 10:10
    关注

    I'm not sure but try json_encode with your echo and add a console.log(response) for check the return

       if($insertItem = $postItem->postAdd($istate,$iname)){
            echo json_encode("ok"); //sucessfully inserted
        } 
    

    and

       if(empty($iname)){
            echo json_encode('1'); //check if iten name is empty
       }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧