PHP - 表单提交时页面重新加载
我一整天都被这个问题困扰了。非常感谢您的帮助:
我有一个“编辑”页面。
我已在顶部声明了我的变量,以便在有人编辑表单时制作“粘性”单选按钮。
我已成功提交,但是如果我使用表单操作返回“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您在同一页面上执行“编辑内容”和“更新表格”并在不同页面上显示结果,您将丢失所有已发布的数据。要将结果显示到另一个页面,您可以执行以下任一方法:
或者
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:
or