Jump to content

AJAX/PHP problem


Noskiw

Recommended Posts

Basically, the problem I am having is that when I send an e-mail through the server to my Hotmail account, and I'm receiving the e-mail, but with nothing on it, no email from the user, no message, and no subject.

 

Also, I have another problem, I'm testing out to make sure that no e-mail is sent through without proper validation of the fields, so that nothing can be sent without all the fields being entered. Now while this is working, when the user clicks send, it is still saying that the email has been sent through an alert message.

 

This is my Ajax/jQuery code:

 

$(document).ready(function(){
    $(".error").hide();
    $(".send").click(function(){
        var name = $("input#name").val();
        if(name == ""){
            $("label#name_error").show();
            $("input#name").focus();
        }
        var email = $("input#email").val();
        if(email == ""){
            $("label#email_error").show();
            $("input#email").focus();
        }
        var subject = $("input#subject").val();
        if(subject == ""){
            $("label#subject_error").show();
            $("input#subject").focus();
        }
        var textarea = $("textarea#textarea").val();
        if(textarea == ""){
            $("label#textarea_error").show();
            $("input#textarea").focus();
        }
        
        var dataString = name + email + subject + textarea;
    
        $.ajax({
            type: "POST",
            data: dataString,
            url: "./include/process_contact.php",
            success: function(){
                alert("Your email has been sent!");
            }else{
                alert("Message not sent!")
            }
        });
    });
});

 

And this is my process_contact.php code:

 

<?php
    $to = "noskiw@hotmail.co.uk";
    $email = $_GET['email'];
    $subject = $_GET['subject'];
    $message = $_GET['textarea'];
    $headers = "";
    
    mail($to, $email, $subject, $message, $headers)
?>

 

If anyone would care to help, it would be greatly appreciated.

 

P.S I've already tried $_POST

Link to comment
Share on other sites

. firstly trhere is no semi colon on the end of your mail function. try seeing if the email was actually sent. The success message only shows that the ajax request executed, not that the email has sent. try something like this:

$(document).ready(function(){
    $(".error").hide();
    $(".send").click(function(){
        var name = $("input#name").val();
        if(name == ""){
            $("label#name_error").show();
            $("input#name").focus();
        }
        var email = $("input#email").val();
        if(email == ""){
            $("label#email_error").show();
            $("input#email").focus();
        }
        var subject = $("input#subject").val();
        if(subject == ""){
            $("label#subject_error").show();
            $("input#subject").focus();
        }
        var textarea = $("textarea#textarea").val();
        if(textarea == ""){
            $("label#textarea_error").show();
            $("input#textarea").focus();
        }
        
        var dataString = name + email + subject + textarea;
    
        $.ajax({
            type: "POST",
            data: dataString,
            url: "./include/process_contact.php",
            success: function(data){
                alert(data);
            }else{
                alert("there was an issue with the ajax request")
            }
        });
    });
});

 

<?php
    $to = "noskiw@hotmail.co.uk";
    $email = $_GET['email'];
    $subject = $_GET['subject'];
    $message = $_GET['textarea'];
    $headers = "";
    
   $send =  mail($to, $email, $subject, $message, $headers);
    if ($send){
  echo 'email sent';
}
else
{
echo ' no email sent';
}
?>

Link to comment
Share on other sites

try changing your javascript to


$(document).ready(function(){
    $(".error").hide();
    $(".send").click(function(){
        var name = $("input#name").val();
        if(name == ""){
            $("label#name_error").show();
            $("input#name").focus();
        }
        var email = $("input#email").val();
        if(email == ""){
            $("label#email_error").show();
            $("input#email").focus();
        }
        var subject = $("input#subject").val();
        if(subject == ""){
            $("label#subject_error").show();
            $("input#subject").focus();
        }
        var textarea = $("textarea#textarea").val();
        if(textarea == ""){
            $("label#textarea_error").show();
            $("input#textarea").focus();
        }
        
        var dataString = name + email + subject + textarea;
    
        $.ajax({
            type: "POST",
            data: dataString,
            url: "./include/process_contact.php",
            success: function(response){
                alert(response);
            }else{
                alert("there was an issue with the ajax request")
            }
        });
    });
});

 

and tell me what happens

Link to comment
Share on other sites

ok, this is going to be a bit difficult without knowing what the ajax is doing: put your code back as it was. Which browser are you using? If its firefox or chrome then install firebug as a plugin and google "debugging ajax with firebug". this basically lets you see the ajax request being sent and what response you actually recieve. Along with any javascript errors that you may recieve

Link to comment
Share on other sites

First of all you are posting the data and looking for get data in your php code. Also it looks like you are just passing the php script one string try the following:-

 

 
$.ajax({
    type: "POST",
    data: {
        name: name,
        email: email,
        subject: subject,
        textarea: textarea
    },
    url: "./include/process_contact.php",
    success: function(data){
        alert(data);
    }
});

<?php
$to = "noskiw@hotmail.co.uk";

// I would do some validating here
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['textarea'];
$headers = "From: {$_POST['name']}";

mail($to, $email, $subject, $message, $headers);

echo "Email sent"; //or better yet some json including any error/success
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.