Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to show one alert saying wrong username and password and then have to redirect to the same page(page1.php"login Form page"). But its not showing. simply it redirects.If i take header function na its showing alert. so help me to find a way
PHP
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row

if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword"); 
header("location:page2.php");
}
else {
echo "<script type='text/javascript'>alert(\"Wrong Username or Password\")</script>";
header("location:page1.php");
}
?>
Posted
Updated 14-Feb-18 19:50pm

You can't mix a header-based redirect with page content. The header is telling the browser to skip displaying a page and go straight to another page.

Since you are using Javascript to display the alert, why not use it to do the redirect too:
PHP
<script type="text/javascript">
alert("Wrong Username or Password");
location="page1.php";
</script>
 
Share this answer
 
Comments
Uday P.Singh 9-Mar-12 11:52am    
Agree 5!
Ashokraj29 9-Mar-12 11:58am    
Parse error: syntax error, unexpected '=' in C:\wamp\www\login.php on line 26
this is the error when i put location="page1.php";
if i put inside script its also showing error..
can u put ur code in my else condition and explain. I'm new to php
Graham Breach 9-Mar-12 12:28pm    
The error is probably because I used double quotes in my answer, and you used double quotes to quote the Javascript in your echo command. Uday's answer shows how to do it more clearly by using single quotes inside the double quotes.
Ashokraj29 9-Mar-12 12:37pm    
Thanks Graham. I made one mistake i just put location=\"page1.php\"; forgot to put windows.location.but its taking 1.5 sec to redirect from login.php
You can try this:

PHP
else {
echo "<script type='text/javascript'>alert('Wrong Username or Password');
window.location='page1.php';
</script>";
}


hope it helps :)
 
Share this answer
 
Comments
Ashokraj29 9-Mar-12 12:36pm    
Hey thanks. Its working but one small minus. Its taking 1.5sec to load from login.php to page1.php after showing alert.
Dilan Kaushalya 4-Dec-15 4:31am    
Nice. It is working. Thank you.
Member 14213075 4-Apr-19 6:30am    
you save our lives thanks :D
This is working fine. I checked it. If u guys have any other way na pls inform me.
PHP
else {
echo "<script type='text/javascript'>alert(\"Wrong Username or Password\")</script>";
//header("location:page1.php");
include("page1.php");
}
 
Share this answer
 
Comments
Uday P.Singh 9-Mar-12 12:06pm    
but this is the not right way to do it see my answer.
Ashokraj29 9-Mar-12 13:16pm    
i checked it with ur answer there is delay in moving from login.php tp page1.php
How i can customized alert box E.g: colors,font style , bgcolor...

echo "alert(\"Wrong Username or Password\")";
//header("location:page1.php");
include("page1.php");
 
Share this answer
 
Comments
CHill60 2-Mar-17 4:47am    
If you have a question of your own then use the  Ask a Question  link. Do not post questions as solutions to old posts
<?php
header("Refresh: 5; url= index.php");
echo 'Successfully redirecting after 5 seconds.';
?>
 
Share this answer
 
Comments
CHill60 15-Feb-18 5:04am    
The question is over 5 years old and already has accepted solutions that work. This is not a good solution.
Stick to answering new posts where the OP still needs help

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900