I know this has been asked and answered before, but I would like to know which of the following is the best way to implement a redirect on form submission.
I have the PHP submission code in a the header section of a webpage. This works fine, but when the form is submitted, the page returns to the same page. If I put a
below the submission code the page immediately redirect to this page (as expected - the code is run as soon as the page loads so it wont even create the form, it will just move on).
Now, do I
1/ Put the code for data submission in a PHP function (together with the redirect code) and then call this function when the form is submitted. This way the function wont run until after the data has been inputted and the same page doesn't have to be reloaded only to redirect to another page almost immediately.
2/ Instead of the function above, use an
if statement in the header to test one of the pieces of data. That way the submission code is ignored until the form has been filled out and the submit button is pressed. The downside as far as I can see is the same page has to be called to get back to running the code in the header, which can then submit the data and run the redirect code.
3/ Something completely different!
This is the code in the header of form.php:
$user = $_POST['email'];
$_SESSION['email'] = $user;
$title = $_POST['title'];
$first_name = $_POST['first_name'];
$surname = $_POST['surname'];
$phone_number = $_POST['phone'];
$email_address = $_POST['email'];
$house = $_POST['add1'];
$street = $_POST['add2'];
$town = $_POST['add3'];
$city = $_POST['add4'];
$postcode = $_POST['add5'];
$query = "INSERT INTO `first_page_data`(`title`, `first_name`, `surname`, `phone_number`, `email_address`, `house`, `street`, `town`, `city`, `postcode`) VALUES ('$title','$first_name','$surname','$phone_number','$email_address','$house','$street','$town','$city','$postcode')";
$result = mysql_query($query, $first_data);
Can this be run on the page that is redirected to, or does it have to be run on the page with the form?
You can do this by using
action attribute, look at the code below...
<form action="newproperty2.php" method="post"> Name: <input type="text" name="first_name"><br/> SURNAME: <input type="text" name="surname"><br/> Phone: <input type="text" name="phone"><br/> E-mail: <input type="text" name="email"><br/> title: <input type="text" name="title"><br/> <input type="submit"> </form>
Here the page is directed to welcome.php after submit button is pressed, moreover you can also access