简体   繁体   English

PHP表单未在提交时处理

[英]PHP Form not processing on submit

I have a form displaying via php after no results were displayed in the original query. 在原始查询中未显示任何结果后,我有一个通过php显示的表单。 The form displays fine but when it is submitted it goes to index.php instead of processing using $_SERVER['PHP_SELF'] . 表单显示正常,但提交后将进入index.php而不是使用$_SERVER['PHP_SELF']

echo "<form method='post' action='". $_SERVER['PHP_SELF']."'>";
echo $error['find'];
echo "<p><label for='find'>Search for Your Favorite Location:</label><br />";
echo "<input type='text' id='find' name='find' value='".($_POST['find'] ? htmlentities($_POST['find']) : '')."' /></p>";
echo "<p><input type='submit' name='submitLocation' value='Submit' /></p></form>";

You can achieve that result by leaving action empty. 您可以通过将action留为空白来实现该结果。 I cleaned up the code a bit, and it now looks like this: 我整理了一下代码,现在看起来像这样:

<?php
if (isset($_POST['submitLocation'])) 
{
 echo isset($_POST['find']) ? htmlentities($_POST['find']) : '';
}
?>

<html>
<body>
<form action="" method="post">
<p><label for='find'>Search for Your Favorite Location:</label><br/>
<input type='text' id='find' name='find' value='' /></p>
<input type='submit' name='submitLocation' value='Submit' />
</form>
</body>
</html>

It checks if the form is submitted or not with PHP's isset() function. 它使用PHP的isset()函数检查表单是否提交。 If it was: then it simply echoes the string the user inputted. 如果是:则仅回显用户输入的字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM