harry
harry

Reputation: 11

HTML form data isn't showing up for php processing

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!

Upvotes: 1

Views: 558

Answers (5)

harry
harry

Reputation: 11

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!

Upvotes: 0

lll
lll

Reputation: 12889

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

Upvotes: 2

ThiefMaster
ThiefMaster

Reputation: 318488

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

Upvotes: 3

mateusza
mateusza

Reputation: 5743

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.

Upvotes: 1

prodigitalson
prodigitalson

Reputation: 60413

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.

Upvotes: 1

Related Questions