PHP - 表单提交时页面重新加载

发布于 2024-11-18 11:05:09 字数 2279 浏览 4 评论 0原文

我一整天都被这个问题困扰了。非常感谢您的帮助:

我有一个“编辑”页面。

我已在顶部声明了我的变量,以便在有人编辑表单时制作“粘性”单选按钮。

我已成功提交,但是如果我使用表单操作返回“confirm.php”,则刚刚提交的内容不会显示在页面上。

如果我使用 PHP_SELF,表单中的内容将恢复到编辑之前的状态,但表格已更新。

<form method="post" action="<?php $_SERVER['PHP_SELF'] ?>">

    <!-- ---------------------- -->
    <!-- RSVP SELECT -->
    RSVP:<br />
    <input type="radio" name="guestAccept" id="accept" value="yes"
    <?php 
        if($rows['guestAccept'] == 'yes'){
            echo 'checked="checked" ';
        }?>
    >Accepts with pleasure<br />

    <input type="radio" name="guestAccept" id="decline" value="no"
    <?php
        if ($rows['guestAccept'] == 'no'){
            echo 'checked="checked" ';
        }?>
    >Declines with regret<br />

    <br />

    <!-- ---------------------- -->
    <!-- MEAL SELECT -->
    <div id="mealSelect">
        Meal:<br />
        <input type="radio" name="guestMeal" value="fish"
        <?php
            if ($rows['guestMeal'] == 'fish'){
                echo 'checked="checked" ';
            }?>
        >Grilled Wild Pacific Salmon<br />
        <input type="radio" name="guestMeal" value="beef"
        <?php
            if ($rows['guestMeal'] == 'beef'){
                echo 'checked="checked" ';
            }?>
        >Roasted Beef Tenderloin<br />
        <input type="radio" name="guestMeal" value="veg"
        <?php
            if ($rows['guestMeal'] == 'veg'){
                echo 'checked="checked" ';
            }?>
        >Vegetarian Risotto<br />
        <br />
    </div>

    <!-- ---------------------- -->
    <!-- SUBMIT -->
    <input type="submit" name="submit" id="btnSubmit" value="Submit" />
</form>

<a href="confirm.php">Go Back</a>

<?php
    // Close WHILE LOOP
    }

    // checks to see if submit button was pressed
    if(isset($_POST['submit'])){

        $query = "UPDATE guest SET guestMeal = '$guestMeal', guestAccept = '$guestAccept' WHERE id = $id";
        mysql_query($query);


    }
?>

我需要重定向到确认页面,或者显示包含更新内容的表单。 我被困住了,我怎样才能做到这一点?

谢谢,

I've been stuck on this problem all day. Help would be greatly appreciated:

I have an 'edit' page.

I have declared my variables up top to make "sticky" radio buttons when somebody goes to edit the form.

I have it submitting successfully, however if I go back to "confirm.php" with the form action, the content that was just submitted doe not display on the page.

If I use PHP_SELF, the content in to form reverts back to what it was before it was editied, however the table was updated.

<form method="post" action="<?php $_SERVER['PHP_SELF'] ?>">

    <!-- ---------------------- -->
    <!-- RSVP SELECT -->
    RSVP:<br />
    <input type="radio" name="guestAccept" id="accept" value="yes"
    <?php 
        if($rows['guestAccept'] == 'yes'){
            echo 'checked="checked" ';
        }?>
    >Accepts with pleasure<br />

    <input type="radio" name="guestAccept" id="decline" value="no"
    <?php
        if ($rows['guestAccept'] == 'no'){
            echo 'checked="checked" ';
        }?>
    >Declines with regret<br />

    <br />

    <!-- ---------------------- -->
    <!-- MEAL SELECT -->
    <div id="mealSelect">
        Meal:<br />
        <input type="radio" name="guestMeal" value="fish"
        <?php
            if ($rows['guestMeal'] == 'fish'){
                echo 'checked="checked" ';
            }?>
        >Grilled Wild Pacific Salmon<br />
        <input type="radio" name="guestMeal" value="beef"
        <?php
            if ($rows['guestMeal'] == 'beef'){
                echo 'checked="checked" ';
            }?>
        >Roasted Beef Tenderloin<br />
        <input type="radio" name="guestMeal" value="veg"
        <?php
            if ($rows['guestMeal'] == 'veg'){
                echo 'checked="checked" ';
            }?>
        >Vegetarian Risotto<br />
        <br />
    </div>

    <!-- ---------------------- -->
    <!-- SUBMIT -->
    <input type="submit" name="submit" id="btnSubmit" value="Submit" />
</form>

<a href="confirm.php">Go Back</a>

<?php
    // Close WHILE LOOP
    }

    // checks to see if submit button was pressed
    if(isset($_POST['submit'])){

        $query = "UPDATE guest SET guestMeal = '$guestMeal', guestAccept = '$guestAccept' WHERE id = $id";
        mysql_query($query);


    }
?>

I need to either redirect to a confirmation page, OR show the form with the update content.
I'm stuck, how can I make this work?

Thanks,

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

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

发布评论

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

评论(1

一身软味 2024-11-25 11:05:09

如果您在同一页面上执行“编辑内容”和“更新表格”并在不同页面上显示结果,您将丢失所有已发布的数据。要将结果显示到另一个页面,您可以执行以下任一方法:

  1. 将发布的数据存储到会话变量,以便您可以将它们检索到另一个页面。

或者

  1. 将刚刚更新的记录的 ID 存储到会话变量中,然后存储到另一个页面,使用该会话变量从数据库中检索数据,然后显示它们。

If you do the 'edit content' and 'update table' on the same page and displays the result on a different page, you'll lose all of the posted data. To display the results to another page, you can do either way:

  1. Store posted data to session variables so that you can retrieve them to another page.

or

  1. Store the ID of the record you have just updated to a session variable, then to another page, use that session variable to retrieve data from the database, then display them.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文