好的伙计..
我有两页
page1包含一个表单
<form id="Form1" name="Form1" method="post" action="form1.PHP"> //form1.PHP is the page1 where this form is..
<input type="text" class="input" name="txtName" value="<?PHP if(isset($name)){echo $name;} ?>" <?PHP if(isset($flag) && $flag == 1){echo "div style = 'border:2px solid red;'". "/div";}?>> //HERE EVERYTHING IS GOOD
<input type="submit" name="submit" value="Submit" class="button3">
</form>
我使用PHP会话将数据发送到page2
session_start();
if(NO ERRORS)) // in the form there is actual code
{
//insert into database
$result = MysqL_query($insert);
if($result)
{
echo("<br>Input data is succeed");
$lastInsertedId = MysqL_insert_id();
$_SESSION['last_id'] = $lastInsertedId;
header('Location: form1_conf.PHP?id');
}
else
{
$message = "The data cannot be inserted.";
$message .= "<br />" . MysqL_error();
}
现在来了第2页
page2名称是form1_conf.PHP&用于向用户显示表单数据,以便他可以检查表单是否有错误,如果有任何错误,他可以单击编辑并返回主表单(第1页)并重新输入数据并重新提交表单.
这是page2代码
这里我使用PHP从page1接收数据:
<?PHP
session_start();
$id = $_SESSION['last_id'];
$query = "SELECT * FROM db_form1 WHERE id=$id";
$result = MysqL_query($query);
while($row = MysqL_fetch_row($result))
{
$name = $row[3];
}
?>
<div id="displayForm">
<div class="dispText">
<?PHP echo $name; ?>
</div>
</div>
<a href="#" class="button">EDIT</a>
现在我有两个问题
>人们告诉我,如果表单在多个选项卡中打开,会话可能会成为一个问题,会话可能会陷入困境.所以我建议使用get.如果这个陈述是有效的,那么PHP代码中的修改应该用get或者更好的方法来完成它?
>是否修改了此代码以通过get获取工作,如果用户单击第2页上的编辑按钮,如何将此表单重定向到page1?看#with EDIT按钮.如何实现?
非常感谢您的帮助朋友.
解决方法:
form.PHP的
<?PHP
if(isset($_POST['txtName'])) {
//the form was submitted, validate the data and insert to the database
if(NO ERRORS)) // in the form there is actual code
{
//insert into database
$result = MysqL_query($insert);
if($result)
{
$lastInsertedId = MysqL_insert_id();
$_SESSION['last_id'] = $lastInsertedId;
//do not echo anything before you try to redirect. There must not be any output before header
header("Location: the_page_that_displays_your_data.PHP?id=$lastInsertedId");
} else
{
$errorMessage = "The data cannot be inserted.";
$errorMessage .= "<br />" . MysqL_error();
}
} else {
//check your errors and display them in the form below
}
}
?>
<!--set action to "" so the page will submit to itself-->
<form id="Form1" name="Form1" method="post" action="">
<!--if $errorMessage is defined you can echo it here (for example)-->
<!--if the txtName in $_POST is set display it after escaping it-->
<input type="text" class="input" name="txtName" value="<?PHP echo isset($_POST['txtName']) ? htmlspecialchars(filter_input(INPUT_POST, 'txtName'), ENT_COMPAT, 'UTF-8') : ''; ?>">
<input type="submit" name="submit" value="Submit" class="button3">
</form>
希望这可以帮助!