【问题标题】:Return a php value from a form without ajax从没有ajax的表单返回一个php值
【发布时间】:2019-01-26 17:19:32
【问题描述】:

这可能是一个初学者的问题,但我真的很挣扎。我想在我的一个页面中放置一个 html 按钮来触发一个功能。 但为了做到这一点,我还需要我的页面中的一个 php 值从按钮返回到函数。

我不想为此使用 Ajax。

//first file
if(isset($_POST['button']))
{
    theFunction($i);
}
require_once 'file.php';


<?php
//second file
$i = 4; ?>
<form method="POST">
   <input type="submit" name="button"  value="buttonvalue">
</form>

【问题讨论】:

    标签: php html forms button


    【解决方案1】:

    您可以通过隐藏的表单字段发送值。

    ...
    //first file
    if(isset($_POST['button'])){
        $i=$_POST['i']; //<---
        theFunction($i);
    }
    require_once 'file.php';
    
    //second file
    $i=4;
    ?>
    
    <form method="POST">
      <input type="hidden" name="i" value="<?php echo $i; ?>"> 
      <input type="submit" name="button"  value="buttonvalue">
    </form>
    

    【讨论】:

      【解决方案2】:
      //first file
      if(isset($_POST['button']))
      {
          if(isset($_POST['INPUT_NAME']) && !empty($_POST['INPUT_NAME']))
          {
              theFunction($_POST['INPUT_NAME']);
          }
      }
      require_once 'file.php';
      
      
      <?php
      //second file
      <form method="POST">
         <input type="hidden" name="INPUT_NAME" value="YOUR_VALUE_eg-4" requiered/>
         <input type="submit" name="button"  value="buttonvalue">
      </form>
      

      此方法不安全。 您只能使用简单的 html 链接,例如 &lt;a href="first.php/?i=4"&gt;TEXT&lt;/a&gt; ou 如果你做一个好的路由器,你可以使用类似&lt;a href="first.php/4/"&gt;TEXT&lt;/a&gt;

      【讨论】:

        猜你喜欢
        • 2018-03-10
        • 2014-06-26
        • 2016-08-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-14
        • 1970-01-01
        相关资源
        最近更新 更多