Noskiw Posted July 4, 2011 Share Posted July 4, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/241082-ajaxphp-problem/ Share on other sites More sharing options...
gristoi Posted July 5, 2011 Share Posted July 5, 2011 . 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'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/241082-ajaxphp-problem/#findComment-1238476 Share on other sites More sharing options...
Noskiw Posted July 5, 2011 Author Share Posted July 5, 2011 I tried both of that, and adding the else statement to my js script messes it up, and it still isn't sending the email. Quote Link to comment https://forums.phpfreaks.com/topic/241082-ajaxphp-problem/#findComment-1238485 Share on other sites More sharing options...
gristoi Posted July 5, 2011 Share Posted July 5, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/241082-ajaxphp-problem/#findComment-1238496 Share on other sites More sharing options...
Noskiw Posted July 5, 2011 Author Share Posted July 5, 2011 The email still doesn't send and the "This Field is required" label is shown on loading and it stays there. Also, when I try and click send, the alert doesn't pop up... Quote Link to comment https://forums.phpfreaks.com/topic/241082-ajaxphp-problem/#findComment-1238500 Share on other sites More sharing options...
gristoi Posted July 5, 2011 Share Posted July 5, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/241082-ajaxphp-problem/#findComment-1238507 Share on other sites More sharing options...
Noskiw Posted July 5, 2011 Author Share Posted July 5, 2011 I still need a bit of help, I've been running through this all day, and I can't find a fault other than the code isn't executing the mail function. So it's definitely the AJAX. Quote Link to comment https://forums.phpfreaks.com/topic/241082-ajaxphp-problem/#findComment-1238696 Share on other sites More sharing options...
isedeasy Posted July 5, 2011 Share Posted July 5, 2011 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 ?> Quote Link to comment https://forums.phpfreaks.com/topic/241082-ajaxphp-problem/#findComment-1238716 Share on other sites More sharing options...
Noskiw Posted July 5, 2011 Author Share Posted July 5, 2011 On the alert (data) pop-up, no data was displayed. I don't quite know what is wrong with this, but I think it's both files, or just the AJAX. Quote Link to comment https://forums.phpfreaks.com/topic/241082-ajaxphp-problem/#findComment-1238718 Share on other sites More sharing options...
isedeasy Posted July 6, 2011 Share Posted July 6, 2011 Make sure error reporting is on and use firebug to see what the ajax is returning. Quote Link to comment https://forums.phpfreaks.com/topic/241082-ajaxphp-problem/#findComment-1238783 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.