Alisinna Alisinna - 1 year ago 58
HTML Question

Multiple actions on form using PHP and HTML

I am creating a website, and part of the website should be a Bookings section. I have created a form, and the data entered into this form is to be stored in a database. Up to this point everything works fine. However, I also need to validate the data input by the user. Such as checking that characters only are entered in the Name text field, etc..I have also managed to do this correctly.

The problem is, that the form is not allowing me to do both functions, as there can only be one "action" in the form tag. The form is allowing me to store data in the database whenever I write action="Database.php" and allowing me to validate the user input whenever I write action= . I need a function which can combine both as I need to do both for my website. Do you have any suggestions please?

This is the code:

<p><span class="error"></span></p>
<form class="MyForm" method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" >

* required field
Name and Surname: <br><input type="text" name="name">
<span class="error">* <?php echo $nameErr;?></span>

E-mail: <br><input type="text" name="email">
<span class="error"> <?php echo $emailErr;?></span>
Contact Number: <br><input type="text" name="contactnumber">
<span class="error">* <?php echo $contactnumberErr;?></span>

Number of people: <br><input type="text" name="numberofpeople">
<span class="error">* <?php echo $numberofpeopleErr;?></span>
Date: <br><input type="date" name="date">
<span class="error">* <?php echo $dateErr;?></span>
Time of Booking: <br><input type="time" name="timeofbooking">
<span class="error">* <?php echo $timeofbookingErr;?></span>
Comments: <br><textarea name="comment" rows="5" cols="40">Comments...</textarea>
<button class="button button1">Submit</button>

And this is the code for the validation in PHP:

// define variables and set to empty values
$nameErr = $emailErr = $contactnumberErr = $numberofpeopleErr = $dateErr = $timeofbookingErr = "";
$name = $email = $contactnumber = $numberofpeople = $date = $timeofbooking = $comment = "";

if (empty($_POST["name"])) {
$nameErr = "Name and Surname are required";
} else {
$name = test_input($_POST["name"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$nameErr = "Only letters and white space allowed";
if (!empty($_POST["email"])) {
$email = test_input($_POST["email"]);
// check if e-mail address is well-formed
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
if (empty($_POST["contactnumber"])) {
$contactnumberErr = "Contact number is required";
} else {
$contactnumber = test_input($_POST["contactnumber"]);

if (!preg_match("/^[0-9 ]*$/",$contactnumber)) {
$contactnumberErr = "Only numbers are allowed";
if (empty($_POST["numberofpeople"])) {
$numberofpeopleErr = "Number of people is required";
} else {
$numberofpeople = test_input($_POST["numberofpeople"]);

if (!preg_match("/^[0-9 ]*$/",$name)) {
$numberofpeopleErr = "Only 2 digit numbers are allowed";
if (empty($_POST["date"])) {
$dateErr = "Date is required";
} else {
$date = test_input($_POST["date"]);
if (empty($_POST["time"])) {
$timeErr = "Time is required";
} else {
$time = test_input($_POST["time"]);
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;

Answer Source

Explaining a few important fundamentals:

The action="database.php" or action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" just refers to where you want the form information to be sent, and does not limit you to how much you can do as you are suggesting with your question. I'll explain this in a simple manner:

If you write the name of a file in your action attribute like so:

<form action="database.php" method="POST">
//... more code

That just means that you are going to send the values that were inside the form to the page database.php, and at the same time, you will be redirected to that page.

If you use the following however:

<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST">
//... more code

that just means that you want to send the form information to the same page and you want to redirect the user to the very same page.

You might ask yourself "but why does code exist that redirects you to the same page?". Well that has to do with the passing of information. If you load a page that has a form in it, after the page has loaded, you no longer have a connection with the website (there are exceptions like when using AJAX code). Since you no longer have a connection, your browser will have to create another connection when you hit the "submit" button on the form. When that connection is made, the form tells the browser: "send this information to this page and redirect me there to". That way, your message is sent to the server, the server can see the input from the form, and process it.

So by redirecting the user to the same page, you are giving the browser a chance to reconnect with the server of the website so that It can pass the information you placed in the form.


So finally... back to the meat of the question. All you have to do is include code that processes both the database information and that does the validation that you want on the same page (regardless if it is in database.php or to the very same page.

You are not being limited to just doing 1 process because your action attribute is different. You just have to make sure that all the processes you want are coded into the page where the form is sending the data.