Hey all, I have built a form that will keep the data in the textboxes incase there is an error before it is submitted to a database, but even if the form has no errors the data is still there, but I want it to be cleared if there are no errors and everything goes well.
Can anyone tell me how this could be done?
clearing form after posting
Moderator: General Moderators
<--here's the function-->
function user_form(){
/* if vars aren't set, set them as empty.
// (prevents "notice" errors showing for those who have them enabled)*/
if (!isset($_POST['item_number'])) $_POST['item_number'] = '';
if (!isset($_POST['description'])) $_POST['description'] = '';
if (!isset($_POST['price'])) $_POST['price'] = '';
if (!isset($_POST['category'])) $_POST['category'] = '';
/* now to output the form HTML.*/
echo '<p>All fields are required.</p>';
echo '<form action="'.htmlspecialchars($_SERVER['PHP_SELF']).'" method="post">';
echo '<table border=0 width=90% align=center>';
echo '<tr><td>';
echo 'item_number:';
echo '<td>';
echo '<input type=text name=item_number value="'.htmlspecialchars($_POST['item_number']).'">';
echo '</td></tr>';
echo '<tr><td>';
echo 'description:';
echo '<td>';
echo '<input type=text name=description value="'.htmlspecialchars($_POST['description']).'">';
echo '</td></tr>';
echo '<tr><td>';
echo 'price:';
echo '<td>';
echo '<input type=text name=price value="'.htmlspecialchars($_POST['price']).'">';
echo '</td></tr>';
/* let's make the catetory input a select box
// you can see how we pre-select an option
//Dropdown menu code*/
echo '<tr>';
echo '<td>';
mysql_select_db(mom);
$contents_of_categories_db = "SELECT category_name FROM categories";
$result = mysql_query($contents_of_categories_db);
echo "Category:";
echo '<td>';
echo "<select name=category_name>";
while($row = mysql_fetch_row($result)){
for ($i=0; $i<1; $i++){
echo "<option>";
echo ("$row[$i] " );
}
}
echo "</option>";
echo "</select>";
echo '</td>';
echo '</tr>';
echo '<tr>';
echo '<td colspan=2 align=right>';
echo "<input type=HIDDEN name=stage VALUE=1>";
echo "<input type=submit value=Submit name=submit>";
echo "</form>";
echo '</td>';
echo '</tr>';
echo '</table>';
}
?>
<--here is the code-->
if (isset($_POST['submit'])) {
/* the form has been submitted*/
/* perform data checks.*/
$error_str = ''; // initialise $error_str as empty
if (empty($_POST['item_number'])) $error_str .= '<li>You did not enter your item number.</li>';
if (empty($_POST['description'])) $error_str .= '<li>You did not enter your description.</li>';
if (empty($_POST['price'])) $error_str .= '<li>You did not enter your price.</li>';
/* more checks*/
if (strlen($_POST['item_number']) < 2) $error_str .= '<li>Your item number must be longer than one number.</li>';
if (strlen($_POST['description']) < 2) $error_str .= '<li>Your description must be longer than one letter.</li>';
if (strlen($_POST['price']) < 3) $error_str .= '<li>Your price must be longer than one number and be in the format $0.00.</li>';
/* we could do more data checks here.*/
/*// we could also strip any HTML from the variables, convert it to entities, have a maximum character limit on the values, etc etc.*/
if (empty($error_str)) {
mysql_select_db(mom);
$insert_new_item = ("INSERT INTO products (item_number, description, price, category_name) VALUES ('$_POST[item_number]', '$_POST[description]', '$_POST[price]', '$_POST[category_name]')");
$result = mysql_query($insert_new_item);
}
} if(!empty($error_str)) {
// errors have occured, halt execution and show form again.
echo '<p>There were errors in the information you entered, they are listed below:</p>';
echo '<ul>'.$error_str.'</ul>';
// show form again
user_form();
exit; // die
}
user_form();
?>
function user_form(){
/* if vars aren't set, set them as empty.
// (prevents "notice" errors showing for those who have them enabled)*/
if (!isset($_POST['item_number'])) $_POST['item_number'] = '';
if (!isset($_POST['description'])) $_POST['description'] = '';
if (!isset($_POST['price'])) $_POST['price'] = '';
if (!isset($_POST['category'])) $_POST['category'] = '';
/* now to output the form HTML.*/
echo '<p>All fields are required.</p>';
echo '<form action="'.htmlspecialchars($_SERVER['PHP_SELF']).'" method="post">';
echo '<table border=0 width=90% align=center>';
echo '<tr><td>';
echo 'item_number:';
echo '<td>';
echo '<input type=text name=item_number value="'.htmlspecialchars($_POST['item_number']).'">';
echo '</td></tr>';
echo '<tr><td>';
echo 'description:';
echo '<td>';
echo '<input type=text name=description value="'.htmlspecialchars($_POST['description']).'">';
echo '</td></tr>';
echo '<tr><td>';
echo 'price:';
echo '<td>';
echo '<input type=text name=price value="'.htmlspecialchars($_POST['price']).'">';
echo '</td></tr>';
/* let's make the catetory input a select box
// you can see how we pre-select an option
//Dropdown menu code*/
echo '<tr>';
echo '<td>';
mysql_select_db(mom);
$contents_of_categories_db = "SELECT category_name FROM categories";
$result = mysql_query($contents_of_categories_db);
echo "Category:";
echo '<td>';
echo "<select name=category_name>";
while($row = mysql_fetch_row($result)){
for ($i=0; $i<1; $i++){
echo "<option>";
echo ("$row[$i] " );
}
}
echo "</option>";
echo "</select>";
echo '</td>';
echo '</tr>';
echo '<tr>';
echo '<td colspan=2 align=right>';
echo "<input type=HIDDEN name=stage VALUE=1>";
echo "<input type=submit value=Submit name=submit>";
echo "</form>";
echo '</td>';
echo '</tr>';
echo '</table>';
}
?>
<--here is the code-->
if (isset($_POST['submit'])) {
/* the form has been submitted*/
/* perform data checks.*/
$error_str = ''; // initialise $error_str as empty
if (empty($_POST['item_number'])) $error_str .= '<li>You did not enter your item number.</li>';
if (empty($_POST['description'])) $error_str .= '<li>You did not enter your description.</li>';
if (empty($_POST['price'])) $error_str .= '<li>You did not enter your price.</li>';
/* more checks*/
if (strlen($_POST['item_number']) < 2) $error_str .= '<li>Your item number must be longer than one number.</li>';
if (strlen($_POST['description']) < 2) $error_str .= '<li>Your description must be longer than one letter.</li>';
if (strlen($_POST['price']) < 3) $error_str .= '<li>Your price must be longer than one number and be in the format $0.00.</li>';
/* we could do more data checks here.*/
/*// we could also strip any HTML from the variables, convert it to entities, have a maximum character limit on the values, etc etc.*/
if (empty($error_str)) {
mysql_select_db(mom);
$insert_new_item = ("INSERT INTO products (item_number, description, price, category_name) VALUES ('$_POST[item_number]', '$_POST[description]', '$_POST[price]', '$_POST[category_name]')");
$result = mysql_query($insert_new_item);
}
} if(!empty($error_str)) {
// errors have occured, halt execution and show form again.
echo '<p>There were errors in the information you entered, they are listed below:</p>';
echo '<ul>'.$error_str.'</ul>';
// show form again
user_form();
exit; // die
}
user_form();
?>
well, since the data is in a array, $_POST in this case, you can unset the array. there are two ways.
1)
2)
unset selected form fields your self.
make sense?
1)
Code: Select all
<?php
foreach($_POST as $key => $value)
{
unset($_POST['$key']);
}
?>unset selected form fields your self.
Code: Select all
<?php
unset($_POST['username']);
unset($_POST['email']);
?>just a note
you cant go like
you have to tell it what you want to remove from the array, so you have to use like this:
you cant go like
Code: Select all
<?php
unset($_POST);
?>Code: Select all
<?php
unset($_POST['field_name']);
?>