Reputation: 1447
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>
Upvotes: 0
Views: 147
Reputation: 7911
Browsers usually clear a form of input when a new page is requested. Since you do a header ("Location: submit_ad.php?err=01");
it clears out the field and you've lost the $_POST data.
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.
<input type="text" name="fname" value="<?php echo $_SESSION['post.fname']; ?>">
Upvotes: 1
Reputation: 1006
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'] : '' ?>" >
Upvotes: 0
Reputation: 24
You have input twice, and you are using name attribute twice.
<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
Upvotes: 1