我有两个PHP页面,A和B.
页面A有一个表单,它将值为'edit'的$ _POST ['mode']变量传递给页面B.
页面B也有其形式,其提交按钮名为“_go”。页面B检查是否设置了$ _POST ['_ go']参数并在同一页面B中处理表单。
问题在于,在页面B上,如果我在表单上输入一些值并在第一次提交表单,它就可以正常工作并获得成功消息。但是,在我收到成功消息后,在没有刷新页面的情况下,重新输入另一个值并重新提交表单,我什么都没得到,并且没有处理重新输入的值。
我自己调查了一下,发现$ _POST ['_ mode']是导致问题的原因。当我第一次提交表单时,$ _POST ['_ mode']是'edit'。但是当我重新提交表单时,$ _POST ['_ mode']仍然设置但它的值为空。
我在这里很沮丧。
如何保持$模式不变?
我在第A页的代码如下(简化)。
// PAGE A
<form action="Page B" id="some id" name="some name" method="post">
<input type="hidden" name="mode" value="edit" />
<input type="submit" class="btn" value="Edit" />
</form>
我在B页的代码如下(简化)。
// PAGE B
<?php
// 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>
<?php
// This $_POST['mode'] variable is passed from Page A
if( isset( $_POST['mode'] ) ) {
$mode = $_POST['mode'];
}
?>
<form 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>
作为解决方法,我更改了以下代码。现在它按预期工作,但我觉得这不是一个很好的方法...
Before:
if( $_go_mode == 'edit' ) {
process_form();
}
After:
if( $_go_mode == 'edit' ) {
$_POST['mode'] = $_go_mode;
process_form();
}
答案 0 :(得分:0)
我已修改您的代码以使用会话,它将解决丢失变量的问题,您甚至可以在其他页面中重复使用它。我已将页面的操作设置为“”,因为我在页面A中创建会话,然后重定向到页面b .note“session_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>
第B页
<?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>