Mosh
Mosh

Reputation: 471

PHP two forms, one page

So in school we're learning about OOP in PHP and for our assignment we need to use 2 forms. this is the first time I'm using 2 forms in one page and I've been trying to figure out how to check which form is being submitted and create the according object. Apparently, afer looking at some other questions, just using if (!empty($_POST['SubmitButtonName'])) should work, but it doesnt. Hope someone can help me and tell me what I'm doing wrong :)

PHP:

if (!empty($_POST['sportwgn'])) 
{
    try 
    {
       $sport->Merk = $_POST['merk'];
       $sport->AantalPassagiers = $_POST['AantalPassagiers'];
       $sport->AantalDeuren = $_POST['AantalDeuren'];
       $sport->Stereo = isset($_POST['stereo']) ? true : false;
       $sport->Save();
       $succes= "Uw sportwagen is gereserveerd!";
    }
    catch( Exception $e)
    {
        $error = $e->getMessage();
    }
}
if (!empty($_POST['vrachtwgn'])) 
{
    try 
    {
       $vracht->Merk = $_POST['merk'];
       $vracht->AantalPassagiers = $_POST['AantalPassagiers'];
       $vracht->AantalDeuren = $_POST['AantalDeuren'];
       $vracht->MaxLast = $_POST['MaxLast'];
       $vracht->Save();
       $succes= "Uw vrachtwagen is gereserveerd!";
    }
    catch( Exception $e)
    {
        $error = $e->getMessage();
    }
}

Forms:

<form action="" method="post">
        <label for="merk">merk</label>
        <input type="text" id="merk" name="merk">
        <br>
        <label for="AantalPassagiers">Aantal passagiers</label>
        <input type="number" min="2" max="4" id="AantalPassagiers" name="AantalPassagiers">
        <br>
        <label for="AantalDeuren">Aantal deuren</label>
        <input type="number" min="1" max="5" id="AantalDeuren" name="AantalDeuren">
        <br>
        <label for="stereo">Stereo?</label>
        <input type="checkbox" name="stereo" id="stereo" value="stereo"><br>
        <br></div><div class="box">
        <button type="submit" name="sportwgn">Reserveer</button></div>
    </form>

</div>
</div>
<div id="container">
<h1 class="box">Reserveer een Vrachtwagen!</h1>
<div id="content">

    <form action="" method="post">
        <label for="merk">merk</label>
        <input type="text" id="merk" name="merk">
        <br>
        <label for="AantalPassagiers">Aantal passagiers</label>
        <input type="number" min="2" max="4" id="AantalPassagiers" name="AantalPassagiers">
        <br>
        <label for="AantalDeuren">Aantal deuren</label>
        <input type="number" min="1" max="5" id="AantalDeuren" name="AantalDeuren">
        <br>
        <label for="MaxLast">Max last</label>
        <input type="number" min="1" max="5" id="MaxLast" name="MaxLast"><br>
        <br></div><div class="box">
        <button type="submit" name="vrachtwgn">Reserveer</button></div>
    </form>

Upvotes: 1

Views: 78

Answers (3)

Maxim Lazarev
Maxim Lazarev

Reputation: 1254

Use action attribute to submit forms to different destinations.

<form action="firstForm.php" method="post">
...
</form>

<form action="secondForm.php" method="post">
...
</form>

And create 2 files for handling form posting.

Upvotes: 0

user2560539
user2560539

Reputation:

Since your forms post on the same page (...action=""...) divide your code in php side to two actions by the submit button. for the forms with

<button type="submit" name="sportwgn">Reserveer</button></div>

use

if(isset($_POST['sportwgn'])) {
// your code
}

and for

<button type="submit" name="vrachtwgn">Reserveer</button></div>

use

if(isset($_POST['vrachtwgn'])) {
// your code
}

Upvotes: 2

Refilon
Refilon

Reputation: 3499

You can use if(isset($_POST['buttonName'])) to check if it's in the post values.

Upvotes: 0

Related Questions