好的伙计..
我有两页
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
php page1 code
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&amp;用于向用户显示表单数据,以便他可以检查表单是否有错误,如果有任何错误,他可以单击编辑并返回主表单(第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];
}
?>
以下是显示此内容的HTML代码
<div id="DisplayForm">
<div class="dispText">
<?php echo $name; ?>
</div>
</div>
<a href="#" class="button">EDIT</a>
现在我有两个问题
非常感谢您的帮助朋友们。
答案 0 :(得分:3)
这里是一个解决方案,可以在一个页面中执行此操作:
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>
希望这有帮助!
答案 1 :(得分:2)
您可以使用查询字符串发送数据,以便在header
函数中发送ID或其他内容:
header('Location: form1_conf.php?id='.$id);
或其他任何地方,例如链接:
www.example.com/page2.php?id=445
然后在接收端,您可以使用$_GET
数组获取数据:
if (isset($_GET['id'])){
$id = $_GET['id'];
}
您也可以使用网址更改编辑链接:
<a href="http://www.example.com/page1.php?id=<?php echo $id; ?>" class="button">EDIT</a>