Adding code to a form button?

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
phpnitemare
Forum Newbie
Posts: 12
Joined: Sat Mar 28, 2009 1:14 pm

Adding code to a form button?

Post by phpnitemare »

Hello,

I use a button which resets the contents of my basket, although this code is currently on a php page and therefore the button takes the user to this page to reset the variables. Is it possible to add the code directly to the button to the variables are changed on a mouse click?

thanks,

Code: Select all

"<form method='post' action='credits.php'>".
           "<input type='hidden' value='$album' name='album_id'>".
        "<input type='hidden' value='$album' name='album'>".
           "<input type='hidden' value='add' name='action'>".
           "<input type='submit' value='  Empty Basket  '>".
           "</form>".
User avatar
it2051229
Forum Contributor
Posts: 312
Joined: Tue Dec 25, 2007 8:34 pm

Re: Adding code to a form button?

Post by it2051229 »

yes it is possible.. you're going to make use of javascript for that.
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Re: Adding code to a form button?

Post by kaszu »

HTML:

Code: Select all

<form onsubmit="return resetForm(this);" action="" method="POST"></form>
Javascript:

Code: Select all

//All types of inputs, which values should be reseted
var validInputs = {'hidden': true, 'text': true, 'password': true};
 
//Handle form submit
function resetForm(form) {
    //Get all inputs, which are in the form; textareas and selects are not included
    var inputs = form.getElementsByTagName('INPUT');
 
    //Go through all inputs
    for(var i=0,j=inputs.length; i<j; i++) {
 
        //If type is in the validInputs list, then
        if (validInputs[inputs[i].getAttribute('type')]) {
            //set value to nothing
            inputs[i].value = '';
        }
    }
 
    //Prevent defautl behaviour, which is submitting form
    return false;
}
Post Reply