使用 php 传递隐藏的输入值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17278645/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:37:16  来源:igfitidea点击:

passing hidden input value by using php

phpforms

提问by Pluto

The code should show the value in the function when I click "submit". Here is the code below:

当我单击“提交”时,代码应该显示函数中的值。这是下面的代码:

  <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>

Do I need to use $_get to make the code work?

我需要使用 $_get 来使代码工作吗?

回答by Kyslik

<?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>

alternative

选择

<?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>

回答by sinaneker

Here a small example of form submitting:

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

<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.
        }
    }
?>

Explanation:

解释:

With the method attribute you change the request method. In this case POST, but you can have GET.
The action attribute sets the location to where your form data will be sent.
The hidden type in the input hides the input.
With the name attribute you 'name' the form and the form fields.

使用 method 属性可以更改请求方法。在本例中为 POST,但您可以使用 GET。
action 属性将位置设置为您的表单数据将被发送到的位置。
输入中的隐藏类型隐藏输入。
使用 name 属性,您可以“命名”表单和表单字段。

回答by Sergio

What you have in your code looks like mixing javascript with php...

您的代码中的内容看起来像是将 javascript 与 php 混合...

If you want to pass values from your form to PHP you can use:

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

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

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

and on your php file you can "use" the values using $_POST.

在您的 php 文件中,您可以使用$_POST.

Example:

例子:

<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">

and in your php file:

并在您的 php 文件中:

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

In your case if you want to have info if user has clicked, you should write your example something like this:

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

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

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

回答by RutledgePaulV

Pluto, your issue is that you are trying to use the variable $clicked before it is actually defined. Why are you passing php code through the value attribute of a hidden element? Your approach seems convoluted. Read up on standard procedures for creating a form and posting it.

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