【问题标题】:How to call a php function on a html text click?如何在 html 文本点击上调用 php 函数?
【发布时间】:2017-11-21 07:27:53
【问题描述】:

我想调用php函数,同时调用该函数我需要传递参数,当点击一些文本时,它必须调用名为test()的方法; 如何做到这一点。

我对 ajax 一无所知。我已使用此代码进行尝试。尝试我使用了简单的代码,但它也无法正常工作。

这是 Ajax.html 文件

<!DOCTYPE html>
<html>
<head>
<body>
  <p>Click Me</p>

<script>
    $(document).ready(function() {
    $("p").click(function(){
    $.ajax({
    url:"script.php", //the page containing php script
    type: "POST", //request type
    success:function(result){
    alert(result);
    }
  });
});
})
</script>
</body>
</head>
</html>

script.php 包含,

<?php 
function test(){
  echo "Hello";
  }
 ?>

当用户单击某些文本时如何调用 php 函数。我必须调用该 test() 方法。谁能帮帮我?

【问题讨论】:

标签: javascript php jquery html ajax


【解决方案1】:

页面调用时需要调用

 <?php 
  test()//mind it
  function test(){
    echo "Hello";
  }
 ?>

或使用任何参数,如动作名称

 <?php 
  if(isset($_GET['action']) && $_GET['action']==1){
      test()//mind it
  }else{
      otherFunc()//mind it
  }
  function test(){
    echo "Hello";
  }
 ?>

在 url 中设置动作

url:"script.php?action=1",

【讨论】:

  • 当点击“点击我”时,它没有调用 test() 方法。
  • 没有任何效果。给我一些建议。
  • 您遇到了什么错误?在控制台或网络选项卡中?
  • 我收到任何错误。它只是打印 Click。就是这样。
  • url:"script.php?action=1", .这个我试过了,还是不行。
猜你喜欢
  • 2015-02-27
  • 1970-01-01
  • 2015-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-31
  • 2012-01-29
相关资源
最近更新 更多