Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Problem with AJAX contact form
Old 12-03-2010, 05:32 PM Problem with AJAX contact form
Novice Talker

Posts: 6
Name: Pero
Trades: 0
I'm using the Jquerys $.ajax() function to process the contact form on page I am building.
I am also using Jquery for the following effects to happen when the form is submitted - 1st to fadeout the contact form details, 2nd to change the css class of the submit button (I am using a custom image for this, it changes the button text from "send it!" to "working"), 3rd to show the results from the php echo in place of the forms, 4th to fadeout the submit button itself.

But doing that I have run into 2 problems, a minor and a major one.
Minor problem is - ".fadeOut" does not seem to work, neither does ".hide("slow")". Only thing that works is ".hide()".
Major problem is - No email is sent. I have tested the php without the AJAX and it worked, so the problem must be in the Jquery coding.

The XHTML of the form looks like this:

HTML Code:
<body>
<div id="main_wrapper">

<form action="formmail.php" method="post" name="contact_form" id="contact_form">

<!-- The ".loader" div class is used later in the JQuery. This is where the ajax "success" text will show up-->
<div class="loader"></div>

<!-- I use the ".section" span to mark the parts of the form that need to disappear upon submit -->
<span class="section">

<!-- the input forms themselves. I have a "name", "email" and "message" form -->

<label for="name" class="label">Your full name:*</label>
<input type="text" name="name" id="name" tabindex="10"  class="form" />
<br />
<label for="email" class="label" >Your e-mail:*</label>
<input type="text" name="email" id="email" tabindex="20"  class="form" />
<br />
<label for="message" class="label" >Your message:</label>
<textarea name="message" id="message" cols="45" rows="5" tabindex="30"   class="form"></textarea>
</span>
<br />

<!--The button-->
<input name="submit" type="submit" id="Submit" tabindex="40" value="Send!" />

</form>
</div>
The JQuery:

Code:
$(function() {
    //to trigger ajax on submit
    $('#contact_form').submit( function(){
        
        //to hide the form elements, I would like this to be with a delay, or a ".fadeOut" but from some reason it does not work that way

        $('.section').hide();

        //to change the look of the submit button
        $(':submit').addClass("working");
        
    
        //to send the ajax request  
        
        $.ajax({
            type: 'post',
            url: 'formmail.php',
            data: 'name=' + $('#name').val() + '&email=' + $('#email').val() + '$comment=' + $('#message').val(),
            success: function(data){
//to hide the button, again neither hide delay nor ".fadeOut" work.
            button.hide();
            $('.loader').append(data); }    
    });
    return false;
    });
});
And the PHP:
PHP Code:
<?php
  $name
=addslashes($_POST['name']);
  
$email=addslashes($_POST['email']);
 
$comment=addslashes($_POST['message']);

 
//I have a real email set in the actual php, don't worry :)
  
$toemail "myemail@mysite.com";
  
$subject "some subject";

  
$headers "MIME-Version: 1.0\n"
            
."From: \"".$name."\" <".$email.">\n"
            
."Content-type: text/html; charset=iso-8859-1\n";

  
$body "Name: ".$name."<br>\n"
            
."Email: ".$email."<br>\n"
            
."Comments:<br>\n"
            
.$comment;
?>

<!--Display a thankyou message in the callback -->
<h1><span>Thank you  <h10><?php echo $name ?></h10></span></h1>
<p>
  <span>Your message will be answered as soon as possible.</span>
</p>
I also use Dreamweavers Spry framework for form validation before submit, but I don't think there is a need to post those scripts here, since they are not connected to the AJAX.

I have hosted the form on a free host for practice HERE.


Last edited by EnfantSauvage; 12-07-2010 at 07:42 PM..
EnfantSauvage is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 12-04-2010, 12:33 AM Re: Problem with AJAX contact form
Super Spam Talker

Posts: 879
Name: Paul W
Trades: 0
I can't see anything in the last php that actually tries to send an email (and there's a semicolon missing on the thank you line).
PaulW is online now
Reply With Quote
View Public Profile
 
Old 12-04-2010, 09:52 AM Re: Problem with AJAX contact form
Novice Talker

Posts: 6
Name: Pero
Trades: 0
Ok, I changed the PHP to this:

PHP Code:
<?php
  $name
=addslashes($_POST['name']);
  
$email=addslashes($_POST['email']);
  
$comment=addslashes($_POST['message']);

 
// you can specify which email you want your contact form to be emailed to here

  
$toemail "myemail@mysite.com";
  
$subject "some subject";

  
$headers "MIME-Version: 1.0\n"
            
."From: \"".$name."\" <".$email.">\n"
            
."Content-type: text/html; charset=iso-8859-1\n";

  
$body "Name: ".$name."<br>\n"
            
."Email: ".$email."<br>\n"
            
."Comments:<br>\n"
            
.$comment;

    
mail($toemail$subject$body$headers);
?>
<!--Display a thankyou message in the callback -->
<h1><span>Thank you  <h10><?php echo $name ?></h10></span></h1>
<p>
  <span>Your message will be answered as soon as possible.</span>
</p>
Now I do receive the e-mails only problem is I don't receive the contents of the "$comment" section.
The e-mails I receive look something like this:

Quote:
Name: "the name of the sender"
Email: "the email of the sender" $comment="here is the contents of what the main message should be?!"
Comment:
"Nothing is posted here?!"

Last edited by EnfantSauvage; 12-04-2010 at 10:07 AM..
EnfantSauvage is offline
Reply With Quote
View Public Profile
 
Old 12-04-2010, 03:20 PM Re: Problem with AJAX contact form
Super Spam Talker

Posts: 879
Name: Paul W
Trades: 0
$comment=addslashes($_POST['message']); should be $_POST['comment']and you still have a missing senicolon.
PaulW is online now
Reply With Quote
View Public Profile
 
Old 12-04-2010, 04:08 PM Re: Problem with AJAX contact form
Novice Talker

Posts: 6
Name: Pero
Trades: 0
I've done that, yet it changes nothing. The emails are still messed up, and from some reason, now the AJAX fails to return the "Thank you" message?!
EnfantSauvage is offline
Reply With Quote
View Public Profile
 
Old 12-07-2010, 05:53 PM Re: Problem with AJAX contact form
Novice Talker

Posts: 6
Name: Pero
Trades: 0
Update - I have successfully fixed the problems with the PHP, as well as the problem with the welcome message.

The only thing I need help with now is how to fade out the contact forms instead of instantly hiding them when the forms are submitted.

Last edited by EnfantSauvage; 12-07-2010 at 05:59 PM..
EnfantSauvage is offline
Reply With Quote
View Public Profile
 
Old 12-07-2010, 07:02 PM Re: Problem with AJAX contact form
CSM
CSM's Avatar
Front-End Developer

Posts: 297
Name: Michael Pehl
Location: Palma de Mallorca
Trades: 0
First of all:

Code:
<div align="center" id="glaven_wrapper">
div align is obsolete in XHTML 1.0 Transitional. Use CSS to style your elements.

I do not see any reason for using jQuery UI. The SpryValidation crap, errrm... validation stuff could be done jQuery validation script.

The XHTML markup is a little bit ... strange. Using <span>'s for the form elements is not the right way, a fieldset would make more sense... hm, anyway.

The jQuery code is a little bit messed, too.

If you want to fadeOut your form when it was sent successfully, you should take a look at your .ajax success function.

You are mixing JS and jQuery ... hm, okay (why not using jQuery instead "document.getElementById").

You have 6 XHTML errors on your page.

http://validator.w3.org/check?uri=ht...Inline&group=0

Do not use ID's for elements more than once (the id="nad_forma" is used several times).

You may take a look at this demo and check Martin's code!

http://tutorialzine.com/2009/09/fancy-contact-form/
__________________
Chief Web Officer / Front-End Developer / System Engineer

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
CSM is offline
Reply With Quote
View Public Profile Visit CSM's homepage!
 
Old 12-07-2010, 07:51 PM Re: Problem with AJAX contact form
Novice Talker

Posts: 6
Name: Pero
Trades: 0
Spry places the <span> tags automatically, and I started learning Javascript/Jquery bout 2 weeks ago, so that is why the exercise site is so full full of both spry and spans

Anyways, thanks for the link and the advice. It will help a lot.
EnfantSauvage is offline
Reply With Quote
View Public Profile
 
Old 12-09-2010, 06:07 AM Re: Problem with AJAX contact form
CSM
CSM's Avatar
Front-End Developer

Posts: 297
Name: Michael Pehl
Location: Palma de Mallorca
Trades: 0
Hehe, jQuery is very helpful

I run a linklist with over 500 jQuery scripts/plugins
__________________
Chief Web Officer / Front-End Developer / System Engineer

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
CSM is offline
Reply With Quote
View Public Profile Visit CSM's homepage!
 
Reply     « Reply to Problem with AJAX contact form
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.35400 seconds with 12 queries