Php:表单提交是如何工作的?


Php: How does form submission work?

我对PHP相当陌生。

代码很简单:

home.php:

<form action="getsentitem.php" method="get">
   <div  >
   <input  name="query" id="query" class="searchQuery" size="20" value="" type="text" autocomplete="off">
   <input id="searchButton" value="Search" type="submit">
   </div>
</form>

getsentitem.php:

<?php
if (isset($_GET['query']))
$query = $_GET['query'];
?>

问题:上面的代码将简单地给我在home.php的文本框中输入的任何内容。现在,有什么方法可以让我获得文本框的其他属性的值吗?例如,是否可以通过这个方法获得文本框的id或大小

不,不是。只有名称=>值对通过您选择的方法(GET/POST)发送到服务器。

如果需要,可以在表单的隐藏输入中包含自定义数据:

<form action="getsentitem.php" method="get">
   <div  >
   <input type="hidden" name="more_info" value="I will be available after submit."/>
   <input  name="query" id="query" class="searchQuery" size="20" value="" type="text" autocomplete="off">
   <input id="searchButton" value="Search" type="submit">
   </div>
</form>
echo $_GET['more_info']; // 'I will be available after submit.'

当您通过AJAX调用和javascript计算生成额外数据时,这很方便。你不一定知道一个高度动态网页的所有id。

CSRF令牌通常也以这种方式发送。


No。只提交输入的值与您给它的名称,没有其他;你可以看到URL中提交了什么,仅此而已。既然首先创建了HTML,那么您应该知道其他值是什么。

简短的回答是:NO

只能从$_GET超数组中获取输入的值。

编辑。

但是如果你做了这样的事情:

<form action="getsentitem.php" method="get">
   <div  >
   <input  name="query" id="query" class="searchQuery" size="20" value="" type="text" autocomplete="off">
   <input name="queryMeta" value="id:query_class:searchQuery_size:20" type="hidden">
   <input id="searchButton" value="Search" type="submit">
   </div>
</form>

那么你可以在PHP中这样读:

<?php
if (isset($_GET['queryMeta']))
$queryMeta = explode('_',$_GET['queryMeta']); //splits the string to array('id:query','class:searchQuery','size:20')
?>