Best Way to Clear Forms?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
snowrhythm
Forum Commoner
Posts: 75
Joined: Thu May 04, 2006 1:14 pm
Location: North Bay Area, CA

Best Way to Clear Forms?

Post 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

Code: Select all

$_GET['inputfieldname']
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?
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

You could do it with javascript...

Code: Select all

document.formname.formelementname.value = '';
User avatar
snowrhythm
Forum Commoner
Posts: 75
Joined: Thu May 04, 2006 1:14 pm
Location: North Bay Area, CA

Post 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?
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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>

User avatar
snowrhythm
Forum Commoner
Posts: 75
Joined: Thu May 04, 2006 1:14 pm
Location: North Bay Area, CA

Post by snowrhythm »

The javascript worked perfectly! Thanks for you help, goat, you're a gentleman and a ninja. :wink:
User avatar
Cameri
Forum Commoner
Posts: 87
Joined: Tue Apr 12, 2005 4:12 pm
Location: Santo Domingo, Dominican Republic

Post by Cameri »

Afaik, you could also use:

Code: Select all

<form id="myForm">
...
</form>

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
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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.
Post Reply