Navigate Back with PHP Form Submission

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



Leave a reply



Submit