0

Hi so i have this sign up to register new users to database but the alert of JavaScript for register users is not working but the for example on the same page to login the user is working... i mean if u fail a password it will show the password is wrong, but if u try to register the user enter in data base but there isn't any alert saying "New record created successfully"

-----------------------------------register to db and alert----------------------

<?php
    session_start();

    $db=mysqli_connect("localhost","root", "","compras");
    if(isset($_POST['signup'])) {

        $fname = ($_POST['fname']);
        $lname = ($_POST['lname']);
        $password = ($_POST['password']);
        $email = ($_POST['email']);
        $phone = ($_POST['phone']);
        $sql;
        $password=md5($password);

if (!$db) {
    die("Connection failed: " . mysqli_connect_error());
}
$sql = "Insert into users(fname,lname,phone,email,password) values ('$fname','$lname','$phone','$email','$password')";

if (mysqli_query($db, $sql)) 
{
    echo '<script type="text/javascript">alert("New record created successfully");</script>';

    header("Location:http://localhost/Fly With US/index.php");
} 
else 
{
    echo '<script type="text/javascript">alert("Error Occured");</script>';
    header("Location:http://localhost/Fly With US/index.php");

    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

    }
?>

---------------------------------main page--------------------------------------

    <?php
session_start();
if(isset($_SESSION['username'])){
    header("Location: cat_type.php");
}
?>
 <html>
 <head>
<link rel="stylesheet" href="css/head.css">
<link rel="icon" href="res/flywithus-pt.png" sizes="16x16">
<link href="https://fonts.googleapis.com/css?family=Bgrree+Serif" rel="stylesheet">
<script type="text/javascript" src="scripts/signupForm.js"></script>
<script type="text/javascript" src="scripts/loginForm.js"></script>
<title>Fly With Us | Buy Drones and Computers.</title>
</head>
<body>
<h1><center><img src="res/flywithus-png.png"></center></h1>



<!-- LOGIN -->

<div id="login">
<form name="loginForm" method="post" action="log.php">
<table align="left" width="150%">
<tr>
    <td align="center">
        <h2>Login</h2>
    </td>
</tr>
<tr>    
    <td>
        <input type="text" name="username" id="user" placeholder="Email" required>
    </td>   
</tr>
<br>
<tr>    

    <td>
        <input type="password" name="password" id="pass" placeholder="Password" required>
    </td>
</tr>
<tr>

    <td>
        <input type="submit" name="login" value="Let me in.." class="click">
    </td>
</tr>
<!--
<tr>
    <td colspan="2" class="signin">
        <input type="image" src="signin_google.png" alt="Login with Google" width="50%" height="50%">

    </td>
</tr>
<tr>
    <td colspan="2" class="signin">
        <input type="image" src="signin_fb.png" alt="Login with FB" width="50%" height="50%">

    </td>
</tr>
//-->
</table>
</form>
</div>


<!-- SIGNUP -->

<form name="signupForm" method="post" action="pro.php" id="signup" onsubmit="return ValidateFname() || ValidateLname() || ValidateEmail() || ValidateMobile() ">
<table align="right"> 
<tr>
    <td align="center" colspan="2">
        <h2>New Here? Register with us..</h2>
    </td>
</tr>
<tr>
    <td>
        <input type="text" name="fname" placeholder="First Name" required>  
    </td>
    <td>
        <input type="text" name="lname" placeholder="Last Name" required>
    </td>
</tr>

<tr>
    <td colspan="2">
        <input type="email" name="email" placeholder="Email" required>
    </td>
</tr>
<tr>
    <td colspan="2">
        <input type="text" name="phone" placeholder="Contact Number" required>
    </td>
</tr>
<tr>    
    <td colspan="2">
        <input type="password" name="password" placeholder="Password" required>
    </td>
</tr>

<tr>
<td colspan="2">
    <input type="submit" name="signup" value="Sign Up" class="click">
</td>
</tr>

</table>
</form>

</body>
</html>

1 Answer 1

2

That's because PHP (header) will still read after JavaScript (alert). In other words, the alert is actually shown but the index.php is being load at the same time so the alert appears not showing.

won't work:

echo '<script type="text/javascript">alert("Error Occured");</script>';
header("Location:http://localhost/Fly With US/index.php");

will work:

echo '<script type="text/javascript">
    alert("Error Occured");
    window.open("index.php","_self");
</script>';
Sign up to request clarification or add additional context in comments.

3 Comments

Oh man thanks alot for your help! it helps and it work just change the two things the sucesseful and the error and it works!! thanks!
could you help me with something else?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.