Php 提交表单,进行页面处理,然后再次提交
我有两个PHP页面,A和B 页面A有一个表单,它将值为“edit”的$u POST['mode']变量传递给页面B 页面B也有它的表单,它的提交按钮被命名为“_go”。第B页检查是否设置了$_POST[''u go']参数,并在同一页B内处理表单 问题是,在B页上,如果我在表单上输入一些值并在第一次提交表单,它就会工作,并且我会收到成功消息。但是,在我收到成功消息后,在没有刷新页面的情况下,重新输入另一个值并重新提交表单,我什么也没有得到,重新输入的值也没有得到处理 我自己对此进行了调查,发现$u POST[''u mode']是问题的原因。当我第一次提交表单时,$\u POST[''u mode']是'edit'。但是,当我重新提交表单时,$\u POST[''u mode']仍处于设置状态,但其值为空 我在这里很沮丧Php 提交表单,进行页面处理,然后再次提交,php,forms,post,submit,Php,Forms,Post,Submit,我有两个PHP页面,A和B 页面A有一个表单,它将值为“edit”的$u POST['mode']变量传递给页面B 页面B也有它的表单,它的提交按钮被命名为“_go”。第B页检查是否设置了$_POST[''u go']参数,并在同一页B内处理表单 问题是,在B页上,如果我在表单上输入一些值并在第一次提交表单,它就会工作,并且我会收到成功消息。但是,在我收到成功消息后,在没有刷新页面的情况下,重新输入另一个值并重新提交表单,我什么也没有得到,重新输入的值也没有得到处理 我自己对此进行了调查,发现$
我已经修改了您的代码以使用会话,它将解决变量丢失的问题,您甚至可以在其他页面中重用它。当我在a页中创建会话,然后重定向到b页时,我已将a页表单的操作设置为“”。请注意,“会话_start();”必须位于每页的顶部,上面只能有代码 A页
<?php
session_start();
if( isset( $_POST['mode'] ) ) {
$mode = $_POST['mode'];
//setting a session
$_session['mode']=$mode;
//put your own file name for b in next line
header("location:pageb.php");
}
?
<form action="" id="some id" name="some name" method="post">
<input type="hidden" name="mode" value="edit" />
<input type="submit" class="btn" value="Edit" />
</form>
我已修改您的代码以使用会话,它将解决变量丢失的问题,您甚至可以在其他页面中重用它。我已将a页表单的操作设置为“”,因为我正在a页创建会话,然后重定向到b页。请注意“会话_start()”;“必须位于每页的顶部,上面只能有代码”
A页
<?php
session_start();
if( isset( $_POST['mode'] ) ) {
$mode = $_POST['mode'];
//setting a session
$_session['mode']=$mode;
//put your own file name for b in next line
header("location:pageb.php");
}
?
<form action="" id="some id" name="some name" method="post">
<input type="hidden" name="mode" value="edit" />
<input type="submit" class="btn" value="Edit" />
</form>
在重新加载页面后,变量和post的get reset将为空。我需要进一步澄清。即使我使用if(isset($\u POST[])在同一页面内处理表单,它也会被视为页面重新加载,变量也会消失?我为您发布了一个答案,以澄清当您第一次加载页面时,它会将$\u POST['mode']加载到页面,您可以获取变量并设置它。重新加载页面时,由于没有向页面发送post数据,因此不再有$\u post['mode']。我认为最好的方法是使用会话,这就是为什么我将您的代码更改为使用会话的原因。感谢Gert,您花时间为我提供了宝贵的建议。不管怎样,我忘了提到我在Wordpress上,启动手动会话对我来说很可疑,tho.我可以发布一个我为单选按钮的答案编写的代码,没有会话,但它只是一页发布到同一页,如果你愿意,我可以将该代码添加到我的答案中,可能会有帮助。它将清空变量,并在页面重新加载后重置。我需要更多的澄清。即使我使用if(isset($\u POST[])在同一页面内处理表单,它也会被视为页面重新加载,变量也会消失?我为您发布了一个答案,以澄清当您第一次加载页面时,它会将$\u POST['mode']加载到页面,您可以获取变量并设置它。重新加载页面时,由于没有向页面发送post数据,因此不再有$\u post['mode']。我认为最好的方法是使用会话,这就是为什么我将您的代码更改为使用会话的原因。感谢Gert,您花时间为我提供了宝贵的建议。无论如何,我忘了提到我在Wordpress上,启动手动会话对我来说很可疑。我可以发布一个代码,我为单选按钮的答案编写了一个没有会话的代码,但是如果你想,只需在同一页上发布一页。我可以将该代码添加到我的答案中,也许这是有帮助的
Before:
if( $_go_mode == 'edit' ) {
process_form();
}
After:
if( $_go_mode == 'edit' ) {
$_POST['mode'] = $_go_mode;
process_form();
}
<?php
session_start();
if( isset( $_POST['mode'] ) ) {
$mode = $_POST['mode'];
//setting a session
$_session['mode']=$mode;
//put your own file name for b in next line
header("location:pageb.php");
}
?
<form action="" id="some id" name="some name" method="post">
<input type="hidden" name="mode" value="edit" />
<input type="submit" class="btn" value="Edit" />
</form>
<?php
session_start();
//checking for the session and assigning variable name
if (isset($_session['mode'])) {
$mode = $_session['mode'];
}
// if form is submitted and mode is 'edit', go process the form
if( isset( $_POST['_go'] && isset( $_POST['_mode'] ) ) {
$_go_mode = $_POST['_mode'];
if( $_go_mode == 'edit' ) {
process_form();
}
}
function process_form() {
// This function receives parameters from the form
// and does what it needs to do
echo '<div class="process-result">';
echo 'Successfully made changes';
echo '</div>';
}
?>
<div>
<form action "" method="post" class="form-horizontal">
<div class="form-group">
<label class="control-label">Some label</label>
<input type="text" class="form-control" name="_name" value="some value" />
</div>
<div class="form-group">
<input type="hidden" name="_mode" value="<?php echo $mode; ?>" />
<input type="submit" name="_go" class="btn" value="Make Changes" />
</div>
</form>
</div>
<html>
<head>
</head>
<body>
<form name="anyname" action="" method="post">
<input type="radio" name="choice" value="asia"/>asia
<input type="radio" name="choice" value="africa"/>africa
<input type="submit" name="submit" value="my radio choice">
</form>
<?php
function choose(){
//declare it is a global variable from outside the function
global $choice;
if ($choice =='africa') {
$test='you choose africa';
}
elseif ($choice =='asia'){
$test='you choose asia';
}
//returning the output of the function
return $test;
}
//here you proccess the data after the page reloads
if (!empty($_POST['choice'])){
$choice=$_POST["choice"];
//running the function
choose();
//getting the output from the function
$testafter=choose();
echo 'this is the data after the page reloads :',$aftertest;
}
//here you process the data before the page reloads
if ((isset($_POST["submit"])) && ($_POST["submit"]!="")){
$choice=$_POST["choice"];
//running the function
choose();
//getting the output from the function
$testbefore=choose();
}
?>
<body>
</html>