【问题标题】:Run PHP function on html button click在 html 按钮单击上运行 PHP 函数
【发布时间】:2013-04-04 04:25:04
【问题描述】:

单击按钮时,我需要运行一些 PHP 函数。我知道这不应该是 php 的用途,而 js 应该这样做,但是当用户询问时,我的函数在从服务器收集数据时正在做什么。具体来说,它获取一些用户数据并将其写入文件,用户应决定将收集哪些数据。
我怎样才能做到这一点?我看到了Run PHP File On Button Click 的帖子,但我仍然不知道如何使用它。
我正在学习,所以请不要太苛刻

我尝试了onclick() 和各种各样的东西,但它没有带来任何有用的东西

【问题讨论】:

  • 请详细描述,甚至是伪代码,你想做什么,你在想什么样的按钮,你尝试了什么,你在哪里失败,你想要什么样的过程运行,为什么它不起作用?
  • 专门研究 AJAX。这是一篇很有帮助的文章,我发现:net.tutsplus.com/tutorials/javascript-ajax/…
  • @markus 例如,我有一个 php 函数 getvideos() 它将 urls 返回到用户的视频,我想在用户单击按钮时调用它
  • 为什么不直接在用户单击该按钮时加载视频页面并在那里显示链接?
  • 我不想加载视频,我想将视频 url 存储在一个文件中

标签: javascript php html ajax button


【解决方案1】:

每当您通过 HTTP 请求(GET、POST、PUT)访问 php 文件时都会运行它。

您可以使用 JQuery/Ajax 在单击按钮时发送请求,甚至只需更改浏览器的 URL 以导航到 php 地址。

根据在 POST/GET 中发送的数据,您可以让 switch 语句运行不同的功能。

通过 GET 指定函数

您可以使用此处的代码:How to call PHP function from string stored in a Variable 以及 switch 语句来根据发送的数据自动调用适当的函数。

所以在 PHP 方面你可以有这样的东西:

<?php

//see http://php.net/manual/en/function.call-user-func-array.php how to use extensively
if(isset($_GET['runFunction']) && function_exists($_GET['runFunction']))
call_user_func($_GET['runFunction']);
else
echo "Function not found or wrong input";

function test()
{
echo("test");
}

function hello()
{
echo("hello");
}

?>

你可以用地址栏做最简单的get请求作为测试:

http://127.0.0.1/test.php?runFunction=hellodddddd

结果:

Function not found or wrong input

http://127.0.0.1/test.php?runFunction=hello

结果:

hello

发送数据

通过 JQuery 获取请求

见:http://api.jquery.com/jQuery.get/

$.get("test.cgi", { name: "John"})
.done(function(data) {
  alert("Data Loaded: " + data);
});

通过 JQuery 的 POST 请求

见:http://api.jquery.com/jQuery.post/

$.post("test.php", { name: "John"} );

通过 Javascript 位置获取请求

见:http://www.javascripter.net/faq/buttonli.htm

<input type=button 
value="insert button text here"
onClick="self.location='Your_URL_here.php?name=hello'">

读取数据 (PHP)

请参阅 PHP Turotial 阅读帖子并获取:http://www.tizag.com/phpT/postget.php

有用的链接

http://php.net/manual/en/function.call-user-func.php http://php.net/manual/en/function.function-exists.php

【讨论】:

  • 是否总是严格需要像`test.php 这样的文件。问题是我有大约 20 个 php 函数,如果有任何方法可以从具有按钮的同一个文件中调用函数,那将有很大帮助。如果这不可能,我会按照你的建议去做。
  • 有可能,您可以使用函数 call_user_func($functionName) 来根据来自 GET/POST 的变量调用特定函数。我正在更新我的帖子以添加一个示例。
  • 任何其他提示或者看起来还可以吗? :)
  • 非常感谢。对于刚接触 Web 开发的人来说,有很多新东西 :) 非常感谢,你让我摆脱了麻烦!
  • 好的,对于您的情况,我建议使用 GET 和 HTML 按钮是最简单的。如果你想做更高级的,你可以让你的函数在 PHP 中回显一个回复,并使用示例 GET Request via JQuery 在警报中显示回复。
【解决方案2】:

如果你想发出服务器请求,你应该使用 AJAX,这样你就可以将你想要的参数发送到服务器,它可以使用这些参数运行你想要的任何 php。

纯javascript示例:

<input type="text" id="name" value="..."/>
<input type="text" id="location" value="..."/>
<input type="button" onclick="ajaxFunction();" value="Submit" />
<div id="ajaxDiv"></div>
<script type="text/javascript">    
    function ajaxFunction(){
        var ajaxRequest;  // The variable that makes Ajax possible!

        try{
            // Opera 8.0+, Firefox, Safari
            ajaxRequest = new XMLHttpRequest();
        } catch (e){
            // Internet Explorer Browsers
            try{
                ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try{
                    ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e){
                    // Something went wrong
                    alert("Your browser broke!");
                    return false;
                }
            }
        }
        // Create a function that will receive data sent from the server
        ajaxRequest.onreadystatechange = function(){
            if(ajaxRequest.readyState == 4){
                var ajaxDisplay = document.getElementById('ajaxDiv');
                ajaxDisplay.innerHTML = ajaxRequest.responseText;
            }
        }
        var name = document.getElementById('name').value;
        var location = document.getElementById('location').value;
        var queryString = "?name=" + name + "&location=" + location;
        ajaxRequest.open("POST", "some.php" + queryString, true);
        ajaxRequest.send(null); 
    }
</script>

jQuery Ajax 示例: http://api.jquery.com/jQuery.ajax/

$.ajax({
  type: "POST",
  url: "some.php",
  data: { name: "John", location: "Boston" }
}).done(function( msg ) {
  alert( "Data Saved: " + msg );
});

您可以拥有一个文件,其中包含函数,例如functions.php

functions.php

<?php
  myFunction($Name, $Location) {
      // etc...
  }
  myFunction2() {
  }
  // ... many functions
?>

some.php

<?php include("functions.php");    
$Name = $_POST['name'];
$Location = $_POST['location'];

myFunction($Name, $Location);

// make what you want with these variables...?>

【讨论】:

  • 我可以在同一个文件中调用一个函数,还是需要制作一个 some.php 文件?
  • 你必须有控制器。所以在你的情况下 some.php 是动作控制器。它可以根据您的需要调用其他功能。
  • 我问是因为我有很多函数——大约 20 个,而且我真的不想为每个函数创建 20 个 php 文件。如果需要,我会
  • 但是我仍然不能有两个函数 myFunction1($Name, $Location) myFunction2($Name, $Location) 并在同一个文件中使用不同的参数调用这两个函数。假设我想让第一个函数说“嘿”,“$Name”“来自”$Location,第二个说“Bye”,“$Name”“来自”$Location(我知道语法不正确,但你明白了“首先应该为波士顿的 John 调用,为伦敦的 Jill 调用第二个。如果可能,不要为每个函数创建文件
  • 你可以。当您在 some.php 文件中使用 include("functions.php") 时,您可以访问这些函数。
【解决方案3】:

使用ajax,一个简单的例子,

HTML

<button id="button">Get Data</button>

Javascript

var button = document.getElementById("button");

button.addEventListener("click" ajaxFunction, false);

var ajaxFunction = function () {
    // ajax code here
}

或者查看jquery ajax http://api.jquery.com/jQuery.ajax/

【讨论】:

    【解决方案4】:
    <?php
    if (isset($_POST['str'])){
    function printme($str){
    echo $str;
    }
    
    printme("{$_POST['str']}");
    }
    ?>
    <form action="<?php $_PHP_SELF ?>" method="POST">
    <input type="text" name="str" /> <input type="submit" value="Submit"/>
    </form>
    

    【讨论】:

      【解决方案5】:

      在您的 PHP 文件上使用 AJAX 请求,然后在您的页面上显示结果,无需重新加载。

      http://api.jquery.com/load/ 如果您不需要任何 POST 数据,这是一个简单的解决方案。

      【讨论】:

        【解决方案6】:

        这取决于您要运行什么功能。如果您需要在服务器端完成某些操作,例如查询数据库或在会话中设置某些内容或在客户端无法完成的任何操作,则需要 AJAX,否则您可以使用 JavaScript 在客户端完成。 当你可以在客户端做你需要做的事情时,不要让服务器工作。

        jQuery 提供了一种简单的 ajax 方法:http://api.jquery.com/jQuery.ajax/

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-12-25
          • 2020-10-21
          • 1970-01-01
          • 2017-04-08
          • 1970-01-01
          • 1970-01-01
          • 2022-12-03
          • 1970-01-01
          相关资源
          最近更新 更多