Jump to content

alert() (or someting similar) Called from PHP


OtagoHarbour

Recommended Posts

I am setting up a log-in form that calls a PHP file. The form (in the html file) is as follows.<form action="login.php" method="post"> User name: <input type="text" name="username" /> Password: <input type="password" name="pwd" /><br /> <input type="submit" value="Submit" /><br /></form>If the user enters the wrong password, I would like a message box to come up saying the password is invalid and for the page with the form to reappear. So far, I have been unable to get the message box to come up. I tried the following.<?php $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } else echo "Connected to MySql Database<br \>"; mysql_select_db("DraculaDB", $con); $result = mysql_query("SELECT * FROM SubScribers"); $row = mysql_fetch_array($result); echo "Password entered: " . sha1($_POST["pwd"]) . "<br \>"; echo "Password required: " . $row['PassWord'] . "<br \>"; if (sha1($_POST["pwd"])==$row['PassWord']) { echo "Valid password"; } else { <script type="text/javascript">alert('Invalid password');</script>; } mysql_close($con);?>But the<script type="text/javascript">alert('Invalid password');</script>;part gives me this error message.Parse error: syntax error, unexpected '<'Many thank in advance for any assistance in resolving this problem,Peter.

Link to comment
Share on other sites

Either enclose the script tags in an echo statement or remove it from the PHP code by closing the PHP tag and re-opening it:echo '<script type="text/javascript">alert("Invalid password");</script>';or?><script type="text/javascript">alert('Invalid password');</script><?phpEDIT: Fixed mismatched quotes in the first solution.

Link to comment
Share on other sites

Either enclose the script tags in an echo statement or remove it from the PHP code by closing the PHP tag and re-opening it:echo '<script type="text/javascript">alert("Invalid password");</script>';or?><script type="text/javascript">alert('Invalid password');</script><?phpEDIT: Fixed mismatched quotes in the first solution.
They both worked great!Thanks very much,Peter.
Link to comment
Share on other sites

Either enclose the script tags in an echo statement or remove it from the PHP code by closing the PHP tag and re-opening it:echo '<script type="text/javascript">alert("Invalid password");</script>';or?><script type="text/javascript">alert('Invalid password');</script><?phpEDIT: Fixed mismatched quotes in the first solution.
This is a tangential question but related to the code I quoted. I would like the code to print an alert message, and then return to the originating page, if the wrong password is used. I tried this. if (sha1($_POST["pwd"])==$row['PassWord']) { echo "Valid password"; } else { '<script type="text/javascript">alert("Invalid password");</script>'; header("Location: http://localhost/PageWithFormOnIt.html"); exit; }It returned to the form page but did not give me an error message. On the surface it seemed like I clicked on the submit button, for the form, and nothing happened. Is there a way to attach a message when redirecting to an html page and/or is there a way to query where the html page was linked to from?Thanks again,Peter.
Link to comment
Share on other sites

It appears you forgot to echo the <script> tag in your else block.
That's true. I did. Thanks. However correcting that error did not fix the problem. Here is the new code. if (sha1($_POST["pwd"])==$row['PassWord']) { echo "Valid password"; } else { echo '<script type="text/javascript">alert("Invalid password");</script>'; header("Location: http://localhost/Dracula.html"); }I still do not get the alert dialog box.Thanks,Peter.
Link to comment
Share on other sites

You are using PHP to redirect. I'm guessing that redirects you before any output is sent to the browser. Therefore your script is never being executed. (Perhaps a more experienced member can comment on this? Either confirm or correct it.)Try replacing the PHP header redirect with a JavaScript redirect. You can use the location object to do that.

Link to comment
Share on other sites

You are using PHP to redirect. I'm guessing that redirects you before any output is sent to the browser. Therefore your script is never being executed. (Perhaps a more experienced member can comment on this? Either confirm or correct it.)Try replacing the PHP header redirect with a JavaScript redirect. You can use the location object to do that.
That worked! I used the following code. ?> <script type="text/javascript"> alert("Invalid password"); document.location.href="http://localhost/Dracula.html"; </script> <?phpThank you so much again,Peter.
Link to comment
Share on other sites

That worked! I used the following code. ?> <script type="text/javascript"> alert("Invalid password"); document.location.href="http://localhost/Dracula.html"; </script> <?phpThank you so much again,Peter.
I like this but how could this be used let's say I want when member logs in..it shows " New Entry= blah blah blah " in other words lets say someone added a new data entry and the next person is alerted when he/she logs in..????
Link to comment
Share on other sites

you would probably want to save timestamps when people log out and whenever any activity occurs in the database (all CREATE/INSERT/UPDATE/DELETE/etc). To let them know changes have been made since their last login, look in the database in whatever relevant tables, and search through the timestamps of all records and see the if the last time they had activity associated with them is newer then the last time they logged out.

Link to comment
Share on other sites

you would probably want to save timestamps when people log out and whenever any activity occurs in the database (all CREATE/INSERT/UPDATE/DELETE/etc). To let them know changes have been made since their last login, look in the database in whatever relevant tables, and search through the timestamps of all records and see the if the last time they had activity associated with them is newer then the last time they logged out.
thanks..will look into that..nice info
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...