简体   繁体   English

如何从单击链接时调用的JS调用PHP function?

[英]How to call a PHP function from JS that is invoked when clicking a link?

I have this test page:我有这个测试页:

http://www.comehike.com/hikes/uncancel_hike.php?hike_id=30 http://www.comehike.com/hikes/uncancel_hike.php?hike_id=30

When clicking "yes" I am able to get the JavaScript function invoked named uncancelHike, but I don't really know how to call the PHP function. When clicking "yes" I am able to get the JavaScript function invoked named uncancelHike, but I don't really know how to call the PHP function.

I do import a php script with a function named uncancelHike() but I am not certain how to call it from that Javascript.我确实导入了一个 php 脚本和一个名为 uncancelHike() 的 function 但我不确定如何从该 Javascript 调用它。

I do use YUI in the code, but not sure if I should be using it here, or this is something much simpler.我确实在代码中使用了 YUI,但不确定我是否应该在这里使用它,或者这更简单。

Ajax is not the answer of every thing:D Ajax 并不是所有事情的答案:D

<?php

    if(isset($_POST['but1'])){
        t1();
    }elseif(isset($_POST['but2'])){
        t2();
    }


    function t1()
    {
       echo "The t1 function is called.";
    }
    function t2()
    {
       echo "The t2 function is called.";
    }

?>

<html>
<head>
<title>Test</title>
</head>
<body>
<form action="test.php" method="POST">
<input type="submit" value="Click1" name="but1">
<input type="submit" value="Click2" name="but2">
</form>
</body>
</html>

You need to use AJAX here, this tutorial should helpful for you:您需要在这里使用 AJAX,本教程应该对您有所帮助:

Beginning AJAX Using The YUI Library 使用 YUI 库开始 AJAX

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM