Form submission opens php page - javascript

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>';
}

Related

I have a HTML form that uses a POST request and PHP file to send me an email automatically, but it isn't working...any advice?

I have a form that uses an HTML form, http post request, and PHP backend to automatically send me the data that a user inputs into the form. The submission works as expected, but I am not getting an email. My email is also run by outlook. Not sure if it's an encryption thing.
HTML Code
<form action="mail.php" method="POST">
<div class="email-box">
<input class="tbox" id="email_box" name="email" type="email" style="cursor: pointer;" placeholder="Enter your email">
<button class="btn" id="email_btn" type="submit" name="button">Subscribe</button>
</div>
</form>
<!-- Snipped JavaScript validation -->
PHP
<?php
$errors = '';
$myemail = 'me#website.com';
if (empty($_POST['email'])) {
$errors .= "\n Error: all fields are required";
}
$email_address = $_POST['email'];
if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",
$email_address)) {
$errors .= "\n Error: Invalid email address";
}
if (empty($errors)) {
$to = $myemail;
$email_subject = "New Vrify subscription: $email_address";
$email_body = "You have a new subscriber. " .
"Here are the details:\n Email: $email_address \n " .
$headers = "From: subscriber#website.com";
mail($to, $email_subject, $email_body, $headers);
//redirect to the 'thank you' page
header('Location: thank-you.html');
}
?>
This may not be the full solution, but as far as I'm aware
"New Vrify subscription: $email_address"
is not valid. Instead, concatenate them using the . operator
"New Vrify subscription:".$email_address
Do the same to the other variables you have, I had the same issue when working on the following php:
if (($_SERVER["REQUEST_METHOD"] ?? 'GET') == "POST") {
$msg ="New message from mysite!\n".$_POST["message"]."\nReply Email:".$_POST["email"];
mail("me#gmail.com", time(), $msg,"From: contact#mysite");
}

PHP form do not show ÆØÅ

So I have made a contact form on my website. Everything works like a charm. However, the result beeing delivered to the email fail to display the ØÆÅ letters and instead only shows as weird symbols.. I have tried everything and feel really lost.
<?php
if (isset($_POST['submit']))
$sword = $_POST['swordy'];
$check = $_POST['check'];
$mailFrom = $_POST['mail'];
$message = $_POST['message'];
$ccname = $_POST['ccname'];
$droppy1 = $_POST['droppy1'];
$tel = $_POST['tel'];
$subject = 'Bryllupsinvitasjon ';
$mailTo = "your#email.com";
$headers = "From: ".$mailFrom;
$txt = " Du har motatt svar på bryllupsinvitasjon fra: ".$ccname."\n\n Svar: $check \n\n Telefonnummer: $tel\n\n Allergier: $droppy1\n\n Andre allergier/intolleranser/spørsmål/henvendelser: $message";
mail($mailTo, $subject, $txt, $headers);
header("Location: index.html?mailsendt");
?>
Try inserting the corresponding Unicode or HTML code for your specific character. I believe all three of your indicated chars are on this page: https://www.rapidtables.com/code/text/unicode-characters.html
Your email headers here aren't specifying UTF-8 encoding. You'll need to add that into the headers, specifically as part of the Content-Type header, like so:
Content-Type: text/html; charset="UTF-8"
With that header, the email should display the characters properly.

How to make PHP script redirect and display a popup box simultaneously when something is true/successful?

In my PHP code, I have tried to make it so that after the user successfully sends an email, it redirects back to the contact page and then displays a popup box informing the user that the email has been successfully sent: however, the program only ever runs the top line, and does not run the popup box code. How do I fix this?
<? php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: #';
$to = '#';
$subject = 'Customer Message - Online Form';
$body = "From: ".$name.
"\r\n E-Mail: ".$email.
"\r\n Message: \r\n".$message;
if (isset($_POST['submit'])) {
if (mail($to, $subject, $body, $from)) {
header('Location: http://www.google.com.au');
echo "<script>alert('Message successfully sent.');</script>";
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
}
?>
You are redirecting, by setting your header, before you print your alert. Try redirecting in the JS.
<? php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: #';
$to = '#';
$subject = 'Customer Message - Online Form';
$body = "From: ".$name.
"\r\n E-Mail: ".$email.
"\r\n Message: \r\n".$message;
if (isset($_POST['submit'])) {
if (mail($to, $subject, $body, $from)) {
echo "<script>alert('Message successfully sent.'); window.location='http://www.google.com.au'</script>";
//header('Location: http://www.google.com.au');
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
}
?>
Note
This will cause a JS alert before switching pages. If you want to alter on the page you land on you will want to send either a $_POST or $_GET variable and listen for it on the page you land on
You can't redirect the user away from the page and expect the page to still display something to the user.
If you want to display a message on a different page than store it in session and then, on the other page, check if something is stored in session and display that.
you are redirecting using your headers, if you try to echo first and then use header to redirect you will get an error, saying the headers are allready set.
You could add the redirect to your frontend, by returning something else like
if (isset($_POST['submit'])) {
if (mail($to, $subject, $body, $from)) {
echo '<script>
alert("Message successfully sent.");
location.href="http://www.google.com.au";
</script>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
}
the location redirect should wait for the alert to be dismissed, before executing

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>';
}
}

Cannot redirect after form submission

I have a form on a HTML page that has form data, and would like to have the user redirected to a new URL page after they have pressed the submit button and it has emailed the information. The form is called with the following button.
<div class="btnp"><input type="submit" value="Continue to Billing" ></div>
The form then sends the data to my PHP file via post. I do not need a success echo message, I would just like the URL to be redirected to the payment page. Below is the PHP file I have set up. I am trying to submit, send email with form date, and then redirect to a new html url.
<?php
//Retrieve form data.
//POST
$URL = "http://www.google.com";
$fname = $_POST['fname'];
$zip = $_POST['zip'];
$phone = $_POST['phone'];
$email = $_POST['email'];
//recipient - change this to your name and email
$to = 'sales#mysite.com';
//sender
$from = $fname . ' <' . $email . '>';
//subject and the html message
$subject = 'New User: ' . $fname;
$message = '
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head></head>
<body>
<table>
<tr><td>First Name</td><td>' . $fame . '</td></tr>
<tr><td>Zip</td><td>' . $zip . '</td></tr>
<tr><td>Telephone</td><td>' . $phone . '</td></tr>
<tr><td>Email</td><td>' . $email . '</td></tr>
</table>
</body>
</html>';
//send the mail
$result = sendmail($to, $subject, $message, $from);
if ($_POST)
if ($result) header('Location: '.$URL);
else echo 'Sorry, unexpected error. Please try again later';
//Simple mail function with HTML header
function sendmail($to, $subject, $message, $from) {
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= 'From: ' . $from . "\r\n";
$result = mail($to,$subject,$message,$headers);
}
?>
To use the "header" function:
header("location: result.php");
You can't output/print/echo anything to the screen, so try this:
if ($_POST)
if ($result) header("location: result-page.php");
else echo 'Sorry, unexpected error. Please try again later';
Or you can use javascript to print the success msg and redirect:
if ($_POST)
if ($result) {
print "<script>alert('Ok, email sent');</script>";
print "<script>window.open('result-page.php','_self');</script>";
}else{
print 'Sorry, unexpected error. Please try again later';
}
use the header('Location: $url); snippet that your tried but correct the function to this:
header('Location: '.$URL);
just make sure the varibale $URL is defined.

Categories