PHP 提交表单后包含

发布于 2024-11-05 22:27:45 字数 548 浏览 0 评论 0原文

当谈到 PHP 和表单时,我有点菜鸟,但我会尽力解释。

我想做的是创建一系列包含在内容容器中的表单。用户填写表单#1,单击“提交”,然后继续填写表单#2,依此类推。每个表单中的数据都会发送到下一个表单并存储在隐藏字段中,一旦表单#3 完成,数据就会全部插入到 MYSQL 数据库中。

到目前为止,所有这些都运行良好。我遇到的问题是我似乎无法让提交按钮按预期工作。我的设想是,用户将导航到“blah.com/recruit.php?p=form1”并填写表单#1,然后提交按钮会将他们带到“blah.com/recruit.php?p=” form2' 等等。

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

这不起作用,但我不明白为什么。我浏览了互联网,发现了一些讨论类似问题的论坛主题,但没有一个主题真正详细介绍了解决方案或为什么这种方法行不通。

谁能向我解释一下我做错了什么?我有一种感觉,这是愚蠢的明显,但我无法指出它。

非常感谢,

斯普拉戈尔

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青朷 2024-11-12 22:27:45

一些工作示例代码可以完成我认为您想要实现的目标:

<?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>

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>
悸初 2024-11-12 22:27:45

看起来你有嵌套的表单。
这在 HTML 中是不可能的。

如果您采用自己的方法,只需始终使用相同的提交按钮即可生成相同的 php 脚本。
在 php 脚本中检查已设置哪些隐藏字段以查看进程进行到什么程度。

在我看来,更好的方法很难将数据存储在会话变量中,并且总是导致另一个表单页面。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文