如果用用户填写的较早输入刷新表格,我将尝试保留输入的值。但是每次我出错时,页面刷新仍然为空。
if ( isset($_POST['make']) && isset($_POST['model']) && isset($_POST['year']) && isset($_POST['mileage'])){
if(strlen($_POST['make']) < 1 || strlen($_POST['model']) < 1 || strlen($_POST['year']) < 1 || strlen($_POST['mileage']) < 1){
$_SESSION["error"] = "All values are required";
header('Location: add.php');
return;
}
在这里,我尝试将post make值作为参数传递给会话,但是,它也不起作用!
elseif (strlen($_POST['make']) < 1 ){
$_SESSION["error"] = "Make is required.";
$_SESSION['make'] = $_POST['make'];
header("Location: add.php?make=".$_POST['make']);
return;
}
然后我在刷新时将其作为url的一部分传递,并使用了全局get值仍然无法正常工作!
else if (strlen($_POST['model']) < 1 ){
$_SESSION["error"] = "Model is required.";
header("Location: add.php?model=".$_POST['model']);
return;
}
else if(strlen($_POST['year']) < 1 ){
$_SESSION["error"] = "Year is required.";
header("Location: add.php?year=".$_POST['year']);
return;
}
else if( strlen($_POST['mileage']) < 1){
$_SESSION["error"] = "Mileage is required.";
header("Location: add.php?mileage=".$_POST['mileage']);
return;
}
else if (!is_numeric($_POST['year']) || !is_numeric($_POST['mileage'])){
$_SESSION["error"] = "Mileage and year must be numeric";
header('Location: add.php');
return;
}else {
$sql = "INSERT INTO autos (make, model, year, mileage)
VALUES (:make, :model, :year, :mileage)";
// // echo("<pre>\n".$sql."\n</pre>\n");
// $opt = "Record Inserted";
$stmt = $pdo->prepare($sql);
$stmt->execute(array(
':make' => $_POST['make'],
':model' => $_POST['model'],
':year' => $_POST['year'],
':mileage' => $_POST['mileage']));
}
$_SESSION["success"] = "added";
header('Location: view.php');
return;
}
然后我在此处将其更改为侦听提交,并且设置了提交后,应将post的所有值作为会话值传递,但是仍然为空。
if (isset($_POST['submit'])) {
$_SESSION['make'] = $_POST['make'];
$_SESSION['model'] = $_POST['model'];
$_SESSION['year'] = $_POST['year'];
$_SESSION['mileage'] = $_POST['mileage'];
}
?>
<?php
if ( isset($_SESSION["error"]) ) {
echo('<p style="color:red">'.$_SESSION["error"]."</p>\n");
unset($_SESSION["error"]);
}
?>
<br>
<p>Adding A New Auto</p>
<form id="autoAdd" method="post">
<p>Make:
然后我检查是否设置了get并尝试将其传递给值,如果没有,则应输出不带值的输入。
<?php
if(isset($_GET['make'])) {
// $make = $_GET['make'];
$make = htmlentities($_SESSION['make']);
echo '<input type="text" name="make" size="40" value="'.$make.'">';
}
else{
echo '<input type="text" name="make" size="40">';
}
?>
</p>
<p>Model:
然后我检查是否设置了get并尝试将其传递给值,如果没有,则应输出不带值的输入。
<?php
if(isset($_GET['model'])) {
$model = $_GET['model'];
echo '<input type="text" name="model" size="40" value="'.$model.'">';
}
else{
echo '<input type="text" name="model" size="40">';
}
?>
</p>
<p>Year:
然后我检查是否设置了get并尝试将其传递给值,如果没有,则应输出不带值的输入。
<?php
if(isset($_GET['year'])) {
$year = $_GET['year'];
echo '<input type="text" name="year" size="40" value="'.$year.'">';
}
else{
echo '<input type="text" name="year" size="40">';
}
?>
</p>
<p>Mileage:
然后我检查是否设置了get并尝试将其传递给值,如果没有,则应输出不带值的输入。
<?php
if(isset($_GET['mileage'])) {
$mileage = $_GET['mileage'];
echo '<input type="text" name="mileage" size="40" value="'.$mileage.'">';
}
else{
echo '<input type="text" name="mileage" size="40">';
}
?>
</p>
<button type="submit" value="Add">Add</button>
答案 0 :(得分:0)
我明白了!! 我创建了变量并将像这样的值传递给Global POST:
self
然后按如下所示更改标题和条件测试用例:
$make = $_POST['make'];
然后像往常一样,将 if(strlen($make) < 1 ){
$_SESSION["error"] = "Make is required.";
header("Location: add.php?autosInput=mileage&make=$make&model=$model&year=$year&mileage=$mileage");
return;
}
留在输入端