简体   繁体   中英

PHP Includes after submitting a form

I'm a bit of a noob when it comes to PHP and forms but I will do my best to explain.

What I am trying to do is create a series of forms that are included in a content container. The user fills out form#1, clicks 'submit' and moves on to fill in form#2 and so on. The data from each form is sent to the next form and is stored in hidden fields and once form#3 is completed the data is all inserted into a MYSQL database.

All of this works fine so far. The problem I am having is that I can't seem to get the submit buttons to work as intended. What I had envisioned is that the user would navigate to 'blah.com/recruit.php?p=form1' and fill out form#1 and then the submit button would take them to 'blah.com/recruit.php?p=form2' and so forth.

<form id="form" action="recruit.php?p=form2" method="post">

This does not work but I don't understand why. I've looked around the internet and I've found a few forum topics that discuss similar issues but none of them actually go into much detail about the solution or why this approach won't work.

Can anyone explain to me what it is I am doing wrong please? I have a feeling it is stupidly obvious but I can't put my finger on it.

Many thanks,

Splatgore

Some working sample code to do what I think you're trying to achieve:

<?php

$p = intval($_GET['p']);

if ($p == 0)
{
    $p = 1; // default to first form
}

if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
{
    // get / set all forms values here since subsequent forms will be 
    // posting the previous forms' data via hidden fields
    $first_name = $_POST['first_name'];
    $last_name  = $_POST['last_name'];
    $tel_number = $_POST['tel_number'];

    if ($p == 3)
    {
        // all forms done, insert to MySQL here
    }

    if ($p == 2)
    {
        // form 2's validation

        $p = 3; // set so that the form that follows will now show the 
                // third form
    }

    if ($p == 1)
    {
        // form 1's validation

        $p = 2; // set so that the form that follows will now show the 
                // second form
    }

}
?>
<form id="form" action="recruit.php?p=<?php echo $p; ?>" method="post">
    <h2>Form #<?php echo $p; ?></h2>
    <?php
    if ($p == 1)
    {
    ?>
    <!-- form 1's fields // -->
    <p><label for="first_name">First Name:</label>
    <input type="text" name="first_name" id="first_name" size="40" value="<?php echo $first_name; ?>" /></p>

    <p><label for="last_name">Last Name:</label>
    <input type="text" name="last_name" id="last_name" size="40" value="<?php echo $last_name; ?>" /></p>
    <?php
    }

    if ($p == 2)
    {
    ?>
    <!-- form 2's fields // -->
    <p><label for="tel_number">Tel Number:</label>
    <input type="text" name="tel_number" id="tel_number" size="25" value="<?php echo $tel_number; ?>" /></p>

    <p><input type="hidden" name="first_name" value="<?php echo $first_name; ?>" />
    <input type="hidden" name="last_name" value="<?php echo $last_name; ?>" /></p>
    <?php
    }

    if ($p == 3)
    {
    ?>
    <!-- form 3's fields // -->
    <div>
        <input type="hidden" name="first_name" value="<?php echo $first_name; ?>" />
        <input type="hidden" name="last_name" value="<?php echo $last_name; ?>" />
        <input type="hidden" name="tel_number" value="<?php echo $tel_number; ?>" />
    </div>
    <?php
    }
    ?>

    <p><input type="submit" value="Submit"></p>
</form>

It looks somehow you have nested forms. This is not possible in HTML.

If you are going with your approach, just use always the same submit button which leads to the same php script. In the php script check which hidden fields are already set to see how far the process is.

A better approach IMO would be tough to store the data in a session var and always lead to another form page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM