Page 1 of 1
Best Way to Clear Forms?
Posted: Wed Nov 29, 2006 12:00 pm
by snowrhythm
Hey people, I have an application that allows users to search a database through a form and I've set the default value
of the input fields to
so that they can see exactly what they typed in the fields after they've
queried. Now I want to have a button that clears the form completely, but the trusty html reset button won't do the trick because it
only restores the values that were in the form when the page loaded, so it doesn't change anything after they've searched.

Does anybody have any ideas on how a guy could pull this off?
Posted: Wed Nov 29, 2006 12:01 pm
by Luke
You could do it with javascript...
Code: Select all
document.formname.formelementname.value = '';
Posted: Wed Nov 29, 2006 12:10 pm
by snowrhythm
so there's no way of doing it in php? I guess i could use javascript...if i absolutely had to.
has there been any talk of php being able to do a little domtree gymnastics like javascript in future versions?
Posted: Wed Nov 29, 2006 12:19 pm
by Luke
Well... you could have a button that refreshes the page and let php set the values to empty...
Code: Select all
<?php
$formelement = '<input type="text" name="name" value="' . $name . '">';
if($_POST['action'] == 'Reset')
{
$formelement = '<input type="text" name="name" value="">';
}
?>
<form method="post" action="#">
<?php echo $formelement; ?>
<input type="submit" name="action" value="Reset">
</form>
Posted: Wed Nov 29, 2006 1:05 pm
by snowrhythm
The javascript worked perfectly! Thanks for you help, goat, you're a gentleman and a ninja.

Posted: Wed Nov 29, 2006 2:22 pm
by Cameri
Afaik, you could also use:
Code: Select all
document.getElementById('myForm').reset();
//or
document.forms["myForm"].reset();
//or
document.forms[0].reset(); // <-- the first form is the one u want to reset
Posted: Wed Nov 29, 2006 2:37 pm
by Luke
resetting is not what he wants... that will just reset it to whatever is in the value="" attribute. He wants to empty the elements, not reset them.