Jump to content

Php Form - Need Help!


Recommended Posts

Hi, I downloaded a lighbox php form from http://www.elated.co...orm-jquery-php/ which is exactly what I want BUT, I also want to add a captcha to the form! I don't have much knowledge about PHP and I've been trying to figure this out: When I test the form and fill in the Captcha, to my surprise, it goes through 100% with no faults but if I purposely type the incorrect code in the captcha and click send, the lightbox returns a message "sending your message, please wait" and that message doesn't go away. The message I want to see and what is supposed to come up is "There was a problem sending your message. Please try again". I feel I'm so close to getting this right, please could you let me know what is wrong with my php below:

<?php// Define some constantsdefine( "RECIPIENT_NAME", "Joe Soap" );define( "RECIPIENT_EMAIL", "JoeSoap@myemail.com" );define( "EMAIL_SUBJECT", "Visitor Message" );// Read the form values$success = false;$senderName = isset( $_POST['senderName'] ) ? preg_replace( "/[^\.\-\' a-zA-Z0-9]/", "", $_POST['senderName'] ) : "";$senderEmail = isset( $_POST['senderEmail'] ) ? preg_replace( "/[^\.\-\_\@a-zA-Z0-9]/", "", $_POST['senderEmail'] ) : "";$message = isset( $_POST['message'] ) ? preg_replace( "/(From:|To:|BCC:|CC:|Subject:|Content-Type:)/", "", $_POST['message'] ) : "";session_start();include("verification_image.class.php");$image = new verification_image();if (($image->validate_code($_POST['validate']) ? "true" : "false") == "false") {  header('Location: http://www.testing123.co.za/fail.htm');  exit;}// If all values exist, send the emailif ( $senderName && $senderEmail && $message ) {  $recipient = RECIPIENT_NAME . " <" . RECIPIENT_EMAIL . ">";  $headers = "From: " . $senderName . " <" . $senderEmail . ">";  $success = mail( $recipient, EMAIL_SUBJECT, $message, $headers );}// Return an appropriate response to the browserif ( isset($_GET["ajax"]) ) {  echo $success ? "success" : "error";} else {?><html>  <head>	<title>Thanks!</title>  </head>  <body>  <?php if ( $success ) echo "<p>Thanks for sending your message! We'll get back to you shortly.</p>" ?>  <?php if ( !$success ) echo "<p>There was a problem sending your message. Please try again.</p>" ?>  <p>Click your browser's Back button to return to the page.</p>  </body></html><?php}?> 

The code I added to try get the Captcha working is:

session_start();include("verification_image.class.php");$image = new verification_image();if (($image->validate_code($_POST['validate']) ? "true" : "false") == "false") {  header('Location: http://www.testing123.co.za/fail.htm');  exit;}

When you fill in the form and put the incorrect captcha code in, I want the error message: "There was a problem sending your message. Please try again" to return in the lightbox like its supposed to and NOT go to location address: http://www.testing123.co.za/fail.htm. So basically all I want is for that error message to show! Please me know, thanks in advance!!

Edited by reflex84
Link to post
Share on other sites
When you fill in the form and put the incorrect captcha code in, I want the error message: "There was a problem sending your message. Please try again" to return in the lightbox like its supposed to and NOT go to location address: http://www.testing123.co.za/fail.htm. So basically all I want is for that error message to show!
well, that's exactly what is happening here. take out the redirect (header) and the exit if you don't want it to redirect.
session_start();include("verification_image.class.php");$image = new verification_image();if (($image->validate_code($_POST['validate']) ? "true" : "false") == "false") {  header('Location: http://www.testing123.co.za/fail.htm');  exit;}

When you fill in the form and put the incorrect captcha code in, I want the error message: "There was a problem sending your message. Please try again" to return in the lightbox like its supposed to and NOT go to location address: http://www.testing123.co.za/fail.htm. So basically all I want is for that error message to show! Please me know, thanks in advance!!

and take this part out. $success is being tested for true/false, not a string. if ( isset($_GET["ajax"]) ) { echo $success ? "success" : "error";} else { and the closing } curly brace for the else Edited by thescientist
Link to post
Share on other sites

Hi, Thanks so much for actually responding on a step by step basis!! Ok, removed what you said above and tested the form, but now the the error message comes up whether I put in the correct captcha code or wrong captcha code. Did I do anything wrong, heres what my php form looks like now:

<?php// Define some constantsdefine( "RECIPIENT_NAME", "Joe Soap" );define( "RECIPIENT_EMAIL", "JoeSoap@myemail.com" );define( "EMAIL_SUBJECT", "Visitor Message" );// Read the form values$success = false;$senderName = isset( $_POST['senderName'] ) ? preg_replace( "/[^\.\-\' a-zA-Z0-9]/", "", $_POST['senderName'] ) : "";$senderEmail = isset( $_POST['senderEmail'] ) ? preg_replace( "/[^\.\-\_\@a-zA-Z0-9]/", "", $_POST['senderEmail'] ) : "";$message = isset( $_POST['message'] ) ? preg_replace( "/(From:|To:|BCC:|CC:|Subject:|Content-Type:)/", "", $_POST['message'] ) : "";session_start();include("verification_image.class.php");$image = new verification_image();if (($image->validate_code($_POST['validate']) ? "true" : "false") == "false") { }// If all values exist, send the emailif ( $senderName && $senderEmail && $message ) {  $recipient = RECIPIENT_NAME . " <" . RECIPIENT_EMAIL . ">";  $headers = "From: " . $senderName . " <" . $senderEmail . ">";  $success = mail( $recipient, EMAIL_SUBJECT, $message, $headers );}// Return an appropriate response to the browser?><html>  <head>    <title>Thanks!</title>  </head>  <body>  <?php if ( $success ) echo "<p>Thanks for sending your message! We'll get back to you shortly.</p>" ?>  <?php if ( !$success ) echo "<p>There was a problem sending your message. Please try again.</p>" ?>  <p>Click your browser's Back button to return to the page.</p>  </body></html><?php?>

...

Link to post
Share on other sites

The if statement checking the image doesn't do anything if it passed or failed. If it passed then it needs to set the $success variable to true, and you should also be checking the value of $success before you try to send the mail to make sure they've already passed the validation.

Link to post
Share on other sites

Ok, just for heads up, I'm useless at PHP!! I assume you are talking about this code:

if (($image->validate_code($_POST['validate']) ? "true" : "false") == "false") {}

I know you are trying to help, but I am clueless to what to do?What code do I need to add? Thanks guys ...

Link to post
Share on other sites

There is also this javascript code thats in the header of the HTML page I'm trying to get the form to work on:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script><script type="text/javascript">var messageDelay = 2000;  // How long to display status messages (in milliseconds)// Init the form once the document is ready$( init );// Initialize the formfunction init() {  // Hide the form initially.  // Make submitForm() the form's submit handler.  // Position the form so it sits in the centre of the browser window.  $('#contactForm').hide().submit( submitForm ).addClass( 'positioned' );  // When the "Send us an email" link is clicked:  // 1. Fade the content out  // 2. Display the form  // 3. Move focus to the first field  // 4. Prevent the link being followed  $('a[href="#contactForm"]').click( function() {    $('#content').fadeTo( 'slow', .2 );    $('#contactForm').fadeIn( 'slow', function() {	  $('#senderName').focus();    } )    return false;  } );   // When the "Cancel" button is clicked, close the form  $('#cancel').click( function() {    $('#contactForm').fadeOut();    $('#content').fadeTo( 'slow', 1 );  } );   // When the "Escape" key is pressed, close the form  $('#contactForm').keydown( function( event ) {    if ( event.which == 27 ) {	  $('#contactForm').fadeOut();	  $('#content').fadeTo( 'slow', 1 );    }  } );}// Submit the form via Ajaxfunction submitForm() {  var contactForm = $(this);  // Are all the fields filled in?  if ( !$('#senderName').val() || !$('#senderEmail').val() || !$('#message').val() || !$('#validate').val() ) {    // No; display a warning message and return to the form    $('#incompleteMessage').fadeIn().delay(messageDelay).fadeOut();    contactForm.fadeOut().delay(messageDelay).fadeIn();  } else {    // Yes; submit the form to the PHP script via Ajax    $('#sendingMessage').fadeIn();    contactForm.fadeOut();    $.ajax( {	  url: contactForm.attr( 'action' ) + "?ajax=true",	  type: contactForm.attr( 'method' ),	  data: contactForm.serialize(),	  success: submitFinished    } );  }  // Prevent the default form submission occurring  return false;}// Handle the Ajax responsefunction submitFinished( response ) {  response = $.trim( response );  $('#sendingMessage').fadeOut();  if ( response == "success" ) {    // Form submitted successfully:    // 1. Display the success message    // 2. Clear the form fields    // 3. Fade the content back in    $('#successMessage').fadeIn().delay(messageDelay).fadeOut();    $('#senderName').val( "" );    $('#senderEmail').val( "" );    $('#message').val( "" );$('#validate').val( "" );    $('#content').delay(messageDelay+500).fadeTo( 'slow', 1 );  } else {    // Form submission failed: Display the failure message,    // then redisplay the form    $('#failureMessage').fadeIn().delay(messageDelay).fadeOut();    $('#contactForm').delay(messageDelay+500).fadeIn();  }}</script>

and this in the body:

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

Anything I need to edit here?

Link to post
Share on other sites

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.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...