1

I am making a basic login web application and I am using the echo function in php to run some javascript to alert the user they got their username/password incorrect, for ease of doing something instead of just going to the main index.php file with no text in the form box's. For some reason my alert doesn't come up but I get redirected to my index.php. I don't know why this is happening?

/ / / This is the code that only half of it is running / / /

echo 
    '<script type="text/javascript">'
    ,'alert("a");'
    ,'</script>'
;
header("Location: /");

?>

Can someone please tell me if I have any syntax errors or any errors at all?

Thank you in advance -mjacob652


index.php's body

<div id="header">
<h1> Welcome to S.O.G <br/> Specials Operations Gaming </h1>
</div>
<div id="wrapper">
<h2> Please Log In </h2>
<form id="form" action="/Account/php/login.php" method="post" enctype="multipart/form-data">
    Username: <input type="text" name="username"/> <br/>
    Password: <input type="password" name="password"/> <br/>
    <input id="submit" type="submit" value="Login" name="submit"/>
</form>

login.php just has the sql stuff and the echo and header

  • 3
    if you change location, browser will redirect before script will be executed. – jcubic Mar 14 '16 at 09:54
  • Your header will not work after this echo statement. What are you trying to do? Javascript and php are two different languages, so php has nothing todo with your javascript, which is by the way a valid statement for alert. – Muhammed Mar 14 '16 at 09:55
  • @MuhammedM. header will work if output_buffering is on, which is by default http://stackoverflow.com/questions/34633002/why-i-dont-get-headers-already-sent-error – jcubic Mar 14 '16 at 09:57
  • @MuhammedM. I'm trying to alert the logging in user that his username or password is incorrect –  Mar 14 '16 at 10:01
  • No you are echoing some javascript to the current page, and then redirecting to a completly different page. Major waste of time – RiggsFolly Mar 14 '16 at 10:03
  • @jcubic How might I go about alerting the user his credentials are incorrect and go back to the index.php file –  Mar 14 '16 at 10:03
  • @RiggsFolly same question for you –  Mar 14 '16 at 10:05
  • Do you have a `login.php` and an `index.php` or is this all happening in one script? – RiggsFolly Mar 14 '16 at 10:07
  • why r u using `header("Location: /");` if you want to use `alert()` – devpro Mar 14 '16 at 10:08
  • a index.php with the form and when you submit the form it calls the login.php –  Mar 14 '16 at 10:08
  • Show us more of your actual code, then we can make sensible suggestions rather than guesses – RiggsFolly Mar 14 '16 at 10:10
  • @jcubic outbut buferring is not on by default in PHP. What he is trying to accomplish, doesnt have to do with ob_, he shouldnt turn it on just to do this echo statement. – Muhammed Mar 14 '16 at 10:12

3 Answers3

1

As a simple and quick workaround:

echo 
    '<script type="text/javascript">'
    ,'alert("a"); window.location.href = "/";'
    ,'</script>';

In this way user will be redirect right after alert dialog popup will be closed.

oleh.meleshko
  • 4,675
  • 2
  • 27
  • 40
  • 1
    Thank you so much after further review i noticed it was a totally different problem makeing nothing appear, thanks a lot –  Mar 14 '16 at 10:25
0

Why will my javascript no run from php?

Imagine that your are in a php script that will redirect to itself everytime it loads.

Chrome will give you

redirected you too many times. ERR_TOO_MANY_REDIRECTS

while in Mozilla

Firefox has detected that the server is redirecting the request for this address in a way that will never complete.


Can someone please tell me if I have any syntax errors or any errors at all?

Of Course you dont have any syntax error, it just the redirection is infinite in your current code.

jameshwart lopez
  • 2,993
  • 6
  • 35
  • 65
  • No it only redirects to the login.php when you submit the form –  Mar 14 '16 at 10:22
  • @mjacob652 when your form is submitted to and it goes to your login.php and had alert then after that alert, php will execute `header("Location: /");` redirecting to login.php itself. that would become infinite. – jameshwart lopez Mar 14 '16 at 10:26
0

Every thing in php Code will run first because we request to server so server will translate server code first so it will can result back. but server will translate echo and then meet header so it will redirect without echo anything

feeling
  • 25
  • 7