shank888 Posted December 18, 2011 Share Posted December 18, 2011 I have a form that is not doing anything after I click "submit". The code is below and a demo with the code is here: http://communitycouch.com/index.php?action=register <?php /* Things to do: Create error to show error when special characters are inputted in First and Last Name Fields Verify Dates to be true upon submit (for bithdays) Create birthday variable to be submitted into database, also add is_numberic() change activate.php to index?action=activate */ if($action == 'register') { if (isset($_POST['submit'])) { $errors = array(); require_once('connection.php'); //////////Checks Username //////////Makes sure username is 4-20 characters and contains only letters and numbers if(ereg("[[:alnum:]]{4,20}",stripslashes(trim($_POST['username'])))) { $user = mysql_real_escape_string($_POST['username']); $query = "SELECT username FROM reg_vars WHERE username = '$username'"; $result = @mysql_query($query); $num = @mysql_num_rows($result); if ($num> 0) { $errors[] = '<font color="red">The username you have chosen has already been taken, please try again.</font>'; } else { $username = mysql_real_escape_string($_POST['username']); } } else { $errors[] = '<font color="red">Please provide a valid username between 4 and 30 characters.</font>'; } //////////Checks E-mail if (!eregi('^[a-zA-Z]+[a-zA-Z0-9_-]*@([a-zA-Z0-9]+){1}(\.[a-zA-Z0-9]+){1,2}', stripslashes(trim($_POST['email'])) ) || empty($_POST['email'])) { $errors[] = '<font color="red">Please provide a valid email address.</font>'; } else { $email = mysql_real_escape_string($_POST['email']); } ///////// Check Names if (empty($f_name)) { $error["f_name"] = "First Name is blank."; } if (empty($l_name)) { $error["l_name"] = "Last Name is blank."; } ////////// Check PASSWORDS if (!empty($_POST['password'])) { if ($_POST['password'] != $_POST['cpassword']) { $errors[] = '<font color="red">The passwords you have entered do not match.</font>'; } else { $password = $_POST['password']; } } else { $errors[] = '<font color="red">Please provide a password.</font>'; } /////////Send Activation E-mail //////// Add Birthday to the registrarion Variables if (empty($errors)) { $a = md5(uniqid(rand(), true)); $query = "INSERT INTO reg_vars (username, f_name, l_name, email, password, active) VALUES ('$username', '$email', SHA1('$password'), '$a')"; $result = @mysql_query($query); if (mysql_affected_rows() == 1) { ////////// Send the Activation email $body = "Thank you for registering at the User Registration site. To activate your account, please click on this link:\n\n"; $body .= "http://www.communitycouch.com/activate.php?x=" . mysql_insert_id() . "&y=$a"; mail($_POST['email'], 'Registration Confirmation', $body, 'From: [email protected]'); ////////// Show thank you message echo '<h3>Thank You!</h3> You have been registered, you have been sent an e-mail to the address you specified before. Please check your e-mails to activate your account.'; } else { echo '<font color="red">You could not be registered, please contact us about the problem and we will fix it as soon as we can.</font>'; } } else { echo '<h3>Error!</h3> The following error(s) occured:<br />'; foreach ($errors as $msg) { echo " <div id=\"success_or_fail\"><font color=\"red\">$msg</font><br />\n</div>"; } } } echo " <div id=\"register_container\" class=\"Container\"> <div id=\"register_Center\" class=\"BoxCenter\"> "; echo" <div id=\"register_BoxContainer\" class=\"Container\"> <div class=\"HeaderLeft\"></div><div id=\"register_Header\" class=\"HeaderCenter\">Register for Community Couch</div><div class=\"HeaderRight\"></div><br /> <div id=\"register_Content\" class=\"BoxContent\"> <form name=\"reg_form\" action=\"";$_SERVER['PHP_SELF']; echo"\" method=\"post\" style=\"margin: 0;\"> <div class=\"Container FormBoxLeft\"> <b>Username:</b> </div> <div class=\"Container FormBoxRight\"> <input name=\"username\" size=\"30\" type=\"text\" /> </div> <br class=\"EndColumn\" /> <div class=\"Container FormBoxLeft\"> <b>First Name:</b> </div> <div class=\"Container FormBoxRight\"> <input name=\"f_name\" size=\"30\" type=\"text\" /> </div> <br class=\"EndColumn\" /> <div class=\"Container FormBoxLeft\"> <b>Last Name:</b> </div> <div class=\"Container FormBoxRight\"> <input name=\"l_name\" size=\"30\" type=\"text\" /> </div> <br class=\"EndColumn\" /> <div class=\"Container FormBoxLeft\"> <b>Birthdate:</b> </div> <div class=\"Container FormBoxRight\"> <select name=\"month\"><option value=\"\">-Month-</option> <option value=\"01\">January</option> <option value=\"02\">February</option> <option value=\"03\">March</option> <option value=\"04\">April</option> <option value=\"05\">May</option> <option value=\"06\">June</option> <option value=\"07\">July</option> <option value=\"08\">August</option> <option value=\"09\">September</option> <option value=\"10\">October</option> <option value=\"11\">November</option> <option value=\"12\">December</option> </select> <select name=\"day\"> <option value=\"\"> -Day-</option>'; //Print 31 Days "; for ($x=1; $x<=31; $x++) { echo "<option value='".$x."'"; if(isset($_POST['submit']) && $day == $x) { echo " selected"; } echo ">".$x."</option>\n"; } echo "</select> <input name=\"year\" size=\"2\" maxlength=\"4\" type=\"text\"> </div> <br class=\"EndColumn\" /> <div class=\"Container FormBoxLeft\"> <b>E-Mail:</b> </div> <div class=\"Container FormBoxRight\"> <input name=\"email\" size=\"30\" type=\"text\" /> </div> <br class=\"EndColumn\" /> <div class=\"Container FormBoxLeft\"> <b>Display E-Mail to the Public:</b> </div> <div class=\"Container FormBoxRight\"> <input name=\"disp_email\" value=\"yes\" type=\"radio\" /> Yes <input name=\"disp_email\" value=\"no\" type=\"radio\" checked /> No </div> <br class=\"EndColumn\" /> <div class=\"Container FormBoxLeft\"> <b>Password:</b> </div> <div class=\"Container FormBoxRight\"> <input name=\"password\" size=\"30\" type=\"password\" /> </div> <br class=\"EndColumn\" /> <div class=\"Container FormBoxLeft\"> <b>Confirm Password:</b> </div> <div class=\"Container FormBoxRight\"> <input name=\"cpassword\" size=\"30\" type=\"password\" /> </div> <br class=\"EndColumn\" /> <div id=\"register_submit\" class=\"Container\"> <input name=\"tos\" type=\"checkbox\" /> I have read and agree to the Terms of Use and Privacy Policy<br /> <input type=\"submit\" value=\"Submit\"><input type=\"reset\" value=\"Reset\"> </div> <br class=\"EndColumn\" /> </form> </div> <div class=\"FooterLeft\"></div><div id=\"register_Footer\" class=\"FooterCenter\"></div><div class=\"FooterRight\"></div> </div> </div> </div> <br class=\"EndColumn\" /> <br />"; }?> [\code] Quote Link to comment https://forums.phpfreaks.com/topic/253431-form-not-processing-after-submit/ Share on other sites More sharing options...
SergeiSS Posted December 18, 2011 Share Posted December 18, 2011 It seems that you problem is in this line or, better say, before this line: if($action == 'register') { You didn't assign value to this variable. I suggest it might come from GET. Write this string before that comparison $action = (!empty($_GET['action']) ) ? $_GET['action'] : ''; BTW here you have a mixture of GET and POST variables. It's not a problem, of course, but you must understand how to work with them. Quote Link to comment https://forums.phpfreaks.com/topic/253431-form-not-processing-after-submit/#findComment-1299051 Share on other sites More sharing options...
shank888 Posted December 18, 2011 Author Share Posted December 18, 2011 I'll try that, all the pages are included in my index.php file, the variable is defined in there: $action = $_GET['action']; Quote Link to comment https://forums.phpfreaks.com/topic/253431-form-not-processing-after-submit/#findComment-1299052 Share on other sites More sharing options...
Pikachu2000 Posted December 18, 2011 Share Posted December 18, 2011 You've neglected to name your submit button, so the if( isset($_POST['submit']) ) { conditional is failing. Some browsers have a tendency to mangle the value of submit buttons, so the better way to handle checking if a form has been submitted with POST is to use the following. if( strtolower($_SERVER['REQUEST_METHOD']) === 'post' ) { Quote Link to comment https://forums.phpfreaks.com/topic/253431-form-not-processing-after-submit/#findComment-1299061 Share on other sites More sharing options...
shank888 Posted December 18, 2011 Author Share Posted December 18, 2011 You've neglected to name your submit button, so the if( isset($_POST['submit']) ) { conditional is failing. Some browsers have a tendency to mangle the value of submit buttons, so the better way to handle checking if a form has been submitted with POST is to use the following. if( strtolower($_SERVER['REQUEST_METHOD']) === 'post' ) { I have never seen that function before, would that replace my if (isset($_POST['submit'])) {? Quote Link to comment https://forums.phpfreaks.com/topic/253431-form-not-processing-after-submit/#findComment-1299062 Share on other sites More sharing options...
Pikachu2000 Posted December 18, 2011 Share Posted December 18, 2011 Yes, it would, as long as the form method is POST. Quote Link to comment https://forums.phpfreaks.com/topic/253431-form-not-processing-after-submit/#findComment-1299064 Share on other sites More sharing options...
shank888 Posted December 19, 2011 Author Share Posted December 19, 2011 Okay, that works lots and I cleaned up my code, now I am trying to add a verification to a radio button. I tried googling it but I can't seem to find what I am looking for. In the updated code below the radio button always seems to come up as empty weather or not you check it. I have been using empty(), is there another function i should use? As well whats the best way to verify if a birthday is correct? <?php if($action == 'register') { if( strtolower($_SERVER['REQUEST_METHOD']) === 'post' ) { $password1 = sha1($_POST['password']); $password2 = sha1($_POST['cpassword']); $month = StripSpecialChars($_POST['month']); $day = StripSpecialChars($_POST['day']); $year = StripSpecialChars($_POST['year']); $bday = $year . "-" . $month . "-" . $day; $reg_date = date("Y-m-d H:i:s"); $pattern = '([[:digit:]]|[~`!@#$%^&*()_=+{}|\:;"/?,]|[|]|-)+'; $fname = stripslashes($_POST['f_name']); $lname = stripslashes($_POST['l_name']); $disp_emai= $_POST['disp_email']; // Check if names are valid if (ereg($pattern,$fname) || ereg($pattern,$lname)) { $errors_code["name"] = "Names must be letters only."; } // Check if the email and username exist already if(MysqlVarExists("communitycouch_users", "reg_vars", "email", $email) == true) { $errors_code["existing_email"] = "That email is already being used."; } if(MysqlVarExists("communitycouch_users", "reg_vars", "username", $username) == true) { $error_code["existing_username"] = "That username is already in use."; } //Check username length and special characters if(!ereg("[[:alnum:]]{4,20}",stripslashes(trim($_POST['username'])))) { $error_code["username_length"] = "Username must contain only letters and numbers."; } // Check for Valid E-mail Address if (!eregi('^[a-zA-Z]+[a-zA-Z0-9_-]*@([a-zA-Z0-9]+){1}(\.[a-zA-Z0-9]+){1,2}', stripslashes(trim($_POST['email'])) ) || empty($_POST['email'])) { $error_code["incorrect_email"] = "Please provide a valid E-mail."; } // Check for matching passwords if($password1 != $password2) { $error_code["password_match"] = "Passwords do not match."; } // Make sure the year is also numeric & Valid if (empty($year) || (eregi("[0-9]{4}", $year) == false) || $year >= 2012) { $error_code["year"] = "Invalid year"; } // Make sure user checks if they want e-mail public if(empty($disp_email)) { $error_code["disp_email"] = "You must choose whether you want your e-mail public."; } // Check if terms of service is checked if (empty($tos)) { $error_code["tos"] = "You must agree to the Terms of Service to register."; } //// If there is errors lets list them, if not lets instert some values into the database if(count($error_code) == 0) { ///If there is no errors we will create a unique activation code $a = md5(uniqid(rand(), true)); // go into the users database @mysql_select_db("communitycouch_users") or die( "Unable to select database"); // Create the query and submit it $query = "INSERT INTO `reg_vars` VALUES('$email', '$username', '$f_name', '$l_name', '$bday', '$disp_email', '$reg_date', '$password', 'Member', '$a')"; mysql_query($query) or die(mysql_error()); ///// Send Activation E-mail! $body = "Thank you for registering a new Community Couch account. To activate your account, please click on this link:\n\n"; $body .= "http://www.communitycouch.com/activate.php?x=" . mysql_insert_id() . "&y=$a"; mail($_POST['email'], 'Registration Confirmation', $body, 'From: [email protected]'); ////////// Show thank you message echo '<h3>Thank You!</h3> You have been registered, you have been sent an e-mail to the address you specified. Please check your e-mail to activate your account.'; } else { echo "<b>ERROR!</b><br />"; foreach($error_code as $value) { echo $value . "<br />"; } } }//////////Quote for Form Submit echo " <div id=\"register_container\" class=\"Container\"> <div id=\"register_Center\" class=\"BoxCenter\"> "; echo" <div id=\"register_BoxContainer\" class=\"Container\"> <div class=\"HeaderLeft\"></div><div id=\"register_Header\" class=\"HeaderCenter\">Register for Community Couch</div><div class=\"HeaderRight\"></div><br /> <div id=\"register_Content\" class=\"BoxContent\"> <form name=\"reg_form\" action=\"";$_SERVER['PHP_SELF']; echo"\" method=\"post\" style=\"margin: 0;\"> <div class=\"Container FormBoxLeft\"> <b>Username:</b> </div> <div class=\"Container FormBoxRight\"> <input name=\"username\" size=\"30\" type=\"text\" /> </div> <br class=\"EndColumn\" /> <div class=\"Container FormBoxLeft\"> <b>First Name:</b> </div> <div class=\"Container FormBoxRight\"> <input name=\"f_name\" size=\"30\" type=\"text\" /> </div> <br class=\"EndColumn\" /> <div class=\"Container FormBoxLeft\"> <b>Last Name:</b> </div> <div class=\"Container FormBoxRight\"> <input name=\"l_name\" size=\"30\" type=\"text\" /> </div> <br class=\"EndColumn\" /> <div class=\"Container FormBoxLeft\"> <b>Birthdate:</b> </div> <div class=\"Container FormBoxRight\"> <select name=\"month\"><option value=\"\">-Month-</option> <option value=\"01\">January</option> <option value=\"02\">February</option> <option value=\"03\">March</option> <option value=\"04\">April</option> <option value=\"05\">May</option> <option value=\"06\">June</option> <option value=\"07\">July</option> <option value=\"08\">August</option> <option value=\"09\">September</option> <option value=\"10\">October</option> <option value=\"11\">November</option> <option value=\"12\">December</option> </select> <select name=\"day\"> <option value=\"\"> -Day-</option>'; //Print 31 Days "; for ($x=1; $x<=31; $x++) { echo "<option value='".$x."'"; if(isset($_POST['submit']) && $day == $x) { echo " selected"; } echo ">".$x."</option>\n"; } echo "</select> <input name=\"year\" size=\"2\" maxlength=\"4\" type=\"text\"> </div> <br class=\"EndColumn\" /> <div class=\"Container FormBoxLeft\"> <b>E-Mail:</b> </div> <div class=\"Container FormBoxRight\"> <input name=\"email\" size=\"30\" type=\"text\" /> </div> <br class=\"EndColumn\" /> <div class=\"Container FormBoxLeft\"> <b>Display E-Mail to the Public:</b> </div> <div class=\"Container FormBoxRight\"> <input name=\"disp_email\" value=\"yes\" type=\"radio\" /> Yes <input name=\"disp_email\" value=\"no\" type=\"radio\" checked /> No </div> <br class=\"EndColumn\" /> <div class=\"Container FormBoxLeft\"> <b>Password:</b> </div> <div class=\"Container FormBoxRight\"> <input name=\"password\" size=\"30\" type=\"password\" /> </div> <br class=\"EndColumn\" /> <div class=\"Container FormBoxLeft\"> <b>Confirm Password:</b> </div> <div class=\"Container FormBoxRight\"> <input name=\"cpassword\" size=\"30\" type=\"password\" /> </div> <br class=\"EndColumn\" /> <div id=\"register_submit\" class=\"Container\"> <input name=\"tos\" type=\"checkbox\" /> I have read and agree to the Terms of Use and Privacy Policy<br /> <input type=\"submit\" value=\"Submit\" name=\"submit\"><input type=\"reset\" value=\"Reset\"> </div> <br class=\"EndColumn\" /> </form> </div> <div class=\"FooterLeft\"></div><div id=\"register_Footer\" class=\"FooterCenter\"></div><div class=\"FooterRight\"></div> </div> </div> </div> <br class=\"EndColumn\" /> <br />"; } /////////Quote for Action= Register[\code] Quote Link to comment https://forums.phpfreaks.com/topic/253431-form-not-processing-after-submit/#findComment-1299116 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.