使用php传递隐藏的输入值

时间:2013-06-24 14:59:23

标签: php forms

当我点击“提交”时,代码应显示函数中的值。以下是代码:

  <form method="post">
  <input type="hidden" name="HDN_FormClicked" value= <?php echo $clicked ?> />
   <?php 
   if($_POST){
     $clicked= "You have clicked the button";}
     ?>
  <input class="button" type="submit"/>
  </form>

我是否需要使用$ _get来使代码生效?

4 个答案:

答案 0 :(得分:7)

<?php 
    if(isset($_POST['submit_button']))
       $clicked = 'You have clicked the button';

?>

<form method="post">
<input type="hidden" name="HDN_FormClicked" value="<?php echo (isset($clicked)) ?  $clicked : '' ?>" />
<input class="button" name="submit_button" type="submit"/>
</form>

替代

<?php 
    $clicked = '';

    if(isset($_POST['submit_button'])) 
       $clicked = 'You have clicked the button'; 
?>

<form method="post">
<input type="hidden" name="HDN_FormClicked" value="<?= $clicked?>" />
<input class="button" name="submit_button" type="submit"/>
</form>

答案 1 :(得分:0)

你的代码中有什么看起来像是将javascript与php混合......

如果要将表单中的值传递给PHP,可以使用:

<form action="phpfile.php" method="post">

在您的php文件中,您可以使用$_POST“使用”这些值。

示例:

<form action="http://somesite.com/prog/adduser" method="post">
<input type="text" name="info_to_get_1" value="" />
<input type="text" name="info_to_get_2" value="" />
<input type="submit" value="Send">

并在你的php文件中:

$value_1 = $_POST["info_to_get_1"];
$value_2 = $_POST["info_to_get_2"];

在您的情况下,如果您想要获得用户点击的信息,您应该写下这样的示例:

if(isset($_POST["HDN_FormClicked"])){$clicked= "You have clicked the button";}

答案 2 :(得分:0)

Pluto,你的问题是你在实际定义之前尝试使用变量$ clicked。为什么要通过隐藏元素的value属性传递php代码?你的方法似乎令人费解。阅读有关创建表单和发布表单的标准过程。

答案 3 :(得分:0)

这是表单提交的一个小例子:

<form method="POST" action="/form.php" name="myForm">
    <input type="hidden" name="myHiddenValue" value="<?php echo $clicked ?>" />
    <input type="text" placeholder="Type in some text" name="myText" value="" />
    <button name="mySubmit" type="submit">Submit the form!</button>
</form>

<?php
    $clicked = "not_clicked";
    if ($_POST) {
        if (isset($_POST['myForm']) && isset($_POST['mySubmit'])) {
            $clicked = "clicked";
            var_dump($_POST); // dumps your $_POST array.
        }
    }
?>

说明:

使用method属性可以更改请求方法。在这种情况下POST,但你可以有GET action属性设置表单数据发送的位置。
输入中的隐藏类型隐藏输入 使用name属性,您可以“命名”表单和表单字段。