简体   繁体   English

PHP / Javascript:重新启动表单按钮不起作用,代码为Insde

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

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 page. 基本上,我希望当用户单击按钮时弹出确认对话框,询问是否确定要重新启动。如果是,则销毁会话并将其带到第一页。 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. 除非Javascript中的session_destroy()方法实际将请求发送到PHP脚本之类,否则您似乎试图将PHP代码放入JavaScript中,这将无法正常工作。

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() . 您应该尝试将其重定向到firstpage.php?reset=1并在PHP脚本内,可以检查reset标志,然后调用session_destroy()

You should use 你应该用

onclick='restartForm(); return false;'

to prevent the form from being sent. 以防止表单被发送。

Also, session_destroy() is not JS AFAIK... 此外,session_destroy()也不是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. 编辑:您还应该记住,会话是服务器端的,而不是客户端的,因此,如果要操作它们,则无法在JS中进行。 The only thing you can do is calling a PHP (eg via AJAX) that does something with the sessions. 您唯一可以做的就是调用PHP(例如通过AJAX)来对会话进行处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM