Xavvey
Xavvey

Reputation: 43

HTML and PHP - How to seperate 2 forms on a single page?

I'm pretty new to HTML and currently I'm making an assignment where I have to make a HTML Form for data input and write the input to a file using PHP.

The writing to file works, but I getting the following comment on my code:

You have tested whether something has been posted, but what happens to your code if there are 2 forms on your page?

I kinda know what is being meant with this, but I am not that fluent to know how to solve this issue or where this comes from in my code.. Does this has to do with the action of the form set to $_SERVER["PHP_SELF"], the name of the submit button set wrong or anything else?

I've tried looking online for HTML forms and how to have 2 on the same page.. but I could not find anything really helpfull. If anyone can help or maybe point me to some info that explains this in detail (and with examples preferably) that would be great!

Here is just my HTML form as I have it with the PHP part checking for the submit. Rest of the code I left out as it is not relevant..

<html>
    <head>
        <title>Save and show data</title>
    </head>
    <body>
        <h2>Fill in the form below</h2>
        <form method="post" action="<?php $_SERVER["PHP_SELF"]; ?>">
            <label for='name'>Naam:</label><br>
            <input type="text" id="name" name="name" placeholder = "John Doe" required><br>
            <label for='address'>Adres:</label><br>
            <input type="text" id="address" name="address" placeholder = "Sunset lane 10" required><br>
            <label for='zip'>Postcode:</label><br>
            <input type="text" id="zip" name="zip" placeholder = "15922" required><br>
            <label for='residence'>Woonplaats:</label><br>
            <input type="text" id="woonplaats" name="woonplaats" placeholder = "Somewhere" required><br>
            <label for='phone'>Telefoonnummer:</label><br>
            <input type="tel" id="phone" name="phone" placeholder = "0678945124" required><br>
            <label for='email'>E-mail:</label><br>
            <input type="email" id="email" name="email" placeholder = "[email protected]" required><br><br>
            <input type="submit" name="submit_data" value="Save"><br>
        </form>
    <?php 
    if(isset($_POST["submit_data"]))
    {   
        // magic stuff happens here   
    }

Upvotes: 1

Views: 387

Answers (1)

v1n_vampire
v1n_vampire

Reputation: 1595

Do you mean something like this? Each submit button has its own validation.

<?php
    if (isset($_POST['submit_data'])) {
        //first form
    }
    
    if (isset($_POST['edit_data'])) {
        //second form
    }
?>


<form method="post" action="<?php $_SERVER["PHP_SELF"]; ?>">
    <label for='name'>Naam:</label><br>
    <input type="text" id="name" name="name" placeholder = "John Doe" required><br>
    <input type="submit" name="submit_data" value="Save"><br>
</form>

<form method="post" action="<?php $_SERVER["PHP_SELF"]; ?>">
    <label for='name'>Naam:</label><br>
    <input type="text" id="name" name="name" placeholder = "John Doe" required><br>
    <input type="submit" name="edit_data" value="Edit"><br>
</form>

Upvotes: 3

Related Questions