SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Thread: Contact Form not submitting

  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2010
    Posts
    73
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Contact Form not submitting

    I'm trying to create a contact form with checkboxes or radio buttons for the first time and am having issues. I did some research, but still don't think the code is right. Right now the validation doesn't work and when you click submit the page just refreshes with a blank form. It doesn't actually submit.

    http://empirebathandkitchen.com/test...t_form_new.php is the test page

    HTML:
    Code:
    <!-- DO NOT EDIT -->
    <form action="<?php echo $PHP_SELF;?>" method="post">
    <table cellpadding="5" border="0">
    <tr> 
    <td colspan="2"> First Name:<br> <input name="firstname" type="text" size="25"></td>
    <td colspan="2"> Last Name:<br> <input name="lastname" type="text" size="25"></td>
    </tr>
    <tr> 
    <td colspan="4"> Business:<br> <input name="company" type="text" size="40"></td>
    </tr>
    <tr> 
    <td colspan="4"> Address:<br> <input name="address" type="text" size="40"></td>
    </tr>
    <tr> 
    <td colspan="2"> City:<br> <input name="city" type="text"  size="20"> </td>
    <td> State:<br> <input name="state" type="text" size="10"> </td>
    <td> Zip:<br> <input name="zip" type="text" size="10"> </td>
    </tr>
    <tr>
    <td colspan="2"> Phone:<br> <input name="phone" type="text" size="20"> </td>
    <td colspan="2"> Email Address:<br> <input name="email" type="text" size="20"> </td>
    </tr>
    <tr>
    <td colspan="2">What is your project time frame?<br />
    	<input type="checkbox" name="timeframe[]" value="Now - 3 Months" checked="checked">Now - 3 Months<br />
    	<input type="checkbox" name="timeframe[]" value="3 Months - 6 Months">3 Months - 6 Months<br />
    	<input type="checkbox" name="timeframe[]" value="6 Months - 9 Months">6 Months - 9 Months<br />
    	<input type="checkbox" name="timeframe[]" value="9 Months - Year>">9 Months - Year<br />
    	<input type="checkbox" name="timeframe[]" value="Over A Year">Over A Year
    </td>
    <td colspan="2">Project Type:<input type="hidden" name="projecttype" value="0" />
    <input name="projecttype[]" type="checkbox" value="New Build" checked="checked">New Build or <input type="checkbox" name="projecttype" value="Remodel">Remodel<br />
    
    <input type="checkbox" name="project[]" value="Kitchen" checked="checked">Kitchen<br />
    <input type="checkbox" name="project[]" value="Bathroom">Bathroom<br />
    <input type="checkbox" name="project[]" value="Other">Other  <input name="other" type="text" size="20">
    </td>
    </tr>
    <tr>
    <td colspan="2">Do you have a contractor? 
    <input type="checkbox" name="contractor[]" value="Yes">Yes <input name="contractor" type="checkbox" value="No" checked="checked">No<br /><br />
    If yes, please fill out: <textarea name="contractordetails" cols=40 rows=4 maxlength="400"></textarea>
    </td>
    </tr>
    <tr>
    <td colspan="4"> Additional Comments &amp; Information:<br> <textarea name="comments" cols=40 rows=4 maxlength="400"></textarea></td>
    </tr>
    <tr>
    <td colspan="4"><img src="mc_captcha.php" class="imginline" width="90" height="30">
    Re-Type Confirmation Code: <input name="code" type="text" size="20"> 
    </td>
    </td>
    <tr>
    <td colspan="4"> <input name="submit" id="submit" type="submit" value="Submit"> &nbsp; <input type="reset" value="Reset"> </td>
    </tr>
    </table>
    </form>
    PHP
    Code:
    <?
    session_start();
    ?>
    <!-- CONTENT AREA GOES HERE -->
    
    <?php
    if($_SERVER["REQUEST_METHOD"] == "POST")     {
        foreach($_POST as $key => $val) {
            if(is_array($_POST[$key])) $_POST[$key] = implode(", ", $_POST[$key]);
        }
    }
    ?>
    <?php
    if (isset($_POST['submit'])) 
    {
    //SET EMTPY ERROR
    $errorset = 0;
    
    //GET FORM DETAILS
    $firstname = $_POST["firstname"];
    $lastname = $_POST["lastname"];
    $company = $_POST["company"];
    $realaddress = $_POST["address"];
    $city = $_POST["city"];
    $state = $_POST["state"];
    $zip = $_POST["zip"];
    $phone = $_POST["phone"];
    $email = $_POST["email"];
    $timeframe = $_POST["timeframe"];
    $projecttype = $_POST["projecttype"];
    $project = $_POST["project"];
    $projectdetails = $_POST["projectdetails"];
    $contractor = $_POST["contractor"];
    $contractordetails = $_POST["contractordetails"];
    $comments = $_POST["comments"];
    
    
    //CHECK FOR ERRORS ON REQUIRED FIELDS 
    
    //Encrypt the posted code field and then compare with the stored key
    if(md5($_POST['code']) != $_SESSION['captchakey'])
    {
    echo "Your confirmation code does not match, go back and re-type!<br>";
    
    //echo md5($_POST['code']);
    //echo " does not equal passed key:   ";
    //echo $_SESSION['captchakey'];
    //echo "<br>";
    
    $errorset += 1;
    }
    
    if ($firstname == "") {
    echo "Required field <b>First Name</b> not set!<br>";
    $errorset += 1;
    }
    
    if ($lastname == "") {
    echo "Required field <b>Last Name</b> not set!<br>";
    $errorset += 1;
    }
    
    //EMAIL CHECK
    function ValidateEmail($address) 
    {
        if (function_exists('filter_var')) { //Introduced in PHP 5.2
          if(filter_var($address, FILTER_VALIDATE_EMAIL) === FALSE) {
            return false;
          } else {
            return true;
          }
        } else {
          return preg_match('/^(?:[\w\!\#\$\%\&\'\*\+\-\/\=\?\^\`\{\|\}\~]+\.)*[\w\!\#\$\%\&\'\*\+\-\/\=\?\^\`\{\|\}\~]+@(?:(?:(?:[a-zA-Z0-9_](?:[a-zA-Z0-9_\-](?!\.)){0,61}[a-zA-Z0-9_-]?\.)+[a-zA-Z0-9_](?:[a-zA-Z0-9_\-](?!$)){0,61}[a-zA-Z0-9_]?)|(?:\[(?:(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])\.){3}(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])\]))$/', $address);
        }
    }
    
    if (($email == "") || (!ValidateEmail($email))) {
    echo "Required field <b>E-Mail</b> not set or invalid!<br>";
    $errorset += 1;
    }
    // END EMAIL CHECK
    
    if (!isset($_POST["timeframe"])) {
    echo "Required field <b>Time Frame</b> not selected!<br>";
    $errorset += 1;	   } 
    
    if (!isset($_POST["projecttype"])) {
    echo "Required field <b>Project Type</b> not selected!<br>";
    $errorset += 1;	}
    
    if (!isset($_POST["project"])) {
    echo "Required field <b>Project</b> not selected!<br>";
    $errorset += 1;	}
    
    if (!isset($_POST["contractor"])) {
    echo "Required field <b>Contractor</b> not selected!<br>";
    $errorset += 1;	}
    
    
    // SETUP THE MESSAGE WE ARE SENDING
    $fullmsg = "Email From: ".$firstname." ".$lastname."<br>";
    $fullmsg .= "Email Address: ".$email." <br>";
    $fullmsg .= "Company: ".$company." <br>";
    $fullmsg .= "Address: ".$realaddress." <br>";
    $fullmsg .= "City: ".$city." <br>";
    $fullmsg .= "State: ".$state." <br>";
    $fullmsg .= "Zip: ".$zip." <br>";
    $fullmsg .= "Phone: ".$phone." <br>";
    $fullmsg .= "Timeframe: ".$timeframe." <br>";
    $fullmsg .= "Project Type: ".$projecttype." <br>";
    $fullmsg .= "Project: ".$project." <br>";
    $fullmsg .= "Details: ".$projectdetails." <br>";
    $fullmsg .= "Contractor: ".$contractor." <br>";
    $fullmsg .= "Contractor Details: ".$contractordetails." <br>";
    $fullmsg .= "Message: "."<br>".$comments;  
    
    //error_reporting(E_ALL);
    error_reporting(E_STRICT);
    date_default_timezone_set('America/Toronto');
    
    //GET MAILER CLASS
    require_once('mc_class.phpmailer.php');
    
    // EXTRA ERROR CHECKING ROUTINE by tREMor
    if ($errorset == 0) 
    {
    $mail             = new PHPMailer();
    $body             = $fullmsg;
    $body             = eregi_replace("[\]",'',$body);
    $mail->IsSMTP(); // telling the class to use SMTP
    $mail->Host       = "k2smtpout.secureserver.net"; // SMTP server
    $mail->SMTPDebug  = 1;                     // enables SMTP debug information (for testing)
                                               // 1 = errors and messages
                                               // 2 = messages only
    $mail->SetFrom("$email", "$firstname $lastname");
    $mail->AddReplyTo("$email", "$firstname $lastname");
    $mail->Subject    = "Web Form Submission";
    $mail->MsgHTML($body);
    $address = "mikev@promediaonline.com";
    $mail->AddAddress($address, "Mike");
    
    
    	if(!$mail->Send()) 
    	{
    		echo "<p>Mailer Error: " . $mail->ErrorInfo ." </p>";
    	} 
    	else
    	{
            echo "<p>Mail Sent:</p>";
            
            include("mc_contact-thankyou.html");
    	}
    }
    else 
    {
        echo "<p>Please fill in all required fields to submit your contact form.</p>";
        //echo "<p><a href='javascript: history.go(-1)'>Click Here</a></p>";
        include("mc_contact_form_new.php");
    }
    
    }
    else 
    {
        include("mc_contact_form_new.php");
        //session_write_close();
    }
    ?>

    I should add the form already existed and was created years ago, but the client wants to add the new checkbox fields so I'm trying to alter the existing form.

  2. #2
    SitePoint Member
    Join Date
    Apr 2013
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    At first glance there's a problem with your form action.. you've got <?php echo $PHP_SELF;?> which should actually be <?php echo htmlentities($_SERVER['PHP_SELF']); ?> (running the variable through htmlentities() prevents XSS attacks: http://www.html-form-guide.com/php-f...tion-self.html).

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •