2016-04-30 135 views
0

好吧,这是一个使用ajax导航加载页面加载罚款其自我我试图使它,以便页面提交时,它不会刷新整个页面。我环顾了一会儿,仍然找不到修复它。表单提交停止刷新

<head> 
    <script> 
    $(document).on('submit', '#changePassword', function() { 
     $("#content").load("core.home"); 

     return false; 

    }); 
    </script> 
</head> 
<?php if(!preg_match("/index.php/i", $_SERVER[ 'PHP_SELF'])) { die(); } ?> 

<form action="" method="post" id="changePassword"> 
    <div class="box"> 
    <div class="boxhead purple purple"> 
     <strong>Change password</strong> 
    </div> 
    <?php if($_POST[ 'submit']) { try { $oldpassword=$ core->clean($_POST['current_password']); $oldpassword_enc = $core->encrypt($oldpassword); $newpassword = $core->clean($_POST['new_password']); $newpassword_enc = $core->encrypt($newpassword); if(!$oldpassword or !$newpassword) { throw new Exception(
    "All fields are required."); } elseif($oldpassword_enc != $user->data['password']) { throw new Exception("The password you entered does not match the one we have on record."); } else { $db->query("UPDATE users SET password = '{$newpassword_enc}' 
    WHERE id = '{$user->data['id']}'"); echo " 
    <div class=\ "square good\">"; echo "<strong>Success</strong>"; echo " 
     <br />"; echo "Password successfully changed!"; echo "</div>"; $db->query("INSERT INTO logs VALUES (NULL, 'Changed Password <font color=\ "51c833\"><b>Success</b></font>', NULL, '<b>{$user->data['fullUsername']}</b> ({$_SERVER['REMOTE_ADDR']})', '3') " 
    ); } } catch(Exception $e) { echo " 
    <div class=\ "square bad\">"; echo "<strong>Error</strong>"; echo " 
     <br />"; echo $e->getMessage(); echo "</div>"; $db->query("INSERT INTO logs VALUES (NULL, 'Changed Password <font color=\ "FF0000\"><b>Fail</b></font> Reason: {$e->getMessage()}', NULL, '<b>{$user->data['fullUsername']}</b> ({$_SERVER['REMOTE_ADDR']})', 
    '3') "); } } ?> 

    <table width="100%" cellpadding="3" cellspacing="0"> 
     <?php echo $core->buildField("password", "required", "current_password", "Current password"); echo $core->buildField("password", "required", "new_password", "New password"); ?> 
    </table> 
    </div> 

    <div class="box" align="right"> 
    <button class="button" type="submit" name="submit" value="Submit" id="button">Submit</button> 
    </div> 
</form> 

<?php echo $core->buildFormJS('changePassword'); ?> 
+0

你想做什么实际?详细解释它。 – Archish

+0

这里的代码是一个单独的文件,当它被提交表单时,它被加载到一个ajax框中,它刷新整个网站回index.php我不想要一个网站全面刷新发生 –

回答

0

您需要使用e.preventDefault停止提交按钮的默认行为(即重新加载上提交)

$(document).on('submit', '#changePassword', function(e) 
{ 
    e.preventDefault(); 

}); 
+0

所以我改变了我的脚本在页面的头部,但它仍然完全重新加载页面 –

+0

你能提供你的代码在jsfiddle或你的网站的网址?你应该把它包装在'$(document).ready(function(){//这里的代码为 });'' –