1

如果值的格式不正确,则用于处理我的表单操作的脚本将重定向到表单页面。我想用用户在重定向到表单页面时输入的错误数据填充文本字段和文本区域。我编写了以下脚本,该脚本在提交错误值时重定向页面,但此后不填写字段。

表单页面上的脚本:

<?php
if(session_id('stat')=="true")
{
$isbn=$_SESSION['var1'] ;
$name=$_SESSION['var2'] ;
$author=$_SESSION['var3'] ;
$publisher=$_SESSION['var4'];
$price=$_SESSION['var5'];
$descrip=$_SESSION['var6'];
$status=$_SESSION['stat'];
}
else
{
$isbn="";
$name="";
$author="";
$publisher="";
$price="";
$descrip="";
$status=false;
}
?>

表单的html部分:

<form action="scripts/addscript.php" method="POST" enctype="multipart/form-data" name="form1" id="form1">

<label for="isbn">ISBN</label>
<input type="text" name="isbn" id="isbn" value="<?php $isbn ?>"/>
 <p>
 <label for="name">Name</label>
 <input type="text" name="name" id="name"  value="<?php echo $name; ?>"/>
 </p>
 <p>
 <label for="author">Author</label>
 <input type="text" name="author" id="author" value="<?php echo $author; ?>"/>
 </p>
<p>
<label for="publisher">Publisher</label>
<input type="text" name="publisher" id="publisher" value="<?php echo $publisher; ?>"/>
</p>
<p>
<label for="price">Price</label>
<input type="text" name="price" id="price" value="<?php echo $price;?>"/>
</p>
<p>
<label for="description">Description</label>
<textarea name="description" id="description" cols="45" rows="5"><?php echo $descrip; ?></textarea>
</p>
 <p>
 <label for="img">Select an image for the book:
<input type="file" name="img" id="img" />
</label>
<input type="submit" name="submit" id="submit" value="Submit"/>
</p>
</form>

表单值提交到的 addscript.php 上的重定向脚本:

<?php
// Get values from form 
$isbn=$_POST['isbn'];
$name=$_POST['name'];
$author=$_POST['author'];
$publisher=$_POST['publisher'];
$price=$_POST['price'];
$descrip=$_POST['description'];

$_SESSION['var1'] = $isbn;
$_SESSION['var2'] = $name;
$_SESSION['var3'] = $author;
$_SESSION['var4'] = $publisher;
$_SESSION['var5'] = $price;
$_SESSION['var6'] = $descrip;
if(strlen($isbn)==0||strlen($name)==0||strlen($author)==0||strlen($publisher)==0||strlen($price)==0)
{
$_SESSION['stat']="true";
header('Location: ' . $_SERVER['HTTP_REFERER']);
}

请告诉我问题出在哪里,我该如何解决这个问题?提前致谢。

4

1 回答 1

1

session_start()必须在 PHP 脚本的顶部调用才能使用该$_SESSION变量。

于 2012-10-17T12:39:15.197 回答