dkhendo
dkhendo

Reputation: 11

php $_POST var clearing itself when i check it

i have a crazy problem that i just can't figure out. my form has two fields and a submit button. when i submit the vars get passed into $_POST just fine...

print('<div class=error>');
print_r($_POST);
print('</div>');

that gives me the two fields with the expected values along with the value of the submit button.

HOWEVER! when i add the following line of code so i can process based on the submit button, it clears all of the data. the post array shows up empty.

if ($_POST['submit'] == 'Submit') {

that clears the data. if i change the value from 'Submit' to anything else, the vars still show up in $_POST, they just get cleared when i try to check them.

any ideas what i'm doing wrong here?

here's the form:

<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<?
  if (isset($msg)) {
    echo "$msg";
  }
?>
<input type=text  name='email'><br>
<br><input type=password name='password'>
<br>
<input type="submit" name="submit" value=Submit>
</form>

and here's the processing code:

if ($_POST['submit'] == 'Submit') {
echo "<div class=error>made it here</div>";
$u = $_POST['email'];
$p = $_POST['password'];
$auth = mysql_query("Select * from member where email='$u' and password='$p'");
$auth = mysql_fetch_array($auth);

if ($auth) {
    $pid = $auth[id];

    echo "aa";
    sess_register("sess_msg");
    $sess_msg = null;
    global $auth, $pid;
}
}

if i change the value when i check to see if the submit button has a value to something other than the actual value of the submit button, which is 'Submit' - it clears all variables sent to $_POST

Upvotes: 0

Views: 273

Answers (2)

Jerry Saravia
Jerry Saravia

Reputation: 3837

From your post, it doesn't look your code should be emptying the $_POST array. The only thing that I can think of at the moment is that maybe in the code you actually only put one '=' sign.

var_dump( $_POST );

if ( isset( $_POST['submit'] ) ) {
    var_dump( $_POST );
    echo "<div class=error>made it here</div>";
    $u = $_POST['email'];
    $p = $_POST['password'];
   $auth = mysql_query("Select * from member where email='$u' and password='$p'");
   $auth = mysql_fetch_array($auth);

   if ($auth) {
       $pid = $auth[id];

       echo "aa";
       sess_register("sess_msg");
       $sess_msg = null;
       global $auth, $pid;
   }
}

var_dump( $_POST );

Upvotes: 0

entropid
entropid

Reputation: 6239

If you want to check which submit button was clicked, you just have to look for its name as a key in the array $_POST.

So you should do:

if (array_key_exists('submit', $_POST)) {
    // your code
}

Little advice: you'd better escape your $_POST data before putting it into a query! Check this out: http://php.net/manual/en/function.mysql-real-escape-string.php

Upvotes: 1

Related Questions