Javascript历史返回清除表单

Javascript历史返回清除表单,javascript,php,jquery,forms,codeigniter,Javascript,Php,Jquery,Forms,Codeigniter,我有两种形式(A和B),它们是通过CI控制器调用的。控制器中的每个功能中都有警告警报,用于在用户输入无效时发出警报。在这两个警报中,都有一个javascript,它在单击警报后将页面重定向到表单页面(history-1)。我的警报代码是: echo "<script>alert('Invalid Input!'); location.href='javascript:window.history.go(-1);'</script>"; echo“警报('Invalid

我有两种形式(A和B),它们是通过CI控制器调用的。控制器中的每个功能中都有警告警报,用于在用户输入无效时发出警报。在这两个警报中,都有一个javascript,它在单击警报后将页面重定向到表单页面(history-1)。我的警报代码是:

echo "<script>alert('Invalid Input!'); location.href='javascript:window.history.go(-1);'</script>";
echo“警报('Invalid Input!');location.href='javascript:window.history.go(-1);”;
我发现这两种形式之间有些奇怪。在表格A中,单击警报后,用户键入的每个输入仍然保留(这是我想要的),但在表格B中,它被清除。我想保留用户在表单B中键入的内容,因为它有这么多表单

如果你有什么解决办法,请告诉我

以下是我的表单A代码片段:

<form method="post" action="<?php echo base_url(); ?>index.php/register/addmember" name="formregister" enctype="multipart/form-data">
    <center>
        <table id="form_register">              
            <tr>
                <td>Email* </td>
                <td id="txtemail"><input placeholder="your email" maxlength="255" type="email" name="email" size="50"/> <br/></td>
            </tr>               
            <tr>
                <td>Username*</td>
                <td id="txtusername"><input placeholder="your username" maxlength="255" type="text" name="username" size="50"/> <br/></td>
            </tr>

            <tr>
                <td>Password*  </td>
                <td id="txtpassword"><input placeholder="your password"  maxlength="255"type="password" name="password" size="50"/><br/></td>
            </tr>                       

            <tr>
                <td>Photo*</td>
                <td><input type="file" name="photo"/></td>
            </tr>
            <tr>
                <td></td>                   
                <td><input type="checkbox" id="agreement" name="agreement"><a href="#" style="text-decoration:none;"><i>I accept the terms and conditions</i></a></input></td>
            </tr>           
            <tr>                
                <td align="right" style="padding-right:15px;"><input type="submit" disabled value="Register" name="submit" id="btnRegister"/></td>
                <td align="left" style="padding-left:15px;"><input type="reset" value="Cancel" id="btnCancel"/></td>                                        
            </tr>           
        </table>
    </center>       
</form> 

我在这里看到两种解决方案。首先,确认如果您仅使用
window.history.go(-1)
,将无法清除历史记录,以便:

解决方案1(推荐)

我建议您通过AJAX处理表单验证,然后您可以控制哪些字段可以清除,哪些字段需要保留填充的数据

解决方案2

您可以重定向用户并传递一个随机参数,该参数将清除(所有)字段,如下所示(a.html仅用于示例,您应该将其替换为表单所在页面的URL):

echo“警报('Invalid Input!');location.href='a.html?'+Math.random();”;

如果我们可以更新您的表单HTMLQUEST updated@sriYou可能应该使用正确的重定向而不是JavaScript。历史,您是指用户键入的内容吗?是的,我不想澄清。我不能使用第二个解决方案,因为它将针对页面的会话。对于第一个解决方案,我将进一步探讨它,但仍然希望在服务器中验证它。谢谢或者你试过使用:来预防它吗?
<form method="post" action="<?php echo base_url(); ?>index.php/festival/adddata/" name="newdata" enctype="multipart/form-data">
                    <div id="edit-area-left">
                    <center>        
                    <table>
                    <tr><td></td>
                        <td><u><b>Main information</b></u></td></tr>
                    <tr>
                        <td>Event Name*</td> 
                        <td><input type="text" maxlength="255" name="eventname" size="50" placeholder="Event Name"/></td>
                    </tr>                       
                    <tr>
                        <td>Time of Event</td>                          
                        <td><input type="text" maxlength="255" name="time" size="50" placeholder="Time of event"></td>
                    </tr>                                           
                </table>
            </center>
                </div>
                <div id="edit-area-right">
                <center>
                <table> 
                    <tr><td></td>
                        <td><u><b>Event Description</b></u></td></tr>
                    <tr>                        
                    <tr>
                        <td>Event Desctiprion*</td>
                        <td><textarea cols="37" rows="5" name="detailevent" id="detailevent" style="resize:none;" maxlength="2000" placeholder="event Detail"></textarea><br/></td>
                    </tr>
                    <tr><td></td>
                        <td><u><b>Image*</b></u></td></tr>
                    <tr>
                    <tr>                            
                        <td><input type="file" name="image"/></td>
                    </tr>
                    <tr></tr>
                    <tr>
                        <td align="right" style="padding-right:15px;"><input type="submit" value="Save" name="save" id="profile-edit-button"/></td>                     
                        <td align="left" style="padding-left:15px;"><input type="reset" value="Cancel" id="profile-edit-button"/></td>
                    </tr>                       
                </table>
                </center>
                </div>
            </form> 
echo "<script>alert('Invalid Input!'); location.href='a.html?' + Math.random();</script>";