开发者

PHP/Javascript: Restart Form Button Isn't Working, Code Insde

开发者 https://www.devze.com 2022-12-30 09:13 出处:网络
Basically I want a confirmation box to pop up when they click the button asking if they sure they want to restart, if yes, then it destroys session and takes them to the first pa开发者_C百科ge. Heres

Basically I want a confirmation box to pop up when they click the button asking if they sure they want to restart, if yes, then it destroys session and takes them to the first pa开发者_C百科ge. Heres what i got...

echo "<form id=\"form\" name=\"form\" method=\"post\" action=\"nextpage.php\">\n";
echo "  <input type=\"button\" name='restart' value='Restart' id='restart'
        onclick='restartForm()' />";

and for the script...

<script type=\"text/javascript\">
     <!--
    function restartForm() {
    var answer = confirm('Are you sure you want to start over?');
    if (answer) {
        form.action=\"firstpage.php\";
        session_destroy();
        form.submit();
    } else
        alert ('Restart Cancelled');
    }
    // --
</script>";

EDIT: Note that pressing the button brings up the confirm box, but if you click okay nothing happens sometimes. Sometimes if u click cancel it still submits the form (To the original action)


Unless your session_destroy() method in Javascript actually sends a request to the PHP script or something, it looks like you are trying to put PHP code in your javascript, which will not work.

You should try redirecting them to something like firstpage.php?reset=1 and inside the PHP script, you can check for the reset flag, then call session_destroy().


You should use

onclick='restartForm(); return false;'

to prevent the form from being sent.

Also, session_destroy() is not JS AFAIK...

EDIT: you should also remember that sessions are server-side, not client side, so if you want to manipulate them you cannot do it in JS. The only thing you can do is calling a PHP (e.g. via AJAX) that does something with the sessions.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号