I have three different forms on a single page. Each form's data will be entered into a different table. For the first one I created I'm using this if
statement to insert the data into the table:
if ( $_SERVER['REQUEST_METHOD'] === 'POST' ) {
$band_name = $_POST['bandname'];
$band_website = $_POST['website'];
$band_descrip = $_POST['description'];
query(
"INSERT INTO bands(bandname, website, description) VALUES(:bandname, :website, :description)",
array('bandname' => $band_name, 'website' => $band_website, 'description' => $band_descrip),
$conn
);
} else {
$status = "Fill out the form";
}
I am assuming I need to create three different versions of the if
statement, one for each form. How can I specify which form was submitted?
Since when you submit a form, only the data in the form tags are submitted you are able to check what form by using something to identity the forms. I usually use the submit buttons name to check for the form. You can use any $_POST
variable as long as you only use it for one form.
<form method="post">
<input type="text" name="form1_text" />
<input type="submit" name="form1_submit" value="Submit" />
</form>
<form method="post">
<input type="text" name="form2_text" />
<input type="submit" name="form2_submit" value="Submit" />
</form>
<?php
if(isset($_POST['form2_submit'])) {
// Handle Form 2
}else if(isset($_POST['form1_submit'])) {
// Handle Form 1
}
?>
For more information on how web browsers should handle forms, you can check the HTML documentation on W3.org - Forms in HTML documents. This explains what data is sent to the server and how that data is selected / determined.