无参数通过Jquery调用PHP
我是jquery和ajax的不速之客,所以请容忍我:) 我想调用一个php脚本,在单击按钮时发送电子邮件。我不需要向PHP页面传递任何数据。下面是我的jquery代码:无参数通过Jquery调用PHP,php,jquery,Php,Jquery,我是jquery和ajax的不速之客,所以请容忍我:) 我想调用一个php脚本,在单击按钮时发送电子邮件。我不需要向PHP页面传递任何数据。下面是我的jquery代码: <script> $(document).ready(function(){ $("#flagbutton").click(function(){ // call php script here $("#div1").hide(); $("#span2").text("Thank
<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>
$(文档).ready(函数(){
$(“#flagbutton”)。单击(函数(){
//在这里调用php脚本
$(“#div1”).hide();
$(“#span2”).text(“谢谢点击!”);
});
});
点击按钮
按钮
下面是我的php代码:
<?php
$to = "somebody@somebody.com";
$subject = "Subject";
$message = $_SERVER['HTTP_REFERER'];
$headers = "From: test@somebody.com";
mail($to,$subject,$message,$headers);
?>
在哪里以及如何调用php脚本?
谢谢 使用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!");
});
});
$(文档).ready(函数(){
$(“#flagbutton”)。单击(函数(){
$.ajax({
url:您的url,
beforeSend:function(){
//你可以展示一些正在加载的东西
},
完成:函数(){
//把装载的东西移到这里
},
成功:功能(响应){
$(“#div1”).hide();
$(“#span2”).text(“谢谢点击!”);
},
错误:函数(xhr、ajaxOptions、thrownError){
警报(thrownError+“\r\n”+xhr.statusText+“\r\n”+xhr.responseText);
}
});
});
});
您不需要jquery和ajax,只需要一个简单的表单和一个重定向即可。很抱歉延迟。这是我关于StackOverflow的第一篇帖子:)
$.post("path_to_php",{}).done(function(data)
{
//ajax completed, the variable data will return which the PHP will echo out.
});
<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>