php 如何使用PHP获取输入字段值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13447554/
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 05:28:02  来源:igfitidea点击:

How to get input field value using PHP

php

提问by nasty

I have a input field as follows:

我有一个输入字段,如下所示:

<input type="text" name="subject" id="subject" value="Car Loan">

I would like to get the input fields value Car Loanand assign it to a session. How do I do this using PHP or jQuery?

我想获取输入字段值Car Loan并将其分配给会话。如何使用 PHP 或 jQuery 执行此操作?

回答by Jeremy John

Use PHP's $_POSTor $_GETsuperglobals to retrieve the value of the input tag via the name of the HTML tag.

使用 PHP$_POST$_GET超全局变量通过 HTML 标记的名称检索输入标记的值。

For Example, change the method in your form and then echo out the value by the name of the input:

例如,更改表单中的方法,然后按输入名称回显值:

Using $_GETmethod:

使用$_GET方法:

<form name="form" action="" method="get">
  <input type="text" name="subject" id="subject" value="Car Loan">
</form>

To show the value:

要显示值:

<?php echo $_GET['subject']; ?>

Using $_POSTmethod:

使用$_POST方法:

<form name="form" action="" method="post">
  <input type="text" name="subject" id="subject" value="Car Loan">
</form>

To show the value:

要显示值:

<?php echo $_POST['subject']; ?>

回答by Eric Leschinski

Example of using PHP to get a value from a form:

使用 PHP 从表单中获取值的示例:

Put this in foobar.php:

把它放在 foobar.php 中:

<html>
<body>
  <form action="foobar_submit.php" method="post">
    <input name="my_html_input_tag"  value="PILLS HERE"/>

    <input type="submit" name="my_form_submit_button" 
           value="Click here for penguins"/>

    </form>
</body>
</html>

Read the above code so you understand what it is doing:

阅读上面的代码,以便您了解它在做什么:

"foobar.php is an HTML document containing an HTML form. When the user presses the submit button inside the form, the form's action property is run: foobar_submit.php. The form will be submitted as a POST request. Inside the form is an input tag with the name "my_html_input_tag". It's default value is "PILLS HERE". That causes a text box to appear with text: 'PILLS HERE' on the browser. To the right is a submit button, when you click it, the browser url changes to foobar_submit.phpand the below code is run.

“foobar.php 是一个包含 HTML 表单的 HTML 文档。当用户按下表单内的提交按钮时,表单的 action 属性运行:foobar_submit.php。表单将作为 POST 请求提交。表单内部是一个 input 标签,带有名称“my_html_input_tag”。它的默认值是“PILLS HERE”。这会导致在浏览器上出现一个带有文本的文本框:“PILLS HERE”。右侧是一个提交按钮,当您单击它时,浏览器网址会发生变化tofoobar_submit.php并运行以下代码。

Put this code in foobar_submit.php in the same directory as foobar.php:

将此代码放在与 foobar.php 相同的目录中的 foobar_submit.php 中:

<?php
  echo $_POST['my_html_input_tag'];
  echo "<br><br>";
  print_r($_POST); 
?>

Read the above code so you know what its doing:

阅读上面的代码,你就知道它在做什么:

The HTML form from above populated the $_POST superglobal with key/value pairs representing the html elements inside the form. The echo prints out the value by key: 'my_html_input_tag'. If the key is found, which it is, its value is returned: "PILLS HERE".

上面的 HTML 表单使用表示表单内 html 元素的键/值对填充 $_POST 超全局变量。echo 通过键打印出值:'my_html_input_tag'。如果找到了键,则返回其值:“PILLS HERE”。

Then print_r prints out all the keys and values from $_POST so you can peek as to what else is in there.

然后 print_r 打印出 $_POST 中的所有键和值,以便您可以查看其中还有什么。

The value of the input tag with name=my_html_input_tagwas put into the $_POST and you retrieved it inside another PHP file.

带有 name= 的输入标签的值my_html_input_tag被放入 $_POST 并且您在另一个 PHP 文件中检索它。

回答by Akhilraj N S

You can get the value $valueas :

您可以获得以下值$value

$value =  $_POST['subject'];

or:

或者:

$value = $_GET['subject'];,depending upon the form method used.

$value = $_GET['subject'];,取决于所使用的形式方法。

session_start();
$_SESSION['subject'] =  $value;

the value is assigned to session variable subject.

该值被分配给会话变量主题。

回答by Somy A

For global use, you may use:

对于全球使用,您可以使用:

$val = $_REQUEST['subject'];

and to add yo your session, simply

并添加您的会话,只需

session_start();
$_SESSION['subject'] =  $val;

And you dont need jQuery in this case.

在这种情况下你不需要 jQuery。

回答by Fee

If its a get request use, $_GET['subject']or if its a post request use, $_POST['subject']

如果是获取请求使用,$_GET['subject']或者是发布请求使用,$_POST['subject']

回答by bhaveshkac

function get_input_tags($html)
{
    $post_data = array();

    // a new dom object
    $dom = new DomDocument; 

    //load the html into the object
    $dom->loadHTML($html); 
    //discard white space
    $dom->preserveWhiteSpace = false; 

    //all input tags as a list
    $input_tags = $dom->getElementsByTagName('input'); 

    //get all rows from the table
    for ($i = 0; $i < $input_tags->length; $i++) 
    {
        if( is_object($input_tags->item($i)) )
        {
            $name = $value = '';
            $name_o = $input_tags->item($i)->attributes->getNamedItem('name');
            if(is_object($name_o))
            {
                $name = $name_o->value;

                $value_o = $input_tags->item($i)->attributes->getNamedItem('value');
                if(is_object($value_o))
                {
                    $value = $input_tags->item($i)->attributes->getNamedItem('value')->value;
                }

                $post_data[$name] = $value;
            }
        }
    }

    return $post_data; 
}

error_reporting(~E_WARNING);
$html = file_get_contents("https://accounts.google.com/ServiceLoginAuth");

print_r(get_input_tags($html));

回答by Asep Nurjaman

<form action="" method="post">
<input type="text" name="subject" id="subject" value="Car Loan">
<button type="submit" name="ok">OK</button>
</form>
<?php
if(isset($_POST['ok'])){
echo $_POST['subject'];
}
?>