Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Here is my index.html page code:-

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        </meta>
        <title>School</title>
        <script type="text/javascript" src="script/jquery-1.8.2.min.js"></script>
        <script type="text/javascript" src="my_script.js"></script>
        <link rel="stylesheet" type="text/css" href="style.css" />
    </head>
    <body>

        <div style="margin:auto; width:1000px;">

            <div class="flt topblock"> <a href="" class="flt1 tp_txtplay">Play School</a>
                <br/><br/>
                <div>

                    <form id='myform' method='post' action='welcome.php' >
                        <span class="flt1 lp_txtlog">Email ID</span>
                        <input name="username" type="text" id="username" class="flt lp_textbox" />
                        <br />
                        <span class="flt1 lp_txtlog2">Password</span>

                        <input name="password" type="password" id="password" class="flt lp_textbox2" />
                        button id="submit" class='flt lp_button'>Login</button>
                    </form>
                    <div id="ack"></div>
                </div>
            </div>
        </div>
    </body>
</html>

Here is my_script.js code :-

$("button#submit").click(function() {
    if ($("#username").val() == "" || $("#password").val() == "")
        $("div#ack").html("Please enter both username and password");
    else
        $.post($("#myForm").attr("action"),
                $("#myForm :input").serializeArray(),
                function(data) {
                    $("div#ack").html(data);
                });

    $("#myForm").submit(function() {
        return false;
    });

});

and below is my welcome.php code :-

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
        <?php
        $con = mysql_connect('localhost', 'root', 'Wahegurug@9');
        mysql_select_db('test', $con);
        if (!$con) {
            die('Could not connect: ' . mysql_error());
        }

        $sql = "SELECT * FROM user ";

        $res = mysql_query($sql);
        if ($row = mysql_fetch_array($res)) {
            if ($row['Username'] == $_POST['username']) {
                if ($row['Password'] == $_POST['password']) {
                    echo'login successful';
                } else {
                    echo'login failed';
                }
            }
        }
        ?>
    </body>
</html>

Details are :- I have made this project in Netbeans 7.3 and using xampp server. I have created a simple login page and am trying to use javascript to prevent the page submitting if the wrong credentials are entered.

In my_script.js I'm using a div with id ack to show the user a success or error message.

My problem is that the user is still being redirected to welcome.php (my form's target) even if the wrong credentials are entered. How can I prevent this from happening?

share|improve this question
Hi Orangepill,I tried the below solution mentioned by you but still getting the same behaviour.Even if I leave the values blak the user gets routed to welcome.php page.So really dont know why its happening. – user2214872 34 mins ago

2 Answers

What you need to do is prevent the default action.

Change

$("button#submit").click( function() {

to

$("button#submit").click( function(event) {
    event.preventDefault();

Also you are explicitly calling the submit function here:

$("#myForm").submit(function() {
    return false;
});

Try removing this code as well, it is likely resolving prior to the $.post call finishes

share|improve this answer
Sorry but this did nt work – user2214872 12 mins ago
noticed something else ... updated the answer – Orangepill 5 mins ago

you may try like this .

$("button#submit").click( function(e) {
    e.preventDefault();//Will prevent the default event of click


} 
share|improve this answer
Sorry but this also did nt work.I tried but got the same results.If you want any additional information also please let me know but I really want to correct this. – user2214872 13 mins ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.