简体   繁体   中英

Email sending in php using jquery

I have to send email in php using jquery but the problem is when i click submit button and if the captcha is correct, it shows the message in alert box ("invalid mail") and Your Query has been received, We will contact you soon."; i really dont know how if else both are executed at same time. if the captcha is not correct, it shows invalid mail and invalid captcha..any one help me to fix this is issue.?

Email.php

<?php
session_start();

$email = '';

$json = array();

if (isset($_POST['Name']))
    $name = $_POST['Name'];
if (isset($_POST['Email']))
    $email = $_POST['Email'];
if (isset($_POST['Message']))
    $message = $_POST['Message'];

$subject = 'Enquiry';
//   $email = filter_var($email, FILTER_SANITIZE_EMAIL);

if (filter_var($email, FILTER_VALIDATE_EMAIL)) {

    //echo filter_var($email, FILTER_VALIDATE_EMAIL);exit;

    if ($_SESSION['add'] == $_POST['captcha']) {


        $subject = $subject;

        $headers = 'MIME-Version: 1.0' . "\r\n";
        $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
        $headers .= 'From:' . $email . "\r\n"; // Sender's Email
        $headers .= 'Cc:' . $email . "\r\n"; // Carbon copy to Sender
        $template = '<div style="padding:50px; color:white;">Hello ' . $name . ',<br/>'
            . '<br/>Thank you...! For Contacting Us.<br/><br/>'
            . 'Name:' . $name . '<br/>'
            . 'Email:' . $email . '<br/>'

            . 'Message:' . $message . '<br/><br/>'
            . 'This is a Contact Confirmation mail.'
            . '<br/>'
            . 'We Will contact You as soon as possible .</div>';
        $sendmessage = "<div style=\"background-color:#7E7E7E; color:white;\">" . $template . "</div>";

        $sendmessage = wordwrap($sendmessage, 70);

        //mail(Configure::read('__general_enquiries'), $subject, $sendmessage, $headers);
        mail('balaji@ramsol.in', $message, $sendmessage, $headers);
        $json['success'] = "Your Query has been received, We will contact you soon.";

    } else {
        //$this->Session->setFlash(_('Invalid Captcha'));

        $json['error'] = "Invalid CAPTCHA";
    }
} else {
    $json['error'] = "Invalid Email";


}
echo json_encode($json);
exit;

//header('location:index.php');
?>

script code:

<script type="text/javascript">

    $(document).ready(function () {
        $('#submit').click(function () {
            var name = $("#name").val();
            var email = $("#email").val();
            var message = $("#message").val();
            var captcha = $("#captcha").val();

            $("#returnmessage").empty();

            if (name == '' || email == '' || message == '' || captcha == '') {
                alert("Please Fill Required Fields");
            }

            else {
                $.ajax({
                    type: 'post',
                    url: 'email.php',
                    dataType: "json",
                    data: {'name': name, 'email': email, 'message': message, 'captcha': captcha},
                    success: function (data) {

                        if (data.success) {
                            alert(data.success);
                            $("#contact-form")[0].reset();
                        } else {
                            alert(data.error);
                        }
                    }

                    //window.location.href = "index.php";
                });
            }
        });

session code:

<?php 

//if(isset($_SESSION)){
session_start();    
$rand = rand(1,9);
$rand1 = rand(1,9);
 $rand =$_SESSION['rand'] = $rand;
 $rand1 =$_SESSION['rand1'] = $rand1;
 $_SESSION['add'] = $rand + $rand1;

?>

html code:

<form id="contact-form" method="post" action="email.php">
            <div class="form-group-inner">
                <div class="form-group">
                    <input type="text" name="Name" id="name" class="form-control" placeholder="Name" />
                </div>
                <div class="form-group">
                    <input type="email" name="Email" id="email" class="form-control" placeholder="Email" />
                </div>
            </div>
            <div class="form-group">
                <textarea class="form-control" name="Message"  id="message" placeholder="Message"></textarea>
            </div>

            <div class="form-group-inner">
                <div class="form-group">
                    <h5 align="right" style="color: white;"><?php echo $_SESSION['rand']; ?> + <?php echo $_SESSION['rand1'];?></h5>
                </div>
                <div class="form-group">
                    <input type="text" name="captcha" id = "captcha" class="form-control" placeholder="Type Here" />
                </div>
            </div>
            <div class="form-action">
                <button type="submit" class="button-btn" name="submit" id="submit">Send</button>
            </div>  
        </form>

Okay, first of all, you should stop submitting of your form, because you are using ajax:

You should change:

    $('#submit').click(function () {

to

    $('#submit').click(function (e) {
        e.preventDefault();

Second, you send data: {'name': name, 'email': email, 'message': message, 'captcha': captcha}, with lower case, but in your php code you check with uppercase of first char:

if (isset($_POST['Name']))
    $name = $_POST['Name'];
if (isset($_POST['Email']))
    $email = $_POST['Email'];
if (isset($_POST['Message']))
    $message = $_POST['Message'];

Final code:

JS:

<script type="text/javascript">

$(document).ready(function () {
    $('#submit').click(function (e) {
        e.preventDefault();
        var name = $("#name").val();
        var email = $("#email").val();
        var message = $("#message").val();
        var captcha = $("#captcha").val();

        $("#returnmessage").empty();

        if (name == '' || email == '' || message == '' || captcha == '') {
            alert("Please Fill Required Fields");
        }

        else {
            $.ajax({
                type: 'post',
                url: 'email.php',
                dataType: "json",
                data: {'name': name, 'email': email, 'message': message, 'captcha': captcha},
                success: function (data) {

                    if (data.success) {
                        alert(data.success);
                        $("#contact-form")[0].reset();
                    } else {
                        alert(data.error);
                    }
                }

                //window.location.href = "index.php";
            });
        }
    });

PHP:

<?php
session_start();

$email = '';

$json = array();

if (isset($_POST['name']))
    $name = $_POST['name'];
if (isset($_POST['email']))
    $email = $_POST['email'];
if (isset($_POST['message']))
    $message = $_POST['message'];

$subject = 'Enquiry';
//   $email = filter_var($email, FILTER_SANITIZE_EMAIL);

if (filter_var($email, FILTER_VALIDATE_EMAIL)) {

    //echo filter_var($email, FILTER_VALIDATE_EMAIL);exit;

    if ($_SESSION['add'] == $_POST['captcha']) {


        $subject = $subject;

        $headers = 'MIME-Version: 1.0' . "\r\n";
        $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
        $headers .= 'From:' . $email . "\r\n"; // Sender's Email
        $headers .= 'Cc:' . $email . "\r\n"; // Carbon copy to Sender
        $template = '<div style="padding:50px; color:white;">Hello ' . $name . ',<br/>'
            . '<br/>Thank you...! For Contacting Us.<br/><br/>'
            . 'Name:' . $name . '<br/>'
            . 'Email:' . $email . '<br/>'

            . 'Message:' . $message . '<br/><br/>'
            . 'This is a Contact Confirmation mail.'
            . '<br/>'
            . 'We Will contact You as soon as possible .</div>';
        $sendmessage = "<div style=\"background-color:#7E7E7E; color:white;\">" . $template . "</div>";

        $sendmessage = wordwrap($sendmessage, 70);

        //mail(Configure::read('__general_enquiries'), $subject, $sendmessage, $headers);
        mail('balaji@ramsol.in', $message, $sendmessage, $headers);
        $json['success'] = "Your Query has been received, We will contact you soon.";

    } else {
        //$this->Session->setFlash(_('Invalid Captcha'));

        $json['error'] = "Invalid CAPTCHA";
    }
} else {
    $json['error'] = "Invalid Email";


}
echo json_encode($json);
exit;

//header('location:index.php');
?>

The problem with your code comes from there, that you are post your form directly to PHP.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM