How do you make a "Cancel" button for forms?

I need to make a cancel button for the form - a button that goes to another page without processing form.

Thanks for your help.

Might depend on what you mean by “cancel”.

If its “restore the form to the state it was in when the page loads” youre looking for the straight html RESET in the form. <input type=reset> This in effect is often named “abandon changes” or similar.

If thats not what you mean but some other “go back” action, well its either go back to the server and do a “Header : Location …” php redirect, or if a JS history.go(-1) type of thing which you’ll find in the JS forum I guess…

Else what do you want cancel to do exactly?


<form action="formmail.php" method="post">
....
<input type="submit" name="submitted" value="cancel">
<input type="submit" name="submitted" value="don't cancel">
</form>


if ($_GET['submitted'] == "cancel")
{
    header("Location: cancel.php");
}
...

Real basic but it should give you the idea.

@webdevguy et al
Sorry I didnt read that properly… “a button that goes to another page without processing form.”

if your buttons are images, you can just put a link on the image to the “cancel” page. Not sure why you would POST only to destroy the data and go to another page when you can just outright link to another page.

what does it matter if I POST or GET.

I think stymiee’s will work great in that I will just POST then check the submitted value.

I’ve been using PEAR Quickform and am going to start rolling my own. Take a look at this link for form submission security - good stuff:

Thanks to everyone.

You can also make a cancel button like this, using the onClick JS event:


<form name="form" method="post" action="process.php">
<input type="submit" name="submit" value="submit" />
<input type="button" name="cancel" value="cancel" onClick="window.location='http://yoursite.com/index.php';" />
</form>

Why would you POST data just to destroy it on the server and send a redirect? When a normal link is what you want.

POSTing data just to destroy it and redirect just wastes bandwidth.

You can also make the cancel button a form upon itself and submit no data and then let php make the redirect. That way no javascript is used which may be truned off.