0

I want to build a a simple sendmail form with jQuery and php. I tried to test the succes with an alert but I see nothing. I think something wrong in the way I call the jQuery function because I don't get the alert. This is my code :

contact.js:

jQuery(function($) {'use strict',

    var form = $('.contact-form');
    alert ('Hello world');
    form.submit(function () {'use strict',
        $this = $(this);
        $.post("sendemail.php", $(".contact-form").serialize(),function(result){
            if(result.type == 'success'){
                $this.prev().text(result.message).fadeIn().delay(3000).fadeOut();
            }
        });
        return false;
    });

});

sendmail.php

header('Content-type: application/json');
$status = array(
    'type'=>'success',
    'message'=>'Thank you for contact us. As early as possible  we will contact you '
);

$name       = @trim(stripslashes($_POST['name'])); 
$email      = @trim(stripslashes($_POST['email'])); 
$subject    = @trim(stripslashes($_POST['subject'])); 
$message    = @trim(stripslashes($_POST['message'])); 

$email_from = $email;
$email_to = 'sallami.ismail@gmail.com';//replace with your email

$body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $message;

$success = @mail($email_to, $subject, $body, 'From: <'.$email_from.'>');

echo json_encode($status);
die;
smile 22121
  • 285
  • 4
  • 20
  • 1
    The first step is to remove all of the ampersands in your PHP, they are suppressing any errors that might help you debug this. I'm guessing you used them because you don't want errors to be breaking your JSON, this question should cover how to do that: http://stackoverflow.com/questions/15949304/turn-off-display-error-php-ini – ThomasRedstone May 12 '16 at 09:17
  • You've written `'use strict',`, but you have to write it like this: `"use strict";`. It doesn't care if you use single or double quotes. But definetively you have to end the statement with semicolon `;` – dlopez May 12 '16 at 09:33

1 Answers1

0

I got a js error : unexpected token var, so I just deleted var form = $('.contact-form'); and used $('.contact-form').submit(function () {'use strict', . I don't know why it worked because the first version looks logical to me.

smile 22121
  • 285
  • 4
  • 20
  • I'm not sure why it even works as you have it. I would think the interpreter would see this as a syntax error. The comma (,) after `'use strict'` denotes that you're adding something to a list. This explains why it wasn't expecting a variable declaration. It was expecting a value to be there. Variable declarations do not return anything and therefore cannot be used as a value. Use a semicolon (;) instead. – Jonathan Gray May 12 '16 at 12:20