簡體   English   中英

當我單擊“ JavaScript警報”對話框上的“確定”時,頁面將刷新,所有文本框中輸入的數據將為空白

[英]When i click on ok on javascript alert dialog the page refreshed and alll the data inputed in the textbox will be blank

 <?php include "dbconfig.php"; session_start(); ?> <!DOCTYPE html> <html> <head> <title>Log in</title> <link rel="stylesheet" type="text/css" href="styles.css"> <link rel="stylesheet" type="text/css" href="bootstrap.css"> <link href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-glyphicons.css" rel="stylesheet"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"> <style type="text/css"> .error { color: red; } .form-control { color: black; } .btn { color: white; } .panel { width: 340px; margin-left: 350px; margin-top:50px; } .form-control { width: 300px; } .right-addon input { padding-right: 30px; } .right-addon .glyphicon { right: 10px;} </style> </head> <body> <div class="container"> <div class="panel panel-info"> <div class="panel-heading"> <center><strong><h2>Log in</h2></strong></center> </div> <div class="panel-body"> <form method="post" role="form" id="login" enctype="multipart/form-data"> <div class="form-group"> <input type="text" class="form-control" name="loginusername" placeholder="Username"> </div> <div class="form-group "> <div class="inner-addon right-addon"> <input type="password" id="password" class="form-control" name="loginpassword" placeholder="Password"> </div> </div> <div class="form-group"> <h5><a href="#">forgot password ?</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="#">issues while signing in ?</a></h5> </div> <div class="form-group"> <input type="submit" class="form-control btn btn-primary" value="Log in" name="lgn"> </div> </form> </div> <div class="panel-footer"> <center>Don't have an account?<a href="sign_up.php">register</a></center> </div> </div> </div> <!-- Jquery Plugin CDN --> <script src="http://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"> </script> <!-- Jquery Validation Plugin CDn --> <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/jquery-validation@1.17.0/dist/jquery.validate.js"> </script> <!-- Linking Page to index.js for form validation --> <script type="text/javascript" src="index.js"> </script> </body> </html> <?php if (isset($_POST["lgn"])) { $result=mysqli_query($db,"select * from signup where semail='$loginusername' and screatepass='$loginpassword'"); if ($loginusername=="admin@gmail.com" && $loginpassword=="039218") { $_SESSION["session_admin"]=$loginusername; header("location:admin.php"); } elseif (mysqli_num_rows($result)==1) { header("location:Home.php"); } else { echo "<script>alert('Invalid username or password')</script>"; } } ?> 

當我在javascript警報對話框上單擊“確定”時,頁面將刷新,並且在文本框中輸入的所有數據將為空白。 如何克服這個問題? 我不想要這個,我希望在javascript警報上單擊“確定”后不應該刷新頁面。 我還添加了與此問題相關的圖片。

在此處輸入圖片說明

您不是在使用AJAX查詢數據庫,而是需要重新加載頁面才能在服務器上運行PHP代碼。 這將清除輸入。 您應該使用AJAX調用來處理數據庫查詢和輸入驗證以正確解決此問題,但是,作為一種解決方法,您可以在PHP中檢查是否設置了字段的發布值並在HTML中進行設置,如下所示:

<input type="text" class="form-control" name="loginusername" placeholder="Username" value="<?php if (isset($_POST['loginusername'])) echo $_POST['loginusername']; ?>">
<input type="password" id="password" class="form-control" name="loginpassword" placeholder="Password" value="<?php if (isset($_POST['loginpassword'])) echo $_POST['loginpassword']; ?>">

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM