javascript - How to replace the windows.alert into a sweetalert?

635
<form form method="POST" action="login.php" autocomplete="off">
    <div >
      <label><b><i ></i> Username</b></label>
      <input type="text" placeholder="Enter Username" name="username" required>
      <label><b><i id="iconcart"></i> Password</b></label>
      <input type="password" placeholder="Enter Password" name="password" required>
      <button type="submit">Login</button>
      <input type="checkbox" checked="checked"> Remember me
    </div>
  </form>








 if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $username=check_input($_POST['username']);

    if (!preg_match("/^[a-zA-Z0-9_]*$/",$username)) {
      ?> 
      <script>
        window.alert('Username should not contain space and special characters!');
        window.history.back();
      </script>
    <?php
    }
    else{

    $fusername=$username;

    $password = check_input($_POST["password"]);
    $fpassword=md5($password);

    $query=mysqli_query($conn,"select * from `user` where username='$fusername' and password='$fpassword'");

    if(mysqli_num_rows($query)==0){
      ?>

      <script>
          window.alert('Login Failed, Invalid Username or Password!');
          window.history.back();
        </script>
257

Answer

Solution:

window.alert=function(event) {
    event.preventDefault();
    /* Your Sweetalert code here */  
}

People are also looking for solutions to the problem: php - Session variable is not available

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.