简体   繁体   English

HTML表单数据未显示用于php处理

[英]HTML form data isn't showing up for php processing

I have a simple HTML form on a index.html page. 我在index.html页面上有一个简单的HTML表单。 When "submitted" the form action goes to "formprocessing.php" and gets worked on. “提交”后,表单操作转到“ formprocessing.php”并开始处理。 It's 3 fields: firstname, lastname, and email address. 这是3个字段:名字,姓氏和电子邮件地址。 It's a separate page, not posting to self. 这是一个单独的页面,没有自我发布。 I don't know if that matters. 我不知道这是否重要。

The problem is that I'm not accessing the data correctly, it seems. 问题在于,看来我没有正确访问数据。 My code is: 我的代码是:

//sets up the variables
$firstname = stripslashes($HTTP_POST_VARS['FirstName']);
$lastname = stripslashes($HTTP_POST_VARS['LastName']);
$email = $HTTP_POST_VARS['Email'];

echo "These variables are: $firstname $lastname $email";
die;

I put in the echo line to troubleshoot because I kept getting an "invalid email" error later in the page. 我放入回显行进行故障排除,因为我稍后在页面中始终收到“无效电子邮件”错误。 There aren't any variables getting retained. 没有任何变量被保留。 I have very similar code working on another page without a problem. 我有非常相似的代码可以在另一个页面上正常工作。 Is there a setting I need to change in the php.ini file? 我需要在php.ini文件中进行更改吗? Does this "HTTP_POST_VARS" only work in certain cases? 这个“ HTTP_POST_VARS”仅在某些情况下有效吗?

Totally stumped. 完全不知所措。 Thanks for any and all suggestions! 感谢您提出的所有建议!

You should use $_POST - the $HTTP_*_VARS arrays are both deprecated and non-superglobal. 您应该使用$_POST $HTTP_*_VARS数组已弃用,并且不是超全局性的。

And you should disable magic_quotes in the PHP config instead of using stripslashes on your variables. 而且,您应该在PHP配置中禁用 magic_quotes,而不是对变量使用反斜杠。 If you cannot, do not unconditionally apply stripslashes to all variables but check if magic quotes are enabled : get_magic_quotes_gpc() , array_map 如果不能, 则不要无条件地将斜杠应用于所有变量,而是检查是否启用了魔术引号get_magic_quotes_gpc()array_map

Firstly, unless you're using a VERY old version of PHP, $HTTP_POST_VARS is deprecated. 首先,除非您使用的是非常旧的PHP版本,否则不建议使用$ HTTP_POST_VARS。 Try using the $_POST superglobal instead. 尝试改用$_POST超全局变量。

If you want to quickly examine the contents of this you can put a var_dump($_POST) in your page to display the contents in a pretty way. 如果要快速检查其内容,可以在页面中放置var_dump($_POST)以漂亮地显示内容。

Here is some example code you can examine and hopefully see where your mistake was, it doesn't require separate html and php files, you can just place it in a single php file. 这是一些示例代码,您可以检查并希望看到错误在哪里,它不需要单独的html和php文件,您可以将其放在单个php文件中。

<?php

if (isset($_POST['submit'])) {
// Show contents of $_POST for debugging.
    var_dump($_POST);

// See if magic_quotes are enabled for $_GET / $_POST / $_COOKIE
    if (get_magic_quotes_gpc()) {

// Recursive stripslashes, in case $_POST contains arrays.
        function stripslashes_deep($value) {
            $value = is_array($value) ?
                array_map('stripslashes_deep', $value) :
                stripslashes($value);

            return $value;
        }

// Tidy $_POST if needed.
        $_POST = array_map(‘stripslashes_deep’, $_POST);
    }

// Output values.
    printf('<h1>The posted values were %s, %s and %s</h1>', $_POST['firstname'], $_POST['lastname'], $_POST['email'] );
}

?>

<form method="post">
    <label for="first">First Name</label>:<br />
    <input type="text" id="first" name="firstname" /><br />
    <label for="last">Last Name</label>:<br />
    <input type="text" id="last" name="lastname" /><br />
    <label for="mail">Email</label>:<br />
    <input type="text" id="mail" name="email" /><br />
    <input type="submit" name="submit" value="Submit" />
</form>

I should also add, these form fields are CASE SENSETIVE. 我还应该添加,这些表单字段区分大小写。 If you have lowercase field names, you need to reference them in lowercase when accessing $_POST 如果您使用小写的字段名称,则在访问$ _POST时需要以小写字母引用它们

Firstly you shoudl use $_POST instead of HTTP_POST_VARS . 首先,您应该使用$_POST而不是HTTP_POST_VARS Secondly the element names are case sensitive, so verify that you are accessing them correctly. 其次,元素名称区分大小写,因此请验证您是否正确访问它们。

Beyond that youll need to post the html for your form so we can take a look. 除此之外,您还需要为表单发布html,以便我们看看。

Try: 尝试:

<pre>
<?php var_dump( $_POST ) ?>
----
<?php var_dump( $_GET ) ?>
----
<?php var_dump( $_SERVER ) ?>
</pre>

to check what values are passed to the script from the form. 检查哪些值从表单传递到脚本。

Found the problem: for some reason the code I had simply had an "href" link, not an input submit. 发现了问题:由于某种原因,我的代码仅具有“ href”链接,而不是输入提交。 My fault for not submitting that part of the code too -- I'm sure someone would have noticed that long before I did (it was someone else's code, btw, they just had that as a placeholder). 我的错是也没有提交那部分代码-我敢肯定有人会在我这样做之前就注意到了(那是别人的代码,顺便说一句,他们只是将其作为占位符)。

Then, additionally, yes, $_POST was needed. 然后,是的,需要$ _POST。 After that it all worked fine. 之后,一切正常。 Thanks for the help! 谢谢您的帮助!

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

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