dongzhizhai4070 2015-10-08 18:50 采纳率: 0%
浏览 251

如何获取ajax表单提交返回值

I use ajax to submit a form, however when i finished the whole process, it stopped at the api.php and did get back to the ajax success. Here the code:

<form method="POST" id="form_1" action="../api.php" enctype="multipart/form-data" novalidate="novalidate">
  <input type="text" name="name" id="name" value="Amy" readonly="readonly">
  <input id="fileBox" class="fileUpload" type="file" name="img" accept="image/*" required>
  <input type="hidden" name="isSubmit" value="1"/>
</form>

<a id="btnSubmit" href="javascript:void(0)">Submit</a>
$("#form_1").submit(function(){
  var formData = new FormData(($this)[0]);
  $ajax({
    type: "POST",
    url: "../../api.php",
    data: { action:"formSubmit", formData:formData},
    processData: false,
    contentType: false,
    success: function(){
      console.log('success return');
    }
  })
});

$("#btnSubmit").click(function(){
  if($("#form_1").valid()){ 
    // inside click event handler you trigger form submission
    $("#form_1").trigger('submit');
  }
});

in my api.php

if($action=='formSubmit'){
  $name = $_POST['name'];
  if(createUser($name)){
    return true;
  }else{
    return false;
  }
}

I checked the database , record is successfully created. But I can't get the console in success. The url show on the browser there is the path of the api.php. Why I stopped there can't get the return?

Thanks for your help!!

I finallly did it !I am missing "return false" after the ajax; But actually I dont really know the function of return false;

$("#form_1").submit(function(){
var formData = new FormData((this)[0]);
$.ajax({
type: "POST",
url: "../../api.php",
data: { action: "formSubmit",  formData: formData},
processData:false,
contentType:false,
success: function(data){
$("#thankyou").show();
}
})
return false;  
});
  • 写回答

2条回答 默认 最新

  • dongxiaoying5882 2015-10-08 18:53
    关注

    javascript doesn't recognize return value from php. Use echo instead.

    if($action=='formSubmit'){
        $name = $_POST['name'];
        if(createUser($name)){
           echo 'true';
        }else{
           echo 'false';
        }
    }
    

    If you want to see actual output coming from api.php then use below code in your ajax.

    success: function(res){
       console.log(res);
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥20 Python安装cvxpy库出问题
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题