bobby317 Posted May 3, 2010 Share Posted May 3, 2010 I am working on an event calendar and right now I am trying to create the registration form. I am going to use php to validate the form and also to send the form info to my data base. The problem I am having now is when you first go to my page it is trying to validate the form and I only want the code to run when the form is submitted. I think I need to create a function with my code and then have it run when submitted but I can’t seem to figure it out. Here is what I have so far. I have marked out some things as I work through it. Also could you please expain what and why you are doing what you are doing so I have a better understanding thank. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Register</title> <style type="text/css"> body { text-align: center; } form { text-align: left; } label { width: 6em; float: left; text-align: right; margin-right: 0.5em; } .submit input { margin-left: 6.5em; } .error { color: #F00; } </style> </head> <body> <?php //connects to database //include 'config.php'; //include 'opendb.php'; //Get email 1 and 2 preventing SQL injections. Get pass one 1 and 2 with md5 hash //$email1 = mysql_real_escape_string($_POST['email1']); //$email2 = mysql_real_escape_string($_POST['email2']); //$password1 = md5($_POST['pass1']); //$password2 = md5($_POST['pass2']); //create error message for blank fields $errorBlank = "Please fill out all fields!"; //check if email1 is blank and matches email 2 if (empty($_POST['email1'])) { echo "<p class=\"error\">$errorBlank</p>"; } else { $email1 = mysql_real_escape_string($_POST['email1']); } ?> <form name="register" action="register.php" method="post"> <p> <label for="email">Email Adress:</label> <input type="text" name="email" maxlength="30" /> </p> <p> <label for="email2">Email Adress Again:</label> <input type="text" name="email2" maxlength="30" /> </p> <p> <label for"password">Password:</label> <input type="password" name="pass1" /> </p> <p> <label for"password2">Password Again:</label> <input type="password" name="pass2" /> </p> <p class="submit"> <input type="submit" value="Register" /> </p> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/200593-exciting-code-when-form-is-submitted/ Share on other sites More sharing options...
Muddy_Funster Posted May 3, 2010 Share Posted May 3, 2010 give this a shot: <?php if(isset ($submit)){ //connects to database include 'config.php'; include 'opendb.php'; //Get email 1 and 2 preventing SQL injections. Get pass one 1 and 2 with md5 hash $email1 = mysql_real_escape_string($_POST['email1']); $email2 = mysql_real_escape_string($_POST['email2']); $password1 = md5($_POST['pass1']); $password2 = md5($_POST['pass2']); //create error message for blank fields $errorBlank = "Please fill out all fields!"; //check if email1 is blank and matches email 2 if (empty($_POST['email1'])) { echo "<p class=\"error\">$errorBlank</p>"; } else { $email1 = mysql_real_escape_string($_POST['email1']); } } ?> <form name="register" action="PHP_SELF?$submit='true'" method="post"> Quote Link to comment https://forums.phpfreaks.com/topic/200593-exciting-code-when-form-is-submitted/#findComment-1052621 Share on other sites More sharing options...
bobby317 Posted May 3, 2010 Author Share Posted May 3, 2010 Thanks Quote Link to comment https://forums.phpfreaks.com/topic/200593-exciting-code-when-form-is-submitted/#findComment-1052635 Share on other sites More sharing options...
Muddy_Funster Posted May 3, 2010 Share Posted May 3, 2010 Your welcome - does that meen it worked? Quote Link to comment https://forums.phpfreaks.com/topic/200593-exciting-code-when-form-is-submitted/#findComment-1052639 Share on other sites More sharing options...
PFMaBiSmAd Posted May 3, 2010 Share Posted May 3, 2010 Muddy_Funster, just posting 'fixed' code without stating what you fixed in it or why you changed it usually does not help anyone. Also, $submit won't exist so the 'fixed' code will skip over all the form processing code anyway. Quote Link to comment https://forums.phpfreaks.com/topic/200593-exciting-code-when-form-is-submitted/#findComment-1052640 Share on other sites More sharing options...
bobby317 Posted May 3, 2010 Author Share Posted May 3, 2010 Ok I have tried the new code and now I am getting an http404 page not found error? Any advice. Quote Link to comment https://forums.phpfreaks.com/topic/200593-exciting-code-when-form-is-submitted/#findComment-1052646 Share on other sites More sharing options...
Muddy_Funster Posted May 3, 2010 Share Posted May 3, 2010 ok, change the form to: <form method="post" action="register.php?$submit='true'" type="submit"> and see if that helps. And just to keep everyone happy here, I was going to wait untill I knew it worked before going into the mechanics of it but basicly I am atempting to alter the code so that, the first time it is loaded the $submit variable is, as PFM.. rightly said, not set and so the validation code is skiped, however, click the button on the form and it reloads the page at the same time assigning the value of 'true' to the $submit variable which will then be registered and run the validation code. I just preffer to have a solution before I explain how it fixes things...just in case it doesn't. Quote Link to comment https://forums.phpfreaks.com/topic/200593-exciting-code-when-form-is-submitted/#findComment-1052651 Share on other sites More sharing options...
bobby317 Posted May 3, 2010 Author Share Posted May 3, 2010 Ok now that I have changed the action I dont get the http 404 error but if I submit the form with email1 empty I dont get my error message just the for shows back up. Here is what my code looks like now if that helps. Thanks for everything. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Register</title> <style type="text/css"> body { text-align: center; } form { text-align: left; } label { width: 6em; float: left; text-align: right; margin-right: 0.5em; } .submit input { margin-left: 6.5em; } .error { color: #F00; } </style> </head> <body> <?php if(isset ($submit)){ //connects to database //include 'config.php'; //include 'opendb.php'; //Get email 1 and 2 preventing SQL injections. Get pass one 1 and 2 with md5 hash //$email1 = mysql_real_escape_string($_POST['email1']); //$email2 = mysql_real_escape_string($_POST['email2']); //$password1 = md5($_POST['pass1']); //$password2 = md5($_POST['pass2']); //create error message for blank fields $errorBlank = "Please fill out all fields!"; //check if email1 is blank and matches email 2 if (empty($_POST['email1'])) { echo "<p class=\"error\">$errorBlank</p>"; } else { $email1 = mysql_real_escape_string($_POST['email1']); } } ?> <form name="register" action="register.php?$submit='true'" type="submit" method="post"> <p> <label for="email">Email Adress:</label> <input type="text" name="email" maxlength="30" /> </p> <p> <label for="email2">Email Adress Again:</label> <input type="text" name="email2" maxlength="30" /> </p> <p> <label for"password">Password:</label> <input type="password" name="pass1" /> </p> <p> <label for"password2">Password Again:</label> <input type="password" name="pass2" /> </p> <p class="submit"> <input type="submit" value="Register" /> </p> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/200593-exciting-code-when-form-is-submitted/#findComment-1052678 Share on other sites More sharing options...
Muddy_Funster Posted May 3, 2010 Share Posted May 3, 2010 ok, fist let's tidy up the echo line... if (empty($_POST['email1'])) { echo '<p class="error">'.$errorBlank.'</p>'; } else { $email1 = mysql_real_escape_string($_POST['email1']); } } Now look at your form input for the email fields. On the form the names are "email" and "email2", you are POSTing "email1" instead of "email". Fix this to your preference and give it another shot. Quote Link to comment https://forums.phpfreaks.com/topic/200593-exciting-code-when-form-is-submitted/#findComment-1052683 Share on other sites More sharing options...
bobby317 Posted May 3, 2010 Author Share Posted May 3, 2010 Ok I made changes and double and triple checked them and still just reloads form and no error when email1 is blank. Also could you tell me why my echo line was wrong why use singe parenthesis instead of double and also why the dots b4 and after the variable? Thanks for everything. And here is my updated code again in case I missed something. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Register</title> <style type="text/css"> body { text-align: center; } form { text-align: left; } label { width: 6em; float: left; text-align: right; margin-right: 0.5em; } .submit input { margin-left: 6.5em; } .error { color: #F00; } </style> </head> <body> <?php if(isset ($submit)){ //connects to database //include 'config.php'; //include 'opendb.php'; //Get email 1 and 2 preventing SQL injections. Get pass one 1 and 2 with md5 hash //$email1 = mysql_real_escape_string($_POST['email1']); //$email2 = mysql_real_escape_string($_POST['email2']); //$password1 = md5($_POST['pass1']); //$password2 = md5($_POST['pass2']); //create error message for blank fields $errorBlank = "Please fill out all fields!"; //check if email1 is blank and matches email 2 if (empty($_POST['email1'])) { echo '<p class="error">'.$errorBlank.'</p>'; } else { $email1 = mysql_real_escape_string($_POST['email1']); } } ?> <form name="register" action="register.php?$submit='true'" type="submit" method="post"> <p> <label for="email1">Email Adress:</label> <input type="text" name="email1" maxlength="30" /> </p> <p> <label for="email2">Email Adress Again:</label> <input type="text" name="email2" maxlength="30" /> </p> <p> <label for"password">Password:</label> <input type="password" name="pass1" /> </p> <p> <label for"password2">Password Again:</label> <input type="password" name="pass2" /> </p> <p class="submit"> <input type="submit" value="Register" /> </p> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/200593-exciting-code-when-form-is-submitted/#findComment-1052689 Share on other sites More sharing options...
bobby317 Posted May 3, 2010 Author Share Posted May 3, 2010 Ok I got it wohoo!!!! I used a hidden field to pass the value. Thanks for all the help with you pointing me in the right direction I got it. Thanks again.. Here is the code that did it... <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Register</title> <style type="text/css"> body { text-align: center; } form { text-align: left; } label { width: 6em; float: left; text-align: right; margin-right: 0.5em; } .submit input { margin-left: 6.5em; } .error { color: #F00; } </style> </head> <body> <?php $submit = ($_POST['submit']); if($submit == true ){ //connects to database //include 'config.php'; //include 'opendb.php'; //Get email 1 and 2 preventing SQL injections. Get pass one 1 and 2 with md5 hash //$email1 = mysql_real_escape_string($_POST['email1']); //$email2 = mysql_real_escape_string($_POST['email2']); //$password1 = md5($_POST['pass1']); //$password2 = md5($_POST['pass2']); //create error message for blank fields $errorBlank = "Please fill out all fields!"; //check if email1 is blank and matches email 2 if (empty($_POST['email1'])) { echo '<p class="error">'.$errorBlank.'</p>'; } else { $email1 = mysql_real_escape_string($_POST['email1']); } } ?> <form name="register" action="register.php" method="post"> <p> <label for="email1">Email Adress:</label> <input type="text" name="email1" maxlength="30" /> </p> <p> <label for="email2">Email Adress Again:</label> <input type="text" name="email2" maxlength="30" /> </p> <p> <label for"password">Password:</label> <input type="password" name="pass1" /> </p> <p> <label for"password2">Password Again:</label> <input type="password" name="pass2" /> </p> <p class="submit"> <input type="submit" value="Register" /> <input type="hidden" name="submit" value="true" /> </p> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/200593-exciting-code-when-form-is-submitted/#findComment-1052700 Share on other sites More sharing options...
PFMaBiSmAd Posted May 3, 2010 Share Posted May 3, 2010 Or you can just do this - Change your <form tag back to what it originally was - <form name="register" action="register.php" method="post"> Add a name="submit" attribute to your submit button. Change if(isset ($submit)){ to - if(isset ($_POST['submit'])){ Quote Link to comment https://forums.phpfreaks.com/topic/200593-exciting-code-when-form-is-submitted/#findComment-1052702 Share on other sites More sharing options...
Muddy_Funster Posted May 3, 2010 Share Posted May 3, 2010 With the echo line, enclosing the main part of the text in the single quote allowed the native double quotes to be echoed for the html tag. the dots before and after the variable are links. They tell the echo line to add what's after the dot to the output as well. While you can echo "this $var" it is better to echo "this ".$var I'm going to come at this from a different angle and see what we get. <?php if(isset ($_POST['submit'])){ ... ?> <form name="register" action="register.php?submit='true'" type="submit" method="post"> ahh well, even better, you got it yourself. Nice one good luck with the rest. Quote Link to comment https://forums.phpfreaks.com/topic/200593-exciting-code-when-form-is-submitted/#findComment-1052706 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.