1

I am pretty new to PHP, HTML, jQuery and I need some help. I have a php script that I need to activate when I click the submit button. I have it set up to set my session to my username and id. All I really need is a way to update or append a p tag to my div if the information entered is the same as the database.

Here is my code:

<div class="modal-footer">
 <input type="submit" name="sumbit" value="Login" class="btn btn-primary" />
    <?php
        if (isset($_POST["username"])) {
            $username = $_POST["username"];
            $password = $_POST["password"];
            $sql = "select * from users where username = '$username' and password = '$password' limit 1";
            $res = mysql_query($sql) or die(mysql_error());
            if (mysql_num_rows($res)) {
                $row = mysql_fetch_assoc($res);
                $_SESSION["uid"] = $row["id"];
                $_SESSION["username"] = $row["username"];
                header("Location: index.php");
            } else {
                ?>
                    //I want to add the text to my modal body above this code
                <?php
            }
        }
    ?>
</div>

Also if you haven't notice already I am using bootstrap.

My main problem is I don't know how to run the PHP to check if the info is correct or not and not having the modal close if clicked if it is.

Laurel
  • 5,965
  • 14
  • 31
  • 57
  • use an xmlhttprequest instead – Kevin Sep 05 '16 at 00:42
  • You should [stop using mysql_ functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php), as they have been removed from PHP. You should also use [password_hash](http://php.net/manual/en/function.password-hash.php) to secure your passwords. Finally, [your code is open to SQL injection](http://stackoverflow.com/documentation/php/275/using-a-database/2685/preventing-sql-injection-with-parametrized-queries) – Machavity Sep 05 '16 at 01:17

1 Answers1

1

What you'd need here is an AJAX request that fetches all this information for you.

Something like this:

HTML

<div class="modal-footer">
   <input type="submit" name="sumbit" value="Login" class="btn btn-primary" />
</div>

Jquery

$("input[name='submit']").click(function(e){
    e.preventDefault(); //to prevent form from submitting itself
    $.post('phpcode.php', $("#form_id").serialize, function(response){
        if(response.status)
        {
             location.reload('index.php');
        }
        else
        {
            $(".modal-footer").html(response.message);
        }
    });
});

PHP (phpcode.php)

 if (isset($_POST["username"])) {
        $username = $_POST["username"];
        $password = $_POST["password"];
        $sql = "select * from users where username = '$username' and password = '$password' limit 1";
        $res = mysql_query($sql) or die(mysql_error());
        if (mysql_num_rows($res)) {
            $row = mysql_fetch_assoc($res);
            $_SESSION["uid"] = $row["id"];
            $_SESSION["username"] = $row["username"];
            return json_encode(array('status' => true));
        } else {
            return json_encode(array('status' => false, 'message' => 'Error Message or whatever you\'d like to show'));
        }
    }

I have assumed quite a few things here. Feel free to get back if there's something you didn't understand.

Akshay Khetrapal
  • 2,586
  • 5
  • 22
  • 38