-1

I trying to use an alert message when someone enters incorrect login details, and I then want to redirect them back to the same login page. I'm doing this inside a php file that is called when someone submits a login form. I'm trying something like this:

echo '<script language="javascript">';
echo 'alert("Invalid Username or Password")';
echo 'location.replace("target.php")';
echo '</script>';

but it goes to the php file itself, which shows nothing.

Qaisar Satti
  • 2,742
  • 2
  • 18
  • 36
tdrsam
  • 527
  • 1
  • 8
  • 28
  • Of course, it does. `location.replace` redirects a user to the page `target.php` and "goes to php file itself". What did you want it to do? – Yeldar Kurmangaliyev Jun 05 '15 at 03:45
  • Sorry. I wasn't very clear on that. It goes to the php file where the script that processes the submit button click is stored, rather than going back to the login page, which is also a php page. There's nothing in the page that it's redirecting to, other than the login processing script, so I get a blank page. My fault. – tdrsam Jun 06 '15 at 05:36

2 Answers2

2

update it you have missing ; for alert

echo '<script language="javascript">';
echo 'alert("Invalid Username or Password");';
echo 'window.location("target.php")';
echo '</script>';
Qaisar Satti
  • 2,742
  • 2
  • 18
  • 36
1
echo '<script>';
echo 'alert("Invalid Username or Password"); ';
echo 'location.replace("http://mywebsite.com/target.php"); ';
echo '</script>';

language attribute is deprecated

location.replace("target.php") must have valid url: location.replace("http://mywebsite.com/target.php") or you can use history.back()

and as noted, don't miss semicolon

Community
  • 1
  • 1
Medet Tleukabiluly
  • 11,662
  • 3
  • 34
  • 69