我是jquery和ajax的菜鸟,所以请耐心等待:) 我想调用一个php脚本,在单击按钮时发送一封电子邮件。我不需要将任何数据传递给PHP页面。下面是我的jquery代码:
<script>
$(document).ready(function(){
$("#flagbutton").click(function(){
// call php script here
$("#div1").hide();
$("#span2").text("Thanks for clicking!");
});
});
</script>
<div style="float:right" id="div1"><span id="span1">Click the button</span>
<button id="flagbutton">The button</button></div>
<span style="float:right" id="span2"></span>
以下是我的PHP代码:
<?php
$to = "somebody@somebody.com";
$subject = "Subject";
$message = $_SERVER['HTTP_REFERER'];
$headers = "From: test@somebody.com";
mail($to,$subject,$message,$headers);
?>
我在哪里以及如何调用php脚本? 谢谢!
答案 0 :(得分:1)
$.post("path_to_php",{}).done(function(data)
{
//ajax completed, the variable data will return which the PHP will echo out.
});
答案 1 :(得分:0)
使用AJAX,您可以实现您想要的目标。下面的代码完全正确!
$("#flagbutton").click(function(){
$.post("php.php", function(){
$("#div1").hide();
$("#span2").text("Thanks for clicking!");
});
});
或者,您可以这样做:
$("#flagbutton").click(function(){
$.post("php.php").done(function(){
$("#div1").hide();
$("#span2").text("Thanks for clicking!");
});
});
答案 2 :(得分:0)
<script>
$(document).ready(function(){
$("#flagbutton").click(function(){
$.ajax({
url: your_url,
beforeSend: function() {
// you can show some loading things
},
complete: function() {
//remove the loading things here
},
success: function(response) {
$("#div1").hide();
$("#span2").text("Thanks for clicking!");
},
error: function(xhr, ajaxOptions, thrownError) {
alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
}
});
});
});
</script>