Why will my javascript no run from php?

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

Answers


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.


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.


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


Need Your Help

Redirect a page after download to update mysql table

php mysql href

I have a following code for download option:

Process `auto_orient` with Cloudinary upload

ruby-on-rails imagemagick carrierwave cloudinary minimagick

I had a regular file upload that I now changed to using Cloudinary.