Php 无法在已创建的div标记中显示ajax响应
大家好,我正试图在已经创建的div中显示ajax响应,但我无法显示。你能帮我找出哪里做错了吗Php 无法在已创建的div标记中显示ajax响应,php,jquery,ajax,Php,Jquery,Ajax,大家好,我正试图在已经创建的div中显示ajax响应,但我无法显示。你能帮我找出哪里做错了吗 <form id="coupon" action=""> <label>Have a Coupon?</label> <input name="couponcode" id="couponcode" value="" /> <input type="submit" value="Apply" /> </form&g
<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>
我的优惠券.php
文件代码低于1
$("#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);
}
});
});
<?php
if (!empty($_POST)) {
$coupon = $_POST['couponcode'];
//json_encode(array('coupon' => $coupon));
echo $coupon;
}
?>
代码需要将写入
div的标记中的响应串联起来
$.ajax({
url: "coupon.php",
type: "post",
data: values,
success: function (response) {
$('#result').html("<div id='message'>"+ response+"</div>");
}
});
$.ajax({
url:“优惠券.php”,
类型:“post”,
数据:价值观,
成功:功能(响应){
$('#result').html(“+response+”);
}
});
尝试使用
$.ajax({
url: "coupon.php",
type: "post",
data: values,
success: function (response) {
$('#result').text(response);
}
});
表单提交的默认方法是GET,但您通过post发送值。Add method=post to form标记
<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>
有优惠券吗?
jQuery(文档).ready(函数($){
$(“#优惠券”)。提交(功能(活动){
/*阻止表单正常提交*/
event.preventDefault();
/*清除结果分区*/
$(“#结果”).html(“”);
/*从页面上的元素获取一些值:*/
var values=$(this.serialize();
/*使用post发送数据,并将结果放入div中*/
$.ajax({
url:“优惠券.php”,
类型:“post”,
数据:价值观,
成功:功能(响应){
$('#result').html(响应);
}
});
});
});
当我尝试此操作时,它可以正常工作将警报放入成功,并检查响应是否为空或返回任何值$(“#优惠券”).html(…
您不是指$(“#结果”).html吗(…
当我成功发出警报时什么都没有是的如果你通过浏览器点击url优惠券.php
会发生什么?警报(响应)
返回什么?