这是我的脚本:
<script>
$('form.subscribe').on('submit', function() {
var that = $(this);
url = that.attr('action');
method = that.attr('method');
data ={};
that.find('[name]').each(function(index, value) {
var that = $(this),
name = that.attr('name'),
value = that.val();
data[name] = value;
});
$.ajax({
url: url,
type:method,
data: data,
success: function(response){
console.log(response);
}
});
return false;
});
</script>
我有一个 ajax 函数,它会根据您在表单上输入的内容向我提供错误消息或成功消息。
我收到的消息来自这个 php 脚本:
<?php
header("Refresh:7; url=contact.php");
$email = $_POST['subscribefield'];
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo "Dit emailadres klopt niet";
die();
}
$to = "flash1996mph@hotmail.com";
$subject = "Abonee voor de nieuwsbrief";
$body = "$email \n Heeft zich aangemeld voor de nieuwsbrief";
mail($to, $subject, $body);
echo "U heeft zich zojuist aangemeld voor de vandenberg nieuwsbrief";
?>
现在我用 console.log()
显示输出.但我想在 <div>
中显示这些内容相反。
最佳答案
你只需要使用 jQuery .html()在 DIV 中设置响应内容。
替换下一行
console.log(response);
与
$('#idOfDiv').html(response);
//or
$('.classOfDiv').html(response);
关于javascript - div 中的 Php 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37722805/