【问题标题】:How to call upon a PHP function on button click?如何在按钮单击时调用 PHP 函数?
【发布时间】:2013-02-27 07:56:04
【问题描述】:

我正在尝试设置一个将 Steam 用户 ID 转换为身份验证 ID 的网站。它会要求访问者输入他们的常规 Steam ID,然后点击按钮将其转换为身份验证 ID。 Steam 为我们提供了从一种类型到另一种类型的 ID 转换功能。

转换 ID 的 Steam 功能:

function convert_steamid_to_accountid($steamid) 
     { 
        $toks = explode(":", $steamid); 
        $odd = (int)$toks[1];   
        $halfAID = (int)$toks[2]; 

        $authid = ($halfAID*2) + $odd;
        echo $authid;
      }

下面是我尝试设置一个基本的 HTML 页面,该页面获取用户输入,然后使用该函数将该输入转换为其他内容。

 <INPUT TYPE = "Text" VALUE ="ENTER STEAM:ID" NAME = "idform">

<?PHP
$_POST['idform'];
$steamid = $_POST['idform'];
?>

另外,这是默认 Steam 用户 ID 的样子:

STEAM_0:1:36716545

感谢大家的帮助!

【问题讨论】:

    标签: php function textbox steam


    【解决方案1】:

    如果你可以把它分成两个单独的文件,那就这样做吧。

    foo.html

    <form method="POST" action="foo.php">
      <input type="text" value="ENTER STEAM:ID" name="idform" />
      <input type="submit" />
    </form>
    

    foo.php

    <?php
      function convert_steamid_to_accountid($steamid) 
      { 
        $toks = explode(":", $steamid); 
        $odd = (int)$toks[1];   
        $halfAID = (int)$toks[2]; 
    
        $authid = ($halfAID*2) + $odd;
        echo $authid;
      }
    
      $id = $_POST['idform'];
      convert_steamid_to_accountid($id)
    ?>
    

    如果您没有制作两个单独文件的选项,您可以将 php 代码添加到 'foo.html' 文件并让表单提交到同一个文件。但是,如果您这样做,请在调用 convert_steamid_to_accountid() 函数之前检查文件是否是第一次被请求,或者是因为提交了表单而被请求。 你可以这样做:

    if ($_SERVER['REQUEST_METHOD']=='POST'){
      // your php code here that should be executed when the form is submitted.
    }
    

    【讨论】:

    • 哇!谢谢!!它工作得很好。制作两个单独的文件而不是 1 个文件的原因是什么?
    • 嗯,这在效率方面并不重要,但在我看来,它使代码更清晰、更有条理、更易于理解。这样,您就不必对请求方法和其他内容进行额外检查。
    • 顺便说一句,如果这个答案对你有帮助,你可以接受这个答案。这样,好吧,我将获得声望点:)
    猜你喜欢
    • 2014-01-11
    • 1970-01-01
    • 2014-11-01
    • 2015-02-27
    • 2020-12-23
    • 1970-01-01
    • 2013-02-26
    • 1970-01-01
    • 2020-07-29
    相关资源
    最近更新 更多