0

I have a login form on my page index.php:

    <form name="login" id="login" action="include/login.php" method="post" onsubmit="return validateForm()"> 
    <h21>Username</h21><br/>
    <input type="text" class="login_form_front" id="myusername" name="myusername"><br/>
    <h21>Password</h21><br/>
    <input type="password" class="login_form_front" autocomplete="off"  id="mypassword" name="mypassword">
    <br/>
    <input type="submit" class="buttons_login" value="Login" id="login" name="login">
    </form> 


<?php if (isset($_SESSION['failed_login'])) {
*run my jquery code*
unset($_SESSION['failed_login']); } ?>

this then runs my script login.php:

$sql = "select * from $tbl_name where user_name = '$myusername' AND password = '$mypassword";
$result = mysql_query($sql) or die( mysql_error() );
$row = mysql_fetch_assoc($result);

if(!$row) {
$_SESSION['failed_login'] = 'my session';  
header("location:../index.php");  

}else{

include 'dashboard.php';

what I want to do is if their is no result, i.e. the username and password do not match then return back to my index page and set a session.

Then If the session is set I want to run a piece of jquery on my index page. Is this possible?

Kevin Parks
  • 95
  • 2
  • 11
  • 1
    Yes it's possible. But you don't want to check username and password with Jquery alone. Because if somebody turns javascript off, they can login without legit username/password. – Refilon Mar 02 '15 at 10:46
  • Yes of course you can refer this [question](http://stackoverflow.com/questions/3057317/run-a-javascript-function-from-a-php-if-statement) it might helps you. – Rikesh Mar 02 '15 at 10:46

2 Answers2

0

Of course it is possible. It's called a 'flash session' in most terminologies, a session which lasts for one server request (aka redirection to the same page), and then destroyed.

This is the way to do it, keep it up. :)

Shay
  • 2,060
  • 2
  • 16
  • 22
0

You need to use session_start(); to make sure a session is started (as soon as possible in your script, probably that would be beginning of index.php). Then to run the jQuery code you need to do something like this:

<?php if (isset($_SESSION['failed_login'])) {
  unset($_SESSION['failed_login']);?>
  <script>
    $(function() {
    // Do whatever you need to do with jQuery here
    });
  </script>
<?php }?>
silviup
  • 56
  • 4