2013-08-29 77 views
0

大家好,我试图在已创建的div中显示ajax响应,但我无法显示。你能帮我解决我错在哪里吗?无法在已创建的div标签中显示ajax响应

<form id="coupon" action=""> 
    <label>Have a Coupon?</label> 
    <input name="couponcode" id="couponcode" value="" /> 
    <input type="submit" value="Apply" /> 
</form> 
<div id="result"></div> 

现在我jQuery的功能是下面一个。

$("#coupon").submit(function (event) { 

    /* Stop form from submitting normally */ 
    event.preventDefault(); 

    /* Clear result div*/ 
    $("#result").html(''); 

    /* Get some values from elements on the page: */ 
    var values = $(this).serialize(); 

    /* Send the data using post and put the results in a div */ 
    $.ajax({ 
     url: "coupon.php", 
     type: "post", 
     data: values, 
     success: function (response) { 
      $('#result').html(response); 
     } 
    }); 
}); 

我的coupon.php文件代码在1以下。

<?php 
if (!empty($_POST)) { 

    $coupon = $_POST['couponcode']; 

    //json_encode(array('coupon' => $coupon)); 

    echo $coupon; 
} 
?> 
+0

认沽警报,并检查响应为空或返回任何价值 – Gautam3164

+0

'$(“#优惠券”),HTML(。 ..''你不是说'$('#result')。html(...' – blue112

+0

没有什么当我成功提醒 – user2362946

回答

0

该代码需要连接写入到div的标记中的响应。

$.ajax({ 
    url: "coupon.php", 
    type: "post", 
    data: values, 
    success: function (response) { 
     $('#result').html("<div id='message'>"+ response+"</div>"); 
    } 
}); 
+0

这是我的错误我编辑,但仍然没有用 – user2362946

+0

如果您通过浏览器打开url'coupon.php',会发生什么情况? –

+0

没有输出 – user2362946

0

尝试使用

$.ajax({ 
    url: "coupon.php", 
    type: "post", 
    data: values, 
    success: function (response) { 
    $('#result').text(response); 
    } 
}); 
+0

我已经在使用那个只有 – user2362946

+0

什么是'a lert(回应)'? –

+0

当我保持警戒声明时没有输出 – user2362946

0

默认方法表单提交的GET,但你通过邮寄值。添加方法=后形成标签

<form id="coupon" action=""> 
    <label>Have a Coupon?</label> 
    <input name="couponcode" id="couponcode" value="" /> 
    <input type="submit" value="Apply" /> 
</form> 
<div id="result"></div> 
<script language="javascript" type="text/javascript" src="jquery-1.8.3.min.js"></script> 
<script> 
jQuery(document).ready(function($) { 
$("#coupon").submit(function (event) { 

    /* Stop form from submitting normally */ 
    event.preventDefault(); 

    /* Clear result div*/ 
    $("#result").html(''); 

    /* Get some values from elements on the page: */ 
    var values = $(this).serialize(); 

    /* Send the data using post and put the results in a div */ 

    $.ajax({ 
     url: "coupon.php", 
     type: "post", 
     data: values, 
     success: function (response) { 
      $('#result').html(response); 
     } 
    }); 
}); 
}); 
</script> 

其工作正常,当我试图在成功这