php form stay on page with div showing close form - javascript

I have the following form that works except for the success message staying on the same page.
I keep racking my brain, but at this point it is mush.
**Problem
I need to have the form close and show the 'success' DIV when the form is submitted.
Right now the only thing I can do is get it to go to a thanks.php page by using a header.
all the code is here at jsfiddle: http://jsfiddle.net/webs4me/hyArd/1/
except the send.hello.php which is below: - Thanks
<?php
// Clean up the input values
foreach($_POST as $key => $value) {
if(ini_get('magic_quotes_gpc'))
$_POST[$key] = stripslashes($_POST[$key]);
$_POST[$key] = htmlspecialchars(strip_tags($_POST[$key]));
}
// Honeypot don't send - THIS WILL BE HIDDEN LATER
if(!empty($_POST["confirm_email"])) {
header("location:spam.php");exit;
}
// Assign the input values to variables for easy reference
$name = $_POST["name"];
$email = $_POST["email"];
$confirm = $_POST["confirm_email"];
$phone = $_POST["phone"];
$message = $_POST["message"];
// Send the email *********** enter your email address and message info
$to = "myemail#myemail.com";
$subject = "Website message: $name";
$message = "From:\n$name\n\nEmail:\n$email\n\nPhone:\n$phone\n\nMessage:\n$message";
$headers = "From: $email";
mail($to, $subject, $message, $headers);
header('location: thanks.php');
?>

Since you're already using JQuery, use $.POST:
http://api.jquery.com/jQuery.post/
$.ajax({
type: "POST",
url: url,
data: data,
success: success,
dataType: dataType
});
data would be constructed from the values of all the form's inputs.
url would be the hello.php page
You'd add either an onsubmit function to the form, or take out the submit button and add a regular input button with an onclick call to a function that builds data and calls $.POST

Related

php - How to show a pop-up thank you message instead of another page

I am using a form-to-email.php for a contact form in my website, but I don't really understand php code.
The ready-to-use form-to-email.php file has a line "redirect to thank you page," and I have to build a thank you page for the action.
But I hope the UX could be easier like a pop-up thank you message instead of another page.
Anyone could help me to create the lines? Thank you very much!!
Below is the complete code of form-to-email.php, the line "header('Location: thank-you.html')" is the redirect path, and I'm wondering is there any way to modify the lines?
<?php
if(!isset($_POST['submit']))
{
//This page should not be accessed directly. Need to submit the form.
echo "error; you need to submit the form!";
}
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];
$agree = $_POST['agree'];
//Validate first
if(empty($name)||empty($visitor_email))
{
echo "Name and email are mandatory!";
exit;
}
if(IsInjected($visitor_email))
{
echo "Bad email value!";
exit;
}
$email_from = 'receiver#gmail.com';//<== update the email address
$email_subject = "letter from customer";
$email_body = "$name\n".
"Message:\n$message\nLINE ID: $line".
$to = "receiver#gmail.com";//<== update the email address
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
//Send the email!
mail($to,$email_subject,$email_body,$headers);
//done. redirect to thank-you page.
header('Location: thank-you.html');
// Function to validate against any email injection attempts
function IsInjected($str)
{
$injections = array('(\n+)',
'(\r+)',
'(\t+)',
'(%0A+)',
'(%0D+)',
'(%08+)',
'(%09+)'
);
$inject = join('|', $injections);
$inject = "/$inject/i";
if(preg_match($inject,$str))
{
return true;
}
else
{
return false;
}
}
?>
If you want to use JavaScript, you can use this code to show the message "Thank you for your message" in an alert() box:
Replace header('Location: thank-you.html') with:
echo'
<script>
window.onload = function() {
alert("Thank you for your message");
location.href = "index.php";
}
</script>
';
You can also use below AJAX script to handle it. It will not reload the page and it will give you good user experience. You must include jquery library to work.
$.ajax({
url: "ready-to-use form-to-email.php",
type: "post",
data: {id:xyz},
success: function (response) {
// you will get response from your php page (what you echo or print)
alert('Success') ;
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(textStatus, errorThrown);
}
});

inserting the php sendmail fuction into a confirm button within a pop up box

I am trying to use the sendmail function in php threw a pop up box. i have gotten as far as making the sendmail function and the pop up to work separately. but i haven't been able to figure out how to connect the sendmail in the right way. below is the last working git version i have. any help would be wonderful.
{
<tr>
<th align="center">Instrument Serial Number:</th>
<td align="left"><input type="text" class="cInstTravText" name="SerialNumber" id="idSerialNumber"/></td>
<button onclick="drop_check()">comfirm</button>
<p id="drop_check"></p>
<script>
function sendEmail()
{
$to = 'jonsrod1992#gmail.com';
$subject = 'Test email using PHP';
$message = "test";
//$message = 'hello world.\n\n this is a test for functionality\n\n this is the first try\n place instrument serial Number here--> '<$modelNumber>'<-- there.';
$headers = 'From: jonr#twobtech.com' . "\r\n" . 'Reply-To: jonr#twobtech.com' . phpversion();
mail($to, $subject, $message, $headers, '-fwebmaster#example.com');
}
function drop_check() {
var x;
if (confirm("transfer and send email!") == true) {
x = "transfered and email sent!";
} else {
x = "You pressed Cancel!";
}
document.getElementById("drop_check").innerHTML = x;
}
</script>
</tr>
The approach you're attempting isn't going to work. The php on your page runs first (server side), and then your pop-up is triggered using JavaScript. This means you'll need to send the response from the pop-up to another php page which then handles the sendmail. You can do this via POST in a form submission, or better yet, using an AJAX call, like so (this goes between script tags, preferably at the top or bottom of your page, not in the middle of your HTML):
if (confirm("transfer and send email!")) {
//if needed, get values from one or more HTML inputs like so:
var stuff = $('#someInputId').val();
$.ajax({
type: "POST",
url: "sendmail.php",
data: { data: stuff } //if you don't need to send any data from your HTML elements this can be ommitted
}).success(function() {
//optional: let the user know email was sent
});
}
You would then put your sendmail code in sendmail.php. If you're sending any data in the AJAX call, you can retrieve it from $_POST. sendmail.php would look something like:
$body = isset($_POST['stuff']) ? $_POST['stuff'] : 'no stuff';
$to = 'jonsrod1992#gmail.com';
$subject = 'Test email using PHP';
$headers = 'From: jonr#twobtech.com' . "\r\n" .
'Reply-To: jonr#twobtech.com' . "\r\n" .
"MIME-Version: 1.0\r\n" .
"Content-Type: text/html; charset=ISO-8859-1\r\n";
mail($to,$subject,$body,$headers);
Maybe I'm missing something, but... it looks like you may be trying to just throw PHP into your javascript. That is not going to work. Also, if this is just weird reformatting of the code, the sendEmail function is not being called.

Form submission opens php page

I have a form on html page. it has action as mailer.php. All code works properly, but on submission, page goes to mailer.php and browser shows an empty page on screen
contactus.html form tag:
<form id="form_20" action="mailer.php" method="post" target="_self"
enctype="multipart/form-data">
//input fields here
</form>
mailer.php file:
<?php
$to = '---------------'; //Removed for privacy
$subject = 'New Enquiry';
$name = $_POST['Name'];
$tel = $_POST['Number'];
$email = $_POST['Email'];
$comments = $_POST['Message'];
$body = "From: $name \nNumber: $tel \nEmail id: $email \nComments: $comments";
$headers = 'FROM: ---------------------------'.PHP_EOL; //Removed for privacy
mail($to, $subject, $body, $headers);
echo '<script>alert("Your data has been submitted.");</script>';
?>
The code works fine, I get the mail too.
The only problem I have is that the page redirects to mailer.php
P.S. : If anyone finds this a duplicate question, please comment the link to the question it duplicates so I get my answer. Thanks
to send the user back to the previous page:
header('Location: http://www.example.com/foo.php?back=yes'); //add your url
after the mail call, and remove the script
on foo.php
if ($_GET['back']=='yes'){
echo '<script>alert("Your data has been submitted.");</script>';
}

display success message with clear the post data and redirect on same page

I have one web page. In that I have contact form.
What I want that after submit the form it should redirect on same page and display success message(message should be fadeout after few seconds) and the post data of form should be clear. In form i am also cheking verification code and sending mail.
if(md5($_POST['vcode']).'a4xn' == $_COOKIE['tntcon'])
{
$msg="message";
$to = 'test#gmail.com';
$subject = 'subject';
$message = $msg;
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .="From: ".$_POST['email']." \r\n";
$mail=mail($to, $subject, $message, $headers);
if($mail==true)
{
echo '<script>$("#deletesuccess").fadeIn();$("#deletesuccess").delay(4000).fadeOut();window.location="index.php";</script>';
//here i want to redirect on same page with clear post data and display success message.
}
}
else
{
echo '<script type="text/javascript">document.getElementById("disp_wrong_code").style.display="inline";</script>';
// This is verification code error.
}
This is custom php page.
For redirecting you can use header function. For storing data between page loads use $_SESSION. Some simplified example:
session_start();
if ($can_do_post)
{
// do some actions here
$mail=mail($to, $subject, $message, $headers);
if ($mail==true) {
// set SESSION to know that it's success result
$_SESSION['form_filled'] = true;
// redirect to the same page
header("Location: /script.php");
die();
}
}
else
{
// no post data here
// check session:
if (isset($_SESSION['form_filled']) && $_SESSION['form_filled']) {
echo '<script>$("#deletesuccess").fadeIn();$("#deletesuccess").delay(4000).fadeOut();window.location="index.php";</script>';
}
}

What's wrong with my process.php that's not causing my form to send?

I have built a form which is submitted via Ajax and then processed with the mail() function in PHP. The form validation and form submission works perfectly (I can console log the data and receive a success message), yet I'm not receiving any emails from my form. I'm assuming it's the process.php.
This is my first time doing this, so also any pointers/code improvements would be much appreciated.
Javascript:
var name = $('#name').val();
var email = $('#email').val();
var message = $('#message').val();
var dataString = 'name=' + name + '&email=' + email + '&message=' + message;
$.ajax({
type: "POST",
url: "process.php",
data: dataString,
success: function () {
alert('success');
}
});
PHP:
<?php
$myemail = "name#example.com";
$subject = "Subject";
$message = "
Name: $name
E-mail: $email
Message: $message
";
mail($myemail, $subject, $message);
?>
I think indeed the problem resides within the php mail() method.
Just make sure it is, by commenting out the mail() call, and replace it with
echo 'I should be sending mail now!';
exit;
Then change your ajax call to:
$.ajax({
type: "POST",
url: "process.php",
data: dataString,
success: function (response) {
alert(response);
}
});
Now send the form to the server, and see what comes up in the alert box.
If you get the expected string, you should indeed setup a smtp server so you can send mail.
Another option would be to use an external mailserver, but it requires you to have a proper email account somewhere else.
There are a bunch of scripts out there, check out phpmailer for instance.
Thanks to dibs and dabs of help, I've managed to work it out.
The Ajax was all good, as expected.
The email wasn't sending because it was running locally, not using my servers mail PHP.
And the data from the form wasn't being referenced, thus returning blank emails.
SO, this is what ended up working:
<?php
$myemail = "name#example.com";
$subject = "Subject";
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$body = "
Name: $name
Email: $email
Message: $message
";
mail($myemail, $subject, $body);
?>

Categories