Page 1 of 1

Ajax ignores POST method

Posted: Fri Nov 06, 2009 4:02 am
by johniem
I'm using AJAX to post a form to a php script for a login page i'm trying to create. The problem is that AJAX call ignores the method that i want to use for the request and uses the default html form attribute which is empty(so GET is choosed by default). So i'm just getting a refresh page effect with wrong GET values on the url (like that "www.mydomain.com/?user_name=&user_pwd=")

If i remove the form tag from html the AJAX request is working fine, but then i have problem with my html document.

Code: Select all

 
<script type="text/javascript">
    $(document).ready(function(){
 
        $("button").click(function(){
 
            $.ajax({
                url:"validate_scripts/validate_login.php",
                data:$("form").serialize(),
                type:"post",
                success:function(response,status){
 
                    alert(response+":"+status);
                }
            });     
    
        });
 
    });
    
    </script>
 

Code: Select all

 
<form id="login_frm" action="">
    <table>
    <caption>Insert username and password</caption>
    <tr>
    <td>Username</td>
    <td><input type="text" name="user_name" id="user_name"/></td>
    </tr>
    <tr>
    <td>Password</td>
    <td><input type="password" name="user_pwd" id="user_pwd" /></td>
    </tr>
    <tr>
    <td>Keep my identity <input type="checkbox" name="set_cookie" id="set_cookie" /></td>
    <td id="loading"></td>
    </tr>
    <tr>
    <td><button>Login</button></td>
    </tr>
    </table>
    </form>
 
Any help on this whould be appreciated..

Thanks in advance..

Re: Ajax ignores POST method

Posted: Fri Nov 06, 2009 12:46 pm
by kaszu
try adding "return false;" after $.ajax(...);

Re: Ajax ignores POST method

Posted: Fri Nov 06, 2009 2:05 pm
by pickle
It's good to know that whenever there's a Javascript problem that I can easily provide the answer too, ~kaszu has likely already done so ;)