Navigate to specific url on php form submission
Use php header('Location: somepage.php');
if(mail($sendTo, $subject, $emailText, "From: " . $from))
{
header('Location: somepage.php');
}
Note: The php mail() function will return true if the message is accepted for delivery but doesn't guarantee that it will be delivered.
Edit: have a look at my simple email function with try catch statement. Compare with your answer maybe it will help you.
// configure
$from = 'Demo contact form <demo@domain.com>';
$sendTo = 'Demo contact form <demo@domain.com>';
$subject = 'New message from contact form';
$fields = array('name' => 'Name', 'email' => 'Email'); // array variable name => Text to appear in email
$okMessage = 'Contact form successfully submitted. Thank you, I will get back to you soon!';
$errorMessage = 'There was an error while submitting the form. Please try again later';
//post varables from your form
//$name = $_POST['name'];
$name = 'John';
//$email = $_POST['email'];
$email = 'johndoe@blah.com';
//Email header
$header = "MIME-Version: 1.0" . "\r\n";
$header .= "Content-type: text/html;charset=iso-8859-1" . "\r\n";
$header .= "From: $from" . "\r\n";
$header .= "Reply-To: ..." . "\r\n";
$emailText = "You have new message from contact form <br/> ============================ <br/> Name : $name <br/> Email : $email <br/>";
try
{
if (mail('demo@domain.com', $subject, $emailText, $header))
{
header('Location: somepage.php');
}
else
{
throw new Exception($errorMessage);
}
}
catch (Exception $ex)
{
echo 'Message: ' . $ex->getMessage();
}
How to go back to page after submitting form using PHP
Try this
header("Location: simple-form.php");
You have to add this in connect.php
, because your submit-button in the form will direct you to connect.php
.
Your connect.php should look like this:3
<?php
$name = $_POST["name"];
$lnk = $_POST["img"];
$dsc = $_POST["dsc"];
$servername = 'localhost';
$username = 'root';
$password = '';
$database = 'cero_db';
$conn = new mysqli($servername, $username, $password,$database);
$sql = "INSERT INTO pg (Name, Link, Description)
VALUES ('".$name."', '".$lnk."', '".$dsc."')";
$conn->query($sql);
header("Location: simple-form.php");
?>
navigating back to to first form without losing values
Sessions would be your best option IMO. In each formX.php file, be sure to start the session:
$session_start();
And then at the beginning of each script, check for $_POST variables. If you have any (i.e submitted from previous form) add them to the $_SESSION array.
$_SESSION['lastname'] = $_POST['lastname']);
In addition, when rendering the form on the page, use whatever value is in the $_SESSION variable for that text field. If it is empty or not set, then the user will be presented with an empty textbox.
[SECURITY DISCLAIMER] I should warn you that haplessly taking user input and throwing around in your code is a recipe for security disasters. Make sure you filter input and escape output when doing all of this!
Back button after form submit?
The most secure would be php,
<a href="<?php echo $_SERVER['HTTP_REFERER'];?>">back</a>
JavaScript can be disabled a times :)
Php Go back to previous page form resubmit
Desired result was achieved by using the GET method instead of POST method (see https://www.tutorialspoint.com/php/php_get_post.htm). reports.php form code was replaced from:
<form method="post" action="bwdates-reports-details.php">
to:
<form method="get" action="bwdates-reports-details.php">
In bwdates-reports-details.php where I have the following variables:
$fdate=$_POST['fromdate'];
$tdate=$_POST['todate'];
Instead I replaced it with:
$fdate=$_GET['fromdate'];
$tdate=$_GET['todate'];
The goback javascript function I have in visitor-detail.php stays the same. And works like a charm. Thanks and cheers!
Related Topics
How to Call a Static Method from a Class If All I Have Is a String of the Class Name
Laravel/Intervention Image Class - Class Not Found
Laravel Eloquent Compare Date from Datetime Field
PHP Jquery Ajax Call Throws Net::Err_Empty_Response
Post Data and Retrieve the Response Using PHP Curl
Declaring a Global Variable Inside a Function
How to Get User Image with Twitter API 1.1
Compress/Archive Folder Using PHP Script
How to Use SQL Server Connection in Laravel
PHP - Create Simple Animated Gif from Two Jpeg Images
Rendering an Svg File to a Png or Jpeg in PHP
Symfony 2 Load Different Template Depending on User Agent Properties
How to Upload Multiple Image in Laravel
Differencebetween Get_Result() and Store_Result() in PHP