Jump to content

Form not processing after submit


shank888

Recommended Posts

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: noreply@communitycouch.com');

          ////////// 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]

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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' ) {

Link to comment
Share on other sites

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'])) {?

Link to comment
Share on other sites

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: noreply@communitycouch.com');
        ////////// 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]

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.