lordflapjack
lordflapjack

Reputation: 31

session_unset and onclick

I'm trying to clear a form that has been populated using PHP sessions.

   Your name: <input type="text" name="name" 
       value="<?php echo($_SESSION['username'])?>" >
   Your email address: <input type="text" name="email" 
       value="<?php echo($_SESSION['email'])?>">
   <br>
       <textarea name="story" cols="60" rows="10">
   <?php echo($_SESSION['story']); ?>
       </textarea> 
   <br>
   <input type="submit" value="Submit"/><input type="reset" value="Reset">

The problem is that when I hit the reset button, the form does not clear.

I think I need to use the session_unset command but no matter where I put it, the form is cleared before the reset button is pushed.

How can I combine session_unset and a reset button?

Upvotes: 0

Views: 4093

Answers (2)

You can do this is two ways. You can start giving the two submit buttons a NAME, and then make a PHP script, which unset the defined Sessions by

<?php if(isset($_POST['reset']){unset($_SESSION['username'],$_SESSION['email']);} ?>

And then of course change the Value to <?=$_SESSION['username'];?> and <?=$_SESSION['email'];?> - Because then it'll only show value if the two sessions contains any.

Otherwise you can do it with JQuery.

EDIT:

^ You should ofcourse also unset the $_SESSION['story'] (Didn't see it at first)

EDIT2:

$(document).ready(function(e) {
    $("form input[type=reset]").click(function(e) {
        $("form input[name=name]").attr("value","");
        $("form input[name=email]").attr("value","");
        $("form textarea[name=story]").attr("value","");
        return false;
    });
});

or if you want it to reset 'anything' which is text-based:

$(document).ready(function(e) {
    $("form input[type=reset]").click(function(e) {
        $("form input[type=text], form input[type=email], form input[type=password], form textarea").attr("value","");
        return false;
    });
});

^ Add more options if you like to :)

Upvotes: 1

Touch
Touch

Reputation: 1491

I think the Reset button resets the form into the way it was received from the server. So that could be why it is not working. It was received with data, thus the button takes back to it's original state. I could be wrong though, haven't tried that before.

And by the way, to unset a $_SESSION, you could do this

unset($_SESSION['specific']);

or you could destroy the whole thing.

session_destroy();

Upvotes: 0

Related Questions