PHP Contact Form Status Update in DIV - php

Sounds like an odd question but here is what I have. The php has a snippet at the end that says success or failure makes a white screen to display this, I really want it to pop up right under the submit button on the same page rather than redirect the entire page.
I assume I could direct that to an empty <div>?
mail.php:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$formcontent=" From: $name \n Phone: $phone \n Message: $message";
$recipient = "art#jamisonsigns.com";
$subject = "Web Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!" . " -" . "<a href='index.html' style='text-decoration:none;color:#ff0099;'> Return Home</a>";
?>
With a simple HTML form on my website as follows:
<form class="contact-form" form action="mail.php" method="POST">
<fieldset>
<input type="text" class="name" id="name" placeholder="Name...">
</fieldset>
<fieldset>
<input type="email" class="email" id="email" placeholder="Email...">
</fieldset>
<fieldset>
<input type="text" class="phone" id="phone" placeholder="Phone...">
</fieldset>
<fieldset>
<textarea name="message" id="message" cols="30" rows="4" placeholder="Message.."></textarea>
</fieldset>
<fieldset>
<input type="submit" class="button" id="button" value="Send Message">
</fieldset>
</form>

You need to submit the form with Ajax. Then on success insert the message in the html.
$('#button').click(function(event) {
event.preventDefault();
var formdata = $('#formID').serializeArray();
var url = '/urltopost.php';
$.ajax(
{
url : url,
type: "POST",
data: formdata,
success:function(data, textStatus, jqXHR)
{
//data: return data from server
console.log("STATUS:" + textStatus + " | jqXHR:" + jqXHR + " | data:" + data);
},
error: function(jqXHR, textStatus, errorThrown)
{
//if fails
alert("ERROR:" + " -> " +errorThrown);
}
});
});
Here it is a tutorial if you need..

Related

Sending mail not showing all filled forms in inbox using PHP

I'm kind of new to this. I have been working around this from someone a while about sending a mail from an HTML form to my inbox and want to display the filled inputs in my inbox with other messages to display in the inbox.
index.html
<form action="mail.php" method="post">
<section id="left">
<label for="form_name">Name</label>
<input name="form_name" id="form_name" type="text" >
<label for="form_email">Email</label>
<input name="form_email" id="form_email" type="email" >
<label for="form_msg">Message</label>
<textarea name="form_msg" id="form_msg"></textarea>
<input id="submit" class="button" name="submit" type="submit" value="send">
</section>
</form>
mail.php
<?php
if($_POST){
$name = $_POST['form_name'];
$email = $_POST['form_email'];
$message = $_POST['form_msg'];
$phone = $_POST['phone_no'];
//send email
mail("myemail#gmail.com", "This is a Reservation from:" ,Name. ':' . $name, $email, $phone);
}
?>
mail.js
$.ajax({
type: "POST",
url: "email.php",
data: $(form).serialize(),
success: function(){
$('.success').fadeIn(1000);
}
});
mail("kondasmajid#gmail.com", "This is a Reservation from:" ,Name. ':' . $name, $email, $phone);
...that line won't lead nowhere. In PHP, you have to concatenate strings through a dot, so the following line should work:
mail("kondasmajid#gmail.com",
"This is a Reservation from: Name:" . $name . ", Email: " . $email . ", Phone: " . $phone);

PHP email form without Refreshing Page

I am trying to set up my page so that when I a form is submitted it emails and a message is displayed without refreshing the page.
I have tried to do this using jQuery/Ajax however I cannot get any emails sent now
Without the Ajax/jQuery, the PHP works just fine but just doesnt include the refresh feature
Any help would be appreciated
PHP (submitForm.php):
<?php
$name = isset($_POST['name']);
$email = isset($_POST['email']);
$phone = isset($_POST['phone']);
$message = isset($_POST['message']);
$feedback = '';
if($name && $email && $phone && $message) {
$name = ($_POST['name']);
$email = ($_POST['email']);
$phone = ($_POST['phone']);
$message = ($_POST['message']);
}
$to = "arshdsoni#gmail.com";
$subject = 'Soni Repairs - Support Request';
$body = <<<EMAIL
Hi There!
My name is $name.
Message: $message.
My email is: $email
Phone Number: $phone
Kind Regards
EMAIL;
$header = "From: $email";
if($_POST) {
if($name == '' || $email == '' || $phone == '' || $message == '') {
$feedback = "Nothing received!";
}
else {
mail($to, $subject, $body, $header);
$feedback = '*Message Received! You will receive a reply shortly!';
}
}
?>
jQuery/Ajax:
function submitForm() {
var name=document.getElementById('name').value;
var dataString = 'name'+ name;
$.ajax({
type:"post",
url:"submitForm.php",
cache:false,
success: function(html) {
$('#feedback').html(html);
}
});
return false;
}
FORM:
<form id="contact" action="#">
<h3>Get in Touch:</h3>
<h4><span id="star" class="glyphicon glyphicon-asterisk"></span>We aim to reply within 24 hours!</h4>
<fieldset>
<input name="name" placeholder="Your Name" type="text" tabindex="1" required>
</fieldset>
<fieldset>
<input name="email" placeholder="Your Email Address" type="email" tabindex="2" required>
</fieldset>
<fieldset>
<input name="phone" placeholder="Your Phone Number" type="tel" tabindex="3" required>
</fieldset>
<fieldset>
<textarea id="textarea" name="message" placeholder="Describe your problem...." tabindex="5" required></textarea>
</fieldset>
<fieldset>
<button name="submit" type="submit" id="contact-submit submitBtn" data-submit="...Sending" "return submitForm();">Submit</button>
</fieldset>
</form>
Issues:
if you usedocument.getElementById you must use id on your elements
Add the data to your ajax request
HTML:
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function(){
$( "#submitBtn" ).click(function( event ) {
alert('pressed');
//values
var name=document.getElementById('name').value;
var email=document.getElementById('email').value;
var phone=document.getElementById('phone').value;
var message=document.getElementById('message').value;
var dataString = {"name": name, "email":email, "phone": phone, "message":message}
$.ajax({
type:"post",
url:"submitForm.php",
data: dataString,
success: function(html) {
$('#feedback').html(html);
}
});
event.preventDefault();
});
});
</script>
</head>
<body>
<form id="contact" method="POST">
<h3>Get in Touch:</h3>
<h4><span id="star" class="glyphicon glyphicon-asterisk"></span>We aim to reply within 24 hours!</h4>
<fieldset>
<input id="name" placeholder="Your Name" type="text" tabindex="1" required>
</fieldset>
<fieldset>
<input id="email" placeholder="Your Email Address" type="email" tabindex="2" required>
</fieldset>
<fieldset>
<input id="phone" placeholder="Your Phone Number" type="tel" tabindex="3" required>
</fieldset>
<fieldset>
<textarea id="message" placeholder="Describe your problem...." tabindex="5" required></textarea>
</fieldset>
<fieldset>
<button name="submit" id="submitBtn">Submit</button>
</fieldset>
</form>
<div id="feedback"></div>
</body>
</html>
PHP
<?php
if(isset($_POST['name'], $_POST['email'], $_POST['phone'], $_POST['message'])){
//Post data
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
//mail settings
$to = "arshdsoni#gmail.com";
$subject = 'Soni Repairs - Support Request';
$body = <<<EMAIL
Hi There!
My name is $name.
Message: $message.
My email is: $email
Phone Number: $phone
Kind Regards
EMAIL;
if(mail($to, $subject, $body, $header)){
$feedback = '*Message sent! You will receive a reply shortly!';
}else{
$feedback = '*Message failed to send';
}
}else{
$feedback = 'Missing Params';
}
echo $feedback;
This is your ajax, where you went wrong
$.ajax({
type:"post",
url:"submitForm.php",
cache:false,
success: function(html) {
$('#feedback').html(html);
}
});
You forgot to include the data to be posted
$.ajax({
type:"post",
url:"submitForm.php",
cache:false,
data {
name: $('#name').val(),
email: $('#email').val(),
phone: $('#phone').val(),
message: $('#msg').val()
}
success: function(html) {
$('#feedback').html(html);
}
});
The keys name, email, message and phone should be as is; because you used $_POST['name'], $_POST['email'], $_POST['message'] and $_POST['phone'].
The calls$('#name').val(), $('#email').val(), $('#phone').val(), $('#message').val() have been made assuming that your input elements have ids name, email, message and phone respectively. JS uses ids rather than names.
Refer to jQuery Ajax documentation for more details.
I do not see you sending the data to PHP file anywhere in your AJAX code
NOTE: Use serialize() function if you want to send all the form data. Else send it separately like
var formDatas = {name:"name",email:"emailID"};
function submitForm() {
var name=document.getElementById('name').value;
var dataString = 'name'+ name;
var formDatas = $('#formID').serialize(); // Send form data
$.ajax({
type:"post",
url:"submitForm.php",
data : {datas : formDatas }
cache:false,
success: function(html) {
$('#feedback').html(html);
}
});
return false;
}
Your references
jQuery Serialize()
jQuery ajax()

FORM HELP - Getting PHP script to email data sent in via ajax

Very new to this so apologies is my question is basic but it's stumped me for some hours.
I have a contact form which uses a bit of AJAX to POST to my PHP file. The problem im having is that when i try to get the PHP to email that data to myself, it isn't reading the POST values. The PHP was working when i tested it without the JS/AJAX post method, but in order to stop the page refresh I have gone with the AJAX option. Now that i've added the ajax in, the email result isn't showing up with the data (I can inspect and see that the Form Data is filled, just unclear why it isnt populating in the email) :
Name:
Email:
Message:
Any guidance as to why my mail server only receives the text but not the POST
Opening & Closing HTML on form:
<form data-parsley-validate action="./assets/contactform.php" method="POST">
<div class="formRow">
<label for="name" class="obscure">Name</label>
<input type="text" name="nameInput" id="name" placeholder="Name" required data-parsley-error-message="Please enter your name"></input>
</div>
<div class="formRow">
<label for="email" class="obscure">Email</label>
<input name="emailInput" id="email" type="email" placeholder="Email" required data-parsley-error-message="Please enter a valid email address"></input>
</div>
<div class="formRow">
<label for="message" class="obscure">Message</label>
<textarea name="messageInput" id="message">Message</textarea>
</div>
<input type="submit" name="submit"></input>
PHP Script:
<?php
$to = 'hello#kymlangford.com';
$subject = 'Post from your website';
$message = "Name: " . $_POST['nameInput'] . "\n Email: " . $_POST['emailInput'] . "\n Message: " . $_POST['messageInput'];
$headers = 'From: hello#kymlangford.com' . "\r\n" .
'Reply-To: ' . $_POST['emailInput'] . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
exit;
?>
Java for the form:
$(function() {
$.ajax({
type: 'POST',
url: './assets/contactform.php',
data: { name : $('input[name=nameInput]').val(),
email : $('input[name=emailInput]').val(),
message : $('textarea[name=messageInput]').val()
},
dataType: 'json'
})
event.preventDefault();
});
});
--Update 08/07/2014--
Below is the jquery code that solved my problem
// get the data from the form
$('form').submit(function(event){
var fdata = $(this).serializeArray();
fdata.push({ name: 'submit', value: true });
$.post('./assets/contactform.php', fdata, function (data) {
alert('Data Loaded:' + data); });
console.log(fdata);
event.preventDefault();
});
});
I think you can use jquery which is the same like ajax.
Here is the HTML code
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function(){
$('#submit').click(function(){
$.post("sendemail.php", $("#contactform").serialize(), function(response)
{
$('#message').html(response);
});
return false;
});
});
</script>
</head>
<body>
<p>
<form action="" method="post" id="contactform" >
<label for="name">Name:</label><br />
<input type="text" name="name" id="name" /><br />
<label for="email">Email:</label><br />
<input type="text" name="email" id="email" /><br />
<label for="emailcontent">Email Content:</label><br />
<textarea name="emailcontent" id="emailcontent"></textarea><br />
<input type="button" value="Send Email" id="submit" /><div id="message"> </div>
</form>
</p>
</body>
</html>
Here is the PHP code, save it with 'sendemail.php' filename
$name = $_POST['name'];
$email = $_POST['email'];
$emailcontent = $_POST['emailcontent'];
$to = 'youremail#domain.com';
$subject = 'the subject';
$emailcontent = 'FROM: '.$name.' Email: '.$email.'Content: '.$emailcontent;
$headers = 'From: youremail#domain.com' . "\r\n";
mail($to, $subject, $message, $headers);
echo "Your email was sent!";

reCAPTCHA issue using JQuery

I'm having a hard time with this reCAPTCHA integration.
Here's my code so far:
HTML
<div class="form_contact">
<form id="form1" name="form1" method="post" action="#">
<div>
<label for="name">Name:</label>
<input type="text" name="name" id="name" value="" tabindex="1" placeholder="*Required Field" />
</div>
<div>
<label for="name">E-mail address:</label>
<input type="email" name="email" id="email" value="" tabindex="2" placeholder="*Required Field" />
</div>
<div>
<label for="name">Message Subject:</label>
<input type="text" name="msgTitle" id="msgTitle" value="" tabindex="3" placeholder="*Required Field" />
</div>
<div>
<label for="textarea">Message:</label>
<textarea cols="40" rows="8" name="message" id="message" tabindex="4" placeholder="*Required Field"></textarea>
</div>
<div>
<label for="textarea">Spam Blocker:</label>
<div class="captcha">
<?php
require_once('recaptchalib.php');
$publickey = "**********************************";
echo recaptcha_get_html($publickey);
?>
</div>
<a class="myButton" tabindex="5" id="submit" name="submit" href="#"> Submit <i class="fa fa-chevron-circle-right fa-2x"></i></
</form>
</div>
JQuery
<script>
$(function(){
$('.myButton').click(function() {
var data= {
name: $("#name").val(),
email: $("#email").val(),
msgTitle: $("#msgTitle").val(),
message: $("#message").val()
};
$.ajax({
type: "POST",
url: "mail.php",
data: data,
success: function(data){
console.log(data);
}
});
alert('Message Sent!');
return false;
});
});
</script>
MAIL.php
<?php
require_once('recaptchalib.php');
$privatekey = "*******************";
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
if (!$resp->is_valid) {
die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
"(reCAPTCHA said: " . $resp->error . ")");
} else {
$mailto = 'sample#example.com';
$nameField = $_POST['name'];
$emailField = $_POST['email'];
$emailSubject = $_POST['msgTitle'];
$messageField = $_POST['message'];
$body = <<<EOD
<br><hr><br>
Name: $nameField <br>
Email: $emailField <br>
Message: $messageField <br>
EOD;
$headers = "From: $emailField\r\n"; // This takes the email and displays it as who this email is from.
$headers .= "Content-type: text/html\r\n"; // This tells the server to turn the coding into the text.
$success = mail($mailto, $emailSubject, $body, $headers); // This tells the server what to send.
echo 'message sent!';
}
?>
I just want to display the error message below of the <form> if the reCAPTCHA inputs are incorrect.
Thanks in advance.!
Looks like a jQuery question, you can create a placeholder below the form to push in all the errors:
</form>
<div id="errors"></div>
And in your AJAX success call do:
success: function(data){
//clean it in case of multiple attempts
$('#errors').html(data);
}
.html() pastes the data sent from the server.
From the answer above I just wanna add this code if you want to return a callback from your backend
die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
"(reCAPTCHA said: " . $resp->error . ")");
then you may want to change die into echo like so:
echo "The reCAPTCHA wasn't entered correctly. Go back and try it again." .
"reCAPTCHA said: " . $resp->error . "";
then it should be like:
$.ajax({
type: "POST",
url: "mail.php",
data: data,
success: function(data){
$('#errors').html(data)
}
});

How to run php script while adding text to same html page?

So basically, I want to have someone complete a form, run a php script, and instead of pulling up the php file's web page, I want to append text at the end of the form.
Currently, I have a form:
<form action="scripts/email.php" method="POST">
<p>Name: <input type="text" name="name" placeholder="Name"></p>
<p>Email: <input type="text" name="email" placeholder="Email"></p>
<p>Subject: <input type="text" name="subject" placeholder="Subject"></p>
<p>Message: </p>
<p><textarea name="message"></textarea></p>
which looks like this:
When 'Submit' is clicked, it leads to my PHP script:
<?php
$from = $_POST['email'];
$to = "comp#c0mp.org";
$subject = $_POST['subject'];
$name = $_POST['name'];
$message = $_POST['message'];
mail($to, $subject, "Name: " . $name . "\nMessage: " . $message, "From:" . $from);
print "Your message has been sent!";
?>
which leads to a blank web page that just says this in plain text:
"Your message has been sent!"
Basically, what I want is text added onto the submit form after the script is ran, such as:
http://i.stack.imgur.com/zha4d.png
How can I achieve running a PHP script on a web page, and instead of loading a new web page of the PHP script's path, but append text on the current HTML web page?
TRY THIS : WRITE THIS CODE INTO A SINGLE PAGE.
<form action="<?php $PHP_SELF ;?>" method="POST">
<p>Name: <input type="text" name="name" placeholder="Name"></p>
<p>Email: <input type="text" name="email" placeholder="Email"></p>
<p>Subject: <input type="text" name="subject" placeholder="Subject"></p>
<p>Message: </p>
<p><textarea name="message"></textarea></p>
<input type="submit" name="submit" value="Submit">
</form>
<?php
if (isset($_POST['submit'] ))
{
$from = $_POST['email'];
$to = "comp#c0mp.org";
$subject = $_POST['subject'];
$name = $_POST['name'];
$message = $_POST['message'];
mail($to, $subject, "Name: " . $name . "\nMessage: " . $message, "From:" . $from);
echo "Your message has been sent";
}
?>
you can use ajax for acheive more secure and fast execution of script.
HTML code:
<form method="POST">
<p>Name: <input type="text" id="name" name="name" placeholder="Name"></p>
<p>Email: <input type="text" id="email" name="email" placeholder="Email"></p>
<p>Subject: <input type="text" id="subject" name="subject" placeholder="Subject"></p>
<p>Message: </p>
<p><textarea id="area" name="message"></textarea></p>
<input type="submit" name="submit" value="Submit" onclick="mail_js();">
</form>
<div id="success_msg" style="display:none;"></div>
Just give proper style to this div.
JS code:
function mail_js()
{
var input_val=$('#name').val();
var email_val=$('#email').val();
var subject_val=$('#subject').val();
var message_val=$('#area').val();
jQuery.ajax({
type: 'post',
data:{
'input_val':input_val,
'email_val':email_val,
'subject_val':subject_val,
'message_val':message_val
},
url: 'email.php',
success:function(response){
$("#success_msg").append(response).show();
},
error: function(errorThrown){
alert('error');
console.log(errorThrown);
}
});
}
PHP script:
$from = $_POST['email_val'];
$to = "comp#c0mp.org";
$subject = $_POST['subject_val'];
$name = $_POST['input_val'];
$message = $_POST['message_val'];
mail($to, $subject, "Name: " . $name . "\nMessage: " . $message, "From:" . $from);
echo "Your message has been sent";

Categories