I have a form that takes input from the user and stores it in a MySQL database when the user submits the form.
print_r('<form action="" method="post">');
/*
form data, input tags, etc
*/
if (isset($_POST['Submit']))
{
$sql = "INSERT INTO table
VALUES (...)";
$insert_form = $conn->query($sql);
}
print_r('</form>');
Initially, the user stayed on the page when they submitted the form and everything worked fine. However, I updated the site to take the user to a new page after the form is submitted. The first line of the above code now looks like this:
print_r('<form action="next-page.php" method="post">');
I didn't think it was a significant change, but now whenever I try to Submit the form, I am taken to the next page without an insert into the database. When I remove the form action, the insert command works, and when I put it back in, it does not.
Any idea why this problem is occurring?
I have found a workaround using the php header function:
if ($insert_form)
header('Location: next-page.php');
..but it feels a little sloppy. Any comments on this?
actionattribute makes you go to its value, +ignoring all the line below it.