douping7105
2016-06-28 12:49
浏览 244
已采纳

为Ajax调用返回的数据

I'm trying to return Data for Ajax, but I'm still not clear about something :
My Form :

<form id="checkitem" class="form-horizontal">
 <input type="text" name="item1"><br><br>
 <input type="submit" value="Submit">
</form>

Ajax call :

<script>
$(document).ready(function(){
    $('#checkitem').submit(function(){
        $.ajax({
            type: 'POST',
            url: 'operations.php?r=checkitem', 
            data: $(this).serialize()
        });    
     });
}); 
</script>

My PHP code (For operations.php?r=checkitem) :

$item1 = mysql_real_escape_string($_POST['item1']);
include ("data.php"); 
$query = mysql_query("INSERT INTO Itemlist (item1, ins_date) VALUES ('$item1',now()");
if (!$query)
{
    die('Invalid query: ' . mysql_error());
}
echo "Item1 has been added";

How to return echo value , or any other php variable from the page operations.php

EDIT :
As per your suggestion guys I should use success function :

 success : function(response){
             alert(response); //Do whatever you want to do with this

My question is , What is response ? is it all what has been echoed from other page ? or the HTML code of the other page ?

Thnx

图片转代码服务由CSDN问答提供 功能建议

我正在尝试返回Ajax数据,但我仍然不清楚某些事情:
\ nMy表格:

 &lt; form id =“checkitem”class =“form-horizo​​ntal”&gt; 
&lt; input type =“text”name =“  item1“&gt;&lt; br&gt;&lt; br&gt; 
&lt; input type =”submit“value =”提交“&gt; 
&lt; / form&gt; 
   
 
 <  p> Ajax调用:
 &lt; script&gt; 
 $(document).ready(function(){
 $('#checkitem')。submit  (function(){
 $ .ajax({
 type:'POST',
 url:'operations.php?r = checkitem',
 data:$(this).serialize()
})  ; 
}); 
});  
&lt; / script&gt; 
   
 
 

我的PHP代码(对于operations.php?r = checkitem):

  $ item1 = mysql_real_escape_string($ _ POST ['item1']); 
include(“data.php”);  
 $ query = mysql_query(“INSERT INTO Itemlist(item1,ins_date)VALUES('$ item1',now()”); 
if(!$ query)
 {
 die('无效查询:'。mysql_error  ()); 
} 
echo“已添加Item1”; 
   
 
 

如何从页面operations.php返回echo值或任何其他php变量

编辑:
根据您的建议,我应该使用成功功能:

  success  :function(response){
 alert(response); //做你想做的事情
   
 
 

我的问题是,什么是响应?是吗? 所有从其他页面回显的内容?或其他页面的HTML代码?

Thnx

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dtotuki47568 2016-06-28 12:54
    已采纳

    You can do it like this.

    AJAX has various callback functions, success is one of them. You can use others like error, complete.

    success will be called when AJAX request successfully executed. This will provide use response text which we echo or print in our PHP page.

    <script>
    $(document).ready(function(){
       $('#checkitem').submit(function(){
         $.ajax({
             type: 'POST',
             url: 'operations.php?r=checkitem', 
             data: $(this).serialize(),
             success : function(response){
                 alert(response); //Do whatever you want to do with this
             }
        })    
       });
     }); 
    </script>
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dtqie02844 2016-06-28 12:53

    the ajax function of jquery has a success callback function, you can retrieve the data from the ajax page you called :

    $.ajax({
        type: 'POST',
        url: 'operations.php?r=checkitem', 
        data: $(this).serialize(),
        success: function(result) {
            console.log(result);
            // do what you want with the result here
            // if it's html you could do $('.elem').html(result);
        }
    })
    

    The better is to specify which kind of data you're expecting with the type key : (type: 'json') or (type: 'text/html) for example. And in your php script, send headers in consequence !

    评论
    解决 无用
    打赏 举报
  • douhuang75397 2016-06-28 12:56

    or use deferred way

    $.ajax({
        type: 'POST',
        url: 'operations.php?r=checkitem', 
        data: $(this).serialize()})
    .done(function(result){
        console.log(result);
    })
    .fail(function(error){
       console.log(error);
    });
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题