JQuery AJAX form process without refresh - php

I am new to JQuery and havent used AJAX at all. I have searched the web for days with no complete answer.
Im trying to have a form be emailed via AJAX while JQuery hides the form and displays additional data. The JQuery portions works as the pages switches to display the proper data but the form information doesnt get emailed.
JQuery/AJAX
$(document).ready(function() {
//When the form is submitted...
$('form').on('submit',function(e) {
//Send the serialized data to mailer.php.
$.ajax({
url:'mailer.php',
data:$(this).serialize(),
type:'POST',
success:function(data){
console.log(data);
$("#success").show().fadeOut(5000); //=== Show Success Message==
},
error:function(data){
$("#error").show().fadeOut(5000); //===Show Error Message====
}
});
e.preventDefault(); //=== To Avoid Page Refresh and Fire the Event "Click"===
//$.post("mailer.php");
//Take our response, and replace whatever is in the "form2"
//div with it.
$('#form1').hide();
$('#form2').show();
});
});
PHP Submit file mailer.php
<?php
//Grab Posted Data
$fname = strip_tags(htmlentities($_POST['fname']));
$lname = strip_tags(htmlentities($_POST['lname']));
$name = $fname." ".$lname;
$email = strip_tags($_POST['email']);
$phone = strip_tags(htmlentities($_POST['phone']));
$address = strip_tags(htmlentities($_POST['address']));
$city = strip_tags(htmlentities($_POST['city']));
$state = strip_tags(htmlentities($_POST['state']));
$zip = strip_tags(htmlentities($_POST['zip']));
$country = strip_tags(htmlentities($_POST['country']));
$message = strip_tags(htmlentities($_POST['goals']));
// PREPARE THE BODY OF THE MESSAGE
$message = '<html><body>';
$message .= '<table rules="all" style="border-color: #666;" cellpadding="10">';
$message .= "<tr style='background: #eee;'><td><strong>Name:</strong> </td><td>$name</td></tr>";
$message .= "<tr><td><strong>Email:</strong> </td><td>$email</td></tr>";
$message .= "<tr><td><strong>Phone:</strong> </td><td>$phone</td></tr>";
$message .= "<tr><td><strong>Address:</strong> </td><td>$address</td></tr>";
$message .= "<tr><td> </td><td>$city, $state $zip $country</td></tr>";
$message .= "<tr><td><strong>Goals:</strong> </td><td>$message</td></tr>";
$message .= "</table>";
$message .= "</body></html>";
// CHANGE THE BELOW VARIABLES TO YOUR NEEDS
$to = 'me#me.com';
$subject = 'Website Change Reqest';
$headers = "From: $email \r\n";
$headers .= "Reply-To: $email \r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
if (mail($to, $subject, $message, $headers)) {
echo 'Your message has been sent.';
} else {
echo 'There was a problem sending the email.';
}
?>

PHP mail() uses sendmail system, and most of the problems happens when it is not set up properly. Sometimes webhosters disable it outright.
Sometimes, i solved this problem by using an external library, PhpMailer, for example, and SMTP with a real mail account.
On a side note - never ever do this:
$fname = strip_tags(htmlentities($_POST['fname']));
$lname = strip_tags(htmlentities($_POST['lname']));
$name = $fname." ".$lname;
$email = strip_tags($_POST['email']);
$phone = strip_tags(htmlentities($_POST['phone']));
$address = strip_tags(htmlentities($_POST['address']));
$city = strip_tags(htmlentities($_POST['city']));
$state = strip_tags(htmlentities($_POST['state']));
$zip = strip_tags(htmlentities($_POST['zip']));
$country = strip_tags(htmlentities($_POST['country']));
$message = strip_tags(htmlentities($_POST['goals']));
It can be easily replaced by something like:
extract(
array_map(
function($elem) {
return strip_tags(html_entities($elem));
}, $_POST)
);

I've already got a problem like this. The issue was not in javascript code, but in php's mail() function.
Happened that nowadays most mail servers do not support unauthenticated e-mail to be sent. See that in order to use mail() function you never provided an username or password to log in a mail server.
My issue was solved using the PHPMailer class (available at http://phpmailer.worxware.com), in witch I provide mailserver's address, account username and password. Thought it is quite easy to create an HTML e-mail with attachments without worry about writing long header codes.

Did you use Denwer for test this example?

Related

PHP error in form spam prevention

I have a problem with this code returning nothing but a blank page... I have followed several different tutorials to try and get this working, I am not a proficient PHP coder at all - But do have a little understanding. My server however, doesn't show error messages. So pinpointing this is rather hard for me to do!
I added this top section here to prevent spam using a hidden field on the html page that posts to this email page.
<?php
$if(isset($_POST['subject'],$_POST['Customer'],$_POST['Email'],$_POST['Phone'],$_POST['Comment'],$_POST['Product'],$_POST['Amount'],$_POST['Valid'])) {
$if(isset($_POST['Name']) && !empty($_POST['Name'])) {
echo "Spam Detected!";
Die();
}
Here is the email part that is supposed to send email once the above section determines that field "name" is empty.
$subject = $_POST['subject'];
$name = $_POST['Customer'];
$from = $_POST['Email'];
$phone = $_POST['Phone'];
$message = $_POST['Comment'];
$prod = $_POST['Product'];
$cash = $_POST['Amount'];
$valid = $_POST['Valid'];
$to = "email#email.com";
$body = 'HTML EMAIL CONTENT HERE...';
$headers .= "From: $from " . "Subject";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
mail($to,$subject,$body,$headers);
}
?>
Any and all help will be greatly appreciated!
Thanks
use this i see your code you use $if try if without $
if(isset($_POST['subject'],$_POST['Customer'],$_POST['Email'],$_POST['Phone'],$_POST['Comment'],$_POST['Product'],$_POST['Amount'],$_POST['Valid'])) {
if(isset($_POST['Name']) && !empty($_POST['Name'])) {
echo "Spam Detected!";
Die();
}
}

Use an Ajax script for a mail function

Can I use this part to send mails?
So if I add some php code in action.php, is this sufficient for a working email script (apart from the php code)?
This is the code, (and it's in a function don't worry this is not everything on the page)
And I know there is not a subject field, but I'll just enter the name (voornaam).
var voornaam = $('#voornaam').val();
var achternaam = $('#achternaam').val();
var telefoonnummer = $('#telefoonnummer').val();
var email = $('#email').val();
$.post('action.php',{action: "button", voornaam:voornaam, achternaam:achternaam, telefoonnummer:telefoonnummer, email:email},function(res){
$('#result').html(res);
});
document.getElementById('goed').innerHTML = 'Verstuurd!';
PHP support send mail function if the web server have correct setting,
try to research for this.
And you can use plugin like PHPmailer too.
The following is a simple example to send html email use mail function.
Just put the code into action.php will be OK.
<?php
$voornaam = $_POST['voornaam'];
$achternaam = $_POST['achternaam'];
$telefoonnummer = $_POST['telefoonnummer'];
$email = $_POST['email'];
$subject = 'Sample mail';
$headers = 'MIME-Version: 1.0' ."\r\n";
$headers .= "Content-type: text/html; charset=utf-8\r\n";
$content = '<html><header><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </header><body>';
$content .= $voornaam;
$content .= $achternaam;
$content .= $telefoonnummer;
$content .= '</body></html>';
mail($email, $subject, $content, $headers); //This method sends the mail.
echo "Your email was sent!"; // success message
?>

how to resolve the situation if email does not received?

I wrote a code using php, html and css. This an contact form that is received by an email after submission. There is no error while submitting but i am not getting the email. I am including the code here. This is the link: http://complaintdesk.byethost15.com/contact.php.
Also I incude the code here.
contact.php
<?php
if(isset($_POST['submit'])){
$name = $_POST['fullname'];
$branch = $_POST['branch'];
$usn = $_POST['usn'];
$sem = $_POST['sem'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$description = $_POST['comment'];
$to = 'email#example.com';
$display = 'From:</br>Name: $name</br>USN: $usn</br>Branch: $branch</br>Semester: $sem</br>Email: $email</br></br>$description';
mail($to,$subject,$display);
echo "<script>alert('Your Complaint has been succesfully submitted, We will contact you soon.')</script>";
};
?>
And the rest of code i am not including...
Try this:
$to_address = "Your email address";
$subject = $_POST['subject'];
$headers = "MIME-Version: 1.0\r\n";
$headers.= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers.= "From: ".$email."\r\n";
if (mail($to_address,$subject,$description,$headers)){
echo "Success";
}
else{
echo "<h2>Unable to submit the form, please recheck the details.</h2>" ;
}
Update: If this does not work then there might be an error with your email configuration. As suggested by other users in the comments above, you might want to look into that part too. This answer is based on assumption that your email server/PHP is configured correctly.

Can you insert an if statement in php email

// from the form
$name = trim(strip_tags($_POST['name']));
$email = trim(strip_tags($_POST['email']));
$message = htmlentities($_POST['message']);
// set here
$subject = "Contact form submitted!";
$to = 'your#email.com';
$body = HTML
$message
HTML;
$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";
// send the email
mail($to, $subject, $body, $headers);
// redirect afterwords, if needed
header('Location: thanks.html');
The question is, weather or not you can insert a php if statement inside the html email body or the best way to add it in there. I'm creating a detailed inventory and once submitted, it needs to be email to an email. But I don't want to email the full Inventory (including empty inputs), It should only email the fields that have something other than 0. I was thinking:
if ($armChair > 0) { echo 'Arm Chairs: ' . $_POST['armChair']; } ]
But it doesn't seem to actually work... any ideas?
This is a badly constructed question.
Yes, you can definitely do that but you need to do it in a way that is sane. We can't tell by what you posted here since you just plugged in "HTML" where the body is defined.
$body = "Hi there,\r\n";
$body .= $armChair > 0 ? "Arm Chairs: ".$_POST['armCharis']."\r\n" : "";
$body.= "some more text";
If you mean overwrite a segment of $message, yes you can do that as well using something like machine tags {INVENTORY} or the likes...
$message = $armChair > 0 ? str_replace('{INVENTORY}', "Arm Chairs: ".$_POST['armCharis']."\r\n", $message) : "";
which of course requires the string {INVENTORY} somewhere in your $message var

Correctly encode characters in a PHP mail form ("I'm" turns to be "I\'m")

I'm testing a PHP mail form, a very barebones one, found here:
<?php
if(isset($_POST['submit']))
{
//The form has been submitted, prep a nice thank you message
$output = '<h3>Thanks for your message</h3>';
//Deal with the email
$to = 'mymail#mail.com';
$subject = 'you have a mail';
$contactname = strip_tags($_POST['contactname']);
$adress = strip_tags($_POST['adress']);
$contactemail = strip_tags($_POST['contactemail']);
$textmessage = strip_tags($_POST['textmessage']);
$boundary =md5(date('r', time()));
$headers = "From: My Site\r\nReply-To: webmaster#mysite.com";
$message = "Name: ".$contactname."\n";
$message .= "Adress: ".$adress."\n";
$message .= "E-mail: ".$contactemail."\n";
$message .= "Message: ".$textmessage."\n";
mail($to, $subject, $message, $headers);
}
?>
The problem is I'm receiving an unwanted slash "\" everytime I write a single or a double quote in my message, so "I'm" appear as "I\'m" in my mailbox.
I know it have to do with the way PHP distinguishes code quotes from only lecture quotes, but I wouldn't know what to add in my form to get it properly running.
Any help is appreciated,
The easiest thing to do is to turn magic quotes off in php.ini,
magic_quotes_gpc=false
If you can't do that, you need to remove slashes like this,
if (get_magic_quotes_gpc()) {
foreach($_POST as $k => $v) {
$_POST[$k] = stripslashes($v);
}
}
you can try stripslashing your message , something like :
$message = stripslashes($message);

Categories