Page 1 of 1

form $_POST method acting strange

Posted: Sun Jul 30, 2006 6:16 am
by SimonJ621
Hello again,

So I am working with this script that allows an admin to enter a new user into the db. All forms used call the same page, addUser.php. However, I keep running into the same problem of the second form not working properly. Here is how I have my if statements set up.

Code: Select all

if (isset($_POST["submitUser"]) OR isset($_POST["confirmUser"])) {
     if (isset($_POST["submitUser"])) {
          // Show the confirmation page
          // Show the second form (confirmUser) which consists of a submit button and hidden variables
          print "
	  <form name = 'confirmUser' action = 'addUser.php' method = 'POST'>
	  <input type = 'hidden' name = 'fname' value = '".$clean['fname']."' />
	  <input type = 'hidden' name = 'lname' value = '".$clean['lname']."' />
	  <input type = 'hidden' name = 'email' value = '".$clean['email']."' />
	  <input type = 'hidden' name = 'pword' value = '".$clean['password']."' />
	  <input type = 'submit' name = 'confirmUser' value = 'confirm' />
	  </form>";
     }
     elseif (isset($_Post["confirmUser"]))  {
          // Insert data into the database using the passed hidden values
     }
     else {
          print "error";
     }
}
else {
     // Show the first form (submitUser) to allow the user to input values
     print "
     Add User Form
     <br />
     <br />
     <form name = 'addUser' action = 'addUser.php' method = 'POST'>
     First Name:&nbsp;&nbsp;
     <input type = 'text' name = 'fname' maxlength = '30' />
     <br />
     Last Name:&nbsp;&nbsp;
     <input type = 'text' name = 'lname' maxlength = '30' />
     <br />
     Email:&nbsp;&nbsp;
     <input type = 'text' name = 'email' maxlength = '60' />
     <br />
     <h5>Note: The passwords will later be automatically generated and encrypted.<br />
     The User will then be prompted (made) to change the password on the first login</h5>
     Password:&nbsp;&nbsp;
     <input type = 'password' name = 'pword' maxlength = '30' />
     <br />
     <br />
     <input type = 'submit' name = 'submitUser' />
     </form>";
}
When I run this script, the first form correctly shows up, and after I submit the info the confirmation screen correctly shows up, and after I confirm the error screen incorrectly shows up. The reason I don't understand this is, the only way the script is entering the first if statement is if isset($_POST["confirmUser"]) is true, but yet it skips over the elseif?

Any help would be greatly appreciated, thanks,

Jason

Posted: Sun Jul 30, 2006 6:26 am
by Charles256
var_dump($_POST); at every if statement..see what's going on :-D Debug my young padawan! :-D

Posted: Sun Jul 30, 2006 6:47 am
by SimonJ621
Ok,

My poor attamept at debugging was to add the else { print "error" }.

I did as you said and am a little confused.

After the first if "if (isset($_POST["submitUser"]) OR isset($_POST["confirmUser"]))", right after i used the form "submitUser", I get this as the var_dump:

array(5) { ["fname"]=> string(5) "Jason" ["lname"]=> string(5) "Simon" ["email"]=> string(17) "blabla@blabla.com" ["pword"]=> string(0) "" ["submitUser"]=> string(12) "Submit Query" }

And the same thing (as expected) after the next if " if (isset($_POST["submitUser"]))"


Then after confirming the data I get this information after the first if "
if (isset($_POST["submitUser"]) OR isset($_POST["confirmUser"]))":

array(5) { ["fname"]=> string(5) "Jason" ["lname"]=> string(5) "Simon" ["email"]=> string(17) "blabla@blabla.com" ["pword"]=> string(80) "13c39fe7dbb0727e10e1e42331328acfbd245780087542dbfca82313324e1e01e7270bbd7ef93c31" ["confirmUser"]=> string(7) "confirm" }

And the same thing after the error else.

The code skips over the elseif statment which is where I want it to go :(

Ok, phew, my first real debugging session.

Jason

Posted: Sun Jul 30, 2006 8:12 am
by ronverdonk
You have a typo in your POST spelling:

Code: Select all

elseif (isset($_Post["confirmUser"]))  {
must be

Code: Select all

elseif (isset($_POST["confirmUser"]))  {

Posted: Sun Jul 30, 2006 8:19 am
by SimonJ621
Ahah! that did it ... thank you!

Now I'm experiencing a different issue. The query is inserting correctly, but the variables aren't being passed correctly. This means I am ending up simply with empty fields in my database. Here is how I have my query set up:

Code: Select all

$query = sprintf("INSERT INTO user SET userid = NULL, fname = '%s', lname = '%s', email = '%s',
			password = '%s', salt = 21, sugar = 21",
			$clean['fname'], $clean['lname'], $clean['email'], $clean['password']); 
$mysqldb->query($query);
The variables replacing %s are replacing it with nothing, yet the var_dump() shows the variables correctly.

Thanks for the help,

Jason

Posted: Sun Jul 30, 2006 8:30 am
by SimonJ621
Ok, I think I figured it out....

I was calling the wrong post variable... my statement looks like this now (unchanged)

Code: Select all

$clean['fname'] = $_POST["$clean{'fname'}"];
$clean['lname'] = $_POST["$clean{'lname'}"];
$clean['email'] = $_POST["$clean{'email'}"];
$clean['password'] = $_POST["$clean{'pword'}"];
And I am pretty sure it should just be $_POST['fname']

I'll repost after I test that.

Thanks,

Jason

Posted: Sun Jul 30, 2006 8:39 am
by SimonJ621
Much thanks to Charles and ronver...

Hopefully, soon I won't need my hand held in this debugging :(

Thanks again,

Jason