【问题标题】:calling php function from submit button从提交按钮调用 php 函数
【发布时间】:2015-05-20 05:13:14
【问题描述】:

我想将输入字段address 的值传递给php。这样当我点击提交按钮时,它将存储在 php 中。

<input type="text" name="address">
<input type="submit" name="go" method="post">
<?php
    if($_POST){
    if(isset($_POST['go'])){
    call();
    }else{
    echo "Error";
    }
    }
    function call(){
    echo "hi";
    print(document.address.value);
    }
?>

但是当我点击按钮时它没有任何反应。

【问题讨论】:

  • 这是java脚本函数“document.address.value”;你必须把它写在脚本标签中
  • 使用 html 表单...阅读一下
  • 了解 HTML 和 Javascript 有什么不同,上面的事情你需要使用 ajax 来做
  • 使用表单发布并使用 $_POST['address'] 获取地址的值。正如@saty 所说 document.address.value 是JS函数。

标签: php html forms


【解决方案1】:

您正在混合使用 PHP 和 JavaScript。如果你想要一个纯 PHP 解决方案,你可以这样做:

<?php
if(isset($_POST['go'])){
     $address = $_POST['address'];
     echo $address;
}
?>

<form method="POST">
  <input type="text" name="address">
  <input type="submit" name="go" method="post">
</form>

使用 PHP 提交表单后,您可以使用 $_POST 访问表单值,因此请使用 $_POST['address'] 而不是 document.address.value

【讨论】:

    【解决方案2】:

    你打算做的可以按照下面的方法来完成。

    <form method="post" >
    
    <input type="text" name="address">
    <input type="submit" name="go">
    
    </form>
    
    
    
    <?php
        if($_POST){
        if(isset($_POST['go'])){
        call();
        }else{
        echo "Error";
        }
        }
        function call(){
        echo "hi";
        echo $_POST['address'];
        }
    ?>
    

    【讨论】:

    • @Loenix,是的。这是一个替代方案,但将其删除并进行了编辑。
    【解决方案3】:

    试试这样,这可以根据您的要求正常工作:

    <html>
    <body>
    <form action="" method="post">
    Address: <input type="text" name="name"><br>
    <input type="submit" name="go" value="call" />
    </form>
    
    <?php
    if (isset($_REQUEST['name'])) {
        call();
        }
    ?>
    <?php
    function call() {
     $address= $_POST['name'];
    echo "Address:".$address;
    exit;
    }
    

    输出:-

    用于输出Click here

    【讨论】:

      【解决方案4】:

      使用此代码

      <input type="text" name="address">
      <input type="submit" name="go" method="post">
      <?php
          if($_POST){
          if(isset($_POST['go'])){
          call();
          }else{
          echo "Error";
          }
      
          function call(){
          echo "hi";
          echo $_POST['address'];
          }
      ?>
      

      【讨论】:

        猜你喜欢
        • 2011-09-05
        • 1970-01-01
        • 2020-01-07
        • 1970-01-01
        • 2022-07-07
        • 1970-01-01
        • 2019-07-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多