Page 1 of 1

Can someone see what's wrong with my form?

Posted: Sat Nov 22, 2014 2:00 pm
by lastchild
My form is not sending intermittently. Sometimes it will error out when I submit, other times it goes right through.
I'm sure the code is wrong in so many levels. Not being a very good coder (to put it mildly) I had to cobble this together to get the features that I needed.

Can someone please take a look and let me know where the critical problem(s) is/are?

Code: Select all

<script>
function submitForm() {
  var contactForm = $(this);

  if ( !$('#fName').val() || !$('#lName').val() || !$('#email').val() || !$('#when').val() || !$('#message').val() ) {
    $('#incompleteMessage');

  } else {

    $('#sendingMessage');

    $.ajax( {
      url: contactForm.attr( 'action' ) + "?ajax=true",
      type: contactForm.attr( 'method' ),
      data: contactForm.serialize(),
      success: submitFinished
    } );
  }

  return false;
}
function submitFinished( response ) {
  response = $.trim( response );
  if ( response == "success" ) {
    $('#fName').val( "" );
    $('#lName').val( "" );
    $('#phone').val( "" );
    $('#email').val( "" );	
    $('#when').val( "" );
    $('#message').val( "" );
  } else {
	  
    $('#failureMessage');
    $('#contactForm');
  }
  return false;
}
</script>



<?php
$num1 = rand(0,10);
$num2 = rand(0,10); 
$o = rand(0,1); // 0 = plus, 1 = minus 
 
function operand($o) {
    switch($o) {
        case 0: return "+"; break;
        case 1: return "-"; break;
        default: return "?"; break;
    }
}
?>

		<form id="contactForm" action="submit.php" method="post">
		  <ul>
                        <li>
                            <label for="fName"><span class="required">*</span> First name</label>
                            <input type="text" name="fName" id="fName" required="required" maxlength="40" />
                        </li>
                        <li>
                            <label for="lname"><span class="required">*</span> Last name</label>
                            <input type="text" name="lname" id="lname" required="required" maxlength="40" />
                        </li>
                        <li>
                            <label for="phone">Phone (Optional)</label>
                            <input type="phone" name="phone" id="phone" maxlength="50" />
                        </li>
                        <li>
                            <label for="email"><span class="required">*</span> Email</label>
                            <input type="email" name="email" id="email" required="required" maxlength="50" />
                        </li>
                        <li>
                            <label for="when"><span class="required">*</span> When is a good time to call?</label>
                            <input type="text" name="when" id="when" required="required" />
                        </li>
                        <li>
                            <li>
                            <label for="message"><span class="required">*</span> Your message:</label>
                            <textarea name="message" id="message" required="required" cols="80" rows="10" maxlength="10000"></textarea>
                        </li>
												
                     </ul>
							
			<div id="formButtons">
                             <ul class="captcha">
                                <li>
                                <label for="math">What is <?php echo $num1 . "&nbsp;" . operand($o) . "&nbsp;" . $num2 . "?"; ?></label>
                                    <input type="text" id="math" name="userAnswer" size="3"></input>
                                    <input type="hidden" name="num1" value="<?php echo $num1; ?>"></input>
                                    <input type="hidden" name="operand" value="<?php echo $o; ?>"></input>
                                    <input type="hidden" name="num2" value="<?php echo $num2; ?>"></input>
				</li>
                           </ul>
                            <div style="clear:both"></div>
                                    <br>
                                    <input type="submit" name="submit" value="Send"></input>
			</div>
                            <div style="clear:both"></div>
				</form>
					<div id="sendingMessage" class="statusMessage">
						<p>Sending your message. Please wait...</p>
					</div>
					<div id="successMessage" class="statusMessage">
						<p>Thanks for sending your message! We'll get back to you shortly.</p>
					</div>
					<div id="failureMessage" class="statusMessage">
						<p>There was a problem sending your message. Please try again.</p>
					</div>
					<div id="incompleteMessage" class="statusMessage">
						<p>Please complete all the fields in the form before sending.</p>
					</div>[/HTML]

<?php 
define( "RECIPIENT_NAME", "Admin" );
define( "RECIPIENT_EMAIL", "admin@gmail.com" ); 
define( "EMAIL_SUBJECT", "Message from Website" );

    if(!isset($_POST["userAnswer"])) {
        exit("You did not enter a sum in the security question. Please <a href='javascript:history.go(-1)'>go back</a> and try again.");
    }
    $userAnswer = $_POST["userAnswer"];
    $num1 = $_POST["num1"]; 
    $num2 = $_POST["num2"];
    $o = $_POST["operand"];
 
    $actual = -999; 
    switch($o) {
        case 0: $actual = $num1 + $num2; break; 
        case 1: $actual = $num1 - $num2; break;
    }
 
    if($userAnswer != $actual) {
        exit("Please <a href='javascript:history.go(-1)'>go back</a> enter the correct math sum in our security question.");
    }
	
$success = false;
$fName = isset( $_POST['fName'] ) ? preg_replace( "/[^\.\-\' a-zA-Z0-9]/", "", $_POST['fName'] ) : "";
$lname = isset( $_POST['lname'] ) ? preg_replace( "/[^\.\-\' a-zA-Z0-9]/", "", $_POST['lname'] ) : "";
$phone = isset( $_POST['phone'] ) ? preg_replace("/[^\.\-\' a-zA-Z0-9]/", "", $_POST['phone'] ) : "";
$email = isset( $_POST['email'] ) ? preg_replace( "/[^\.\-\_\@a-zA-Z0-9]/", "", $_POST['email'] ) : "";
$when = isset( $_POST['when'] ) ? preg_replace( "/[^\.\-\_\@a-zA-Z0-9]/", "", $_POST['when'] ) : "";
$message = isset( $_POST['message'] ) ? preg_replace( "/(From:|To:|BCC:|CC:|Subject:|Content-Type:)/", "", $_POST['message'] ) : "";
$message .= "\n\nTelephone: $phone\n\n";

// If all values exist, send the email
if ( $fName && $lname && $email && $when && $message ) {
  $recipient = RECIPIENT_NAME . " <" . RECIPIENT_EMAIL . ">";
  $headers = "From: " . $fName . " " . $lname . " <" . $email . ">";
  $success = mail( $recipient, EMAIL_SUBJECT, $message, $when, $headers );
}

// Return an appropriate response to the browser
if ( isset($_GET["ajax"]) ) {
  echo $success ? "success" : "error";
} else {
?>
<html>
  <head>
    <title>Thank you</title>
  </head>
  <body>
  <?php if ( $success ) echo "<p>Thank you for sending your message.</p>" ?>
  <?php if ( !$success ) echo "<p>There was a problem sending your email.</p>" ?>
  <p>Please <a href='javascript:history.go(-1)'>go back</a> and try again.</p>
  </body>
</html>
<?php
}
?>

Re: Can someone see what's wrong with my form?

Posted: Sat Nov 22, 2014 2:08 pm
by Celauran
lastchild wrote:Sometimes it will error out when I submit
Can you elaborate on what that means?

Re: Can someone see what's wrong with my form?

Posted: Sat Nov 22, 2014 5:29 pm
by lastchild
Celauran wrote:
lastchild wrote:Sometimes it will error out when I submit
Can you elaborate on what that means?
You pretty much name it. Many times it will tell me the captcha was incorrect, or sometimes it will submit and say thank you for submitting but then the first name, last name, email, phone are not on the email I receive. It's a mess..

Resolved: Can someone see what's wrong with my form?

Posted: Sat Nov 22, 2014 10:14 pm
by lastchild
Never mind. I'm good now