doupao6698 2014-03-23 12:47
浏览 24
已采纳

通过Jquery调用PHP而不带参数

I'm a noob to jquery and ajax, so please bear with me :) I want to call a php script that sends out an email when a button is clicked. I do not need to pass any data to the PHP page. Below is my jquery code:

<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>

Below is my php code:

<?php
    $to = "somebody@somebody.com";
    $subject = "Subject";
    $message = $_SERVER['HTTP_REFERER'];
    $headers = "From: test@somebody.com";
    mail($to,$subject,$message,$headers);
?>

Where and how do I call the php script? Thanks!

  • 写回答

3条回答 默认 最新

  • dqqxkq4047 2014-03-23 12:49
    关注

    Using AJAX, you can do what you wanna achieve. The code below does it exactly!

      $("#flagbutton").click(function(){  
         $.post("php.php", function(){
           $("#div1").hide();
           $("#span2").text("Thanks for clicking!");
         });
      });
    

    Or, you can do this way:

      $("#flagbutton").click(function(){  
         $.post("php.php").done(function(){
           $("#div1").hide();
           $("#span2").text("Thanks for clicking!");
         });
      });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 正弦信号发生器串并联电路电阻无法保持同步怎么办
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序