Ajax ignores POST method

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
johniem
Forum Commoner
Posts: 29
Joined: Mon Jul 20, 2009 8:58 am

Ajax ignores POST method

Post 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..
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Re: Ajax ignores POST method

Post by kaszu »

try adding "return false;" after $.ajax(...);
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Ajax ignores POST method

Post 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 ;)
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply