1

I have a simple HTML form on a index.html page. When "submitted" the form action goes to "formprocessing.php" and gets worked on. It's 3 fields: firstname, lastname, and email address. 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? Does this "HTTP_POST_VARS" only work in certain cases?

Totally stumped. Thanks for any and all suggestions!

5 Answers 5

3

You should use $_POST - the $HTTP_*_VARS arrays are both deprecated and non-superglobal.

And you should disable magic_quotes in the PHP config instead of using stripslashes on your variables. If you cannot, do not unconditionally apply stripslashes to all variables but check if magic quotes are enabled: get_magic_quotes_gpc(), array_map

Sign up to request clarification or add additional context in comments.

4 Comments

There's no such thing as $POST.
+1 for correct answer. but do you think he can follow and asdjust his code based on this?
$_POST (prefixed with underline)
markdown interpretet the underline as italic, i put code tags around it ;)
2

Firstly, unless you're using a VERY old version of PHP, $HTTP_POST_VARS is deprecated. Try using the $_POST superglobal instead.

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.

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.

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

1 Comment

@ThiefMaster Thanks! Many years of using the wrong word finally corrected :)
1

Firstly you shoudl use $_POST instead of 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.

Comments

1

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.

1 Comment

If you are checking the values passed to the form what would be the need in checking $_GET and $_SERVER arrays?
0

Found the problem: for some reason the code I had simply had an "href" link, not an input submit. 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. After that it all worked fine. Thanks for the help!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.