[英]Error retaining form field values on a html form
I am trying to retain the values on a form field (title) when a form is submitted. 我正在尝试在提交表单时将值保留在表单字段(标题)上。 I want the values to remain on the form field even when an error message is displayed but this is not working.
我希望这些值即使在显示错误消息时仍保留在表单字段中,但无法正常工作。 It returns a blank space.
它返回一个空格。 What is the problem here?
这里有什么问题?
<?php
$err = array(
'01' => "Your Ad was submitted!",
'02' => "Your Ad was not submitted, try again",
'03' => "Image format not supported",
'04' => "Only letters and numbers are allowed",
'05' => "Only letters and numbers are allowed"
);
$err_code = isset($_GET['err']) ? $_GET['err'] : null;
if (isset($_POST['submit'])) {
$param = array(
'title' => $_POST['title'],
'category' => $_POST['category'],
'school' => $_POST['school'],
'description' => $_POST['description'],
'price' => $_POST['price'],
'date' => date('Y-m-d H:i:s'),
'member' => $_SESSION['id']
);
$sql = "INSERT INTO ads
(title, category, school, description, price, member_id, date)
VALUES
(:title, :category, :school, :description, :price, :member_id, :date)";
if ($db->query($sql, $param)) {
$ad_id = $db ->getLastInsertId();
header ("Location: submit_ad.php?err=01");
}
else{
header ("Location: submit_ad.php?err=02");
}
?>
<form action="" class="" role="form" id="idea" method="post" enctype="multipart/form-data">
<?php
echo ('01' == $err_code) ? "<span class='error'>{$err['01']}</span>" : '';
echo ('02' == $err_code) ? "<span class='error'>{$err['02']}</span>" : '';
echo ('03' == $err_code) ? "<span class='error'>{$err['03']}</span>" : '';
?>
<div class='form-group'>
<label for="title">Title*</label>
<input type="text" class="form-control form-3x" id="title" name="title" value="<?php echo isset($_POST['title']) ? $_POST['title'] : '' ?>" >
<p class='help-block'>Add a short title to describe your Ad</p>
</div>
<div class='form-group'>
<label for="desc">Description*</label>
<textarea name="description" class="form-control form-3x" id="desc"></textarea>
<p class='help-block'>Describe your ad</p>
</div>
<div class='form-group'>
<input type="submit" value="Post" name="submit" class="btn btn-success" />
</div>
</form>
You have input twice, and you are using name attribute twice. 您输入两次,并且两次使用name属性。
<input type="text" class="form-control form-3x" id="title" name="title" <input type="text" name="myField1" value="<?php echo isset($_POST['title']) ? $_POST['title'] : '' ?>" >
The correct syntax would be: 正确的语法为:
<input type="text" class="form-control form-3x" id="title" name="title" value="<?php echo isset($_POST['title']) ? $_POST['title'] : '' ?>" >
name of input has to be "title" for your php code to work 输入的名称必须为“ title”才能使您的php代码正常工作
Browsers usually clear a form of input when a new page is requested. 当请求新页面时,浏览器通常清除一种输入形式。 Since you do a
header ("Location: submit_ad.php?err=01");
由于您执行了
header ("Location: submit_ad.php?err=01");
it clears out the field and you've lost the $_POST data. 它会清除该字段,并且您丢失了$ _POST数据。
What you can do in this case is to store the post data in a session and echo it in a value attribute of the input element. 在这种情况下,您可以做的是将发布数据存储在会话中,并将其回显到输入元素的value属性中。
<input type="text" name="fname" value="<?php echo $_SESSION['post.fname']; ?>">
title input your looks like this 标题输入如下所示
<input type="text" class="form-control form-3x" id="title" name="title" <input type="text" name="myField1" value="<?php echo isset($_POST['title']) ? $_POST['title'] : '' ?>" >
issue here ^ to here ^
should be: 应该:
<input type="text" class="form-control form-3x" id="title" name="title" value="<?php echo isset($_POST['title']) ? $_POST['title'] : '' ?>" >
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.