slwr
slwr

Reputation: 1175

jquery, AJAX form submit not working. Page crashes

I'm trying to get a submit form without reloading the page.

Here's the script:

$(function() {
    $("#submit").click(function() {
        var email = document.getElementById('emailaddress');
        var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        if (!filter.test(email.value)) {
            $('#invalidmail').show();
            var t=setTimeout(function(){$('#invalidmail').fadeOut("slow");},2000);
            email.focus;
            return false;
        } else {    
            $.ajax({
                type: "POST",
                url: "traces_form_handler.cgi",
                data: email,
                success: function(){
                    alert("success!");
                }
            });
            alert("success!");
            return false;
        };

      });  
});
</script>

and here's the html

<form name="form" method="post">
    Email: <input type="text" name="email" id="emailaddress" />
    <input type="submit" value="Submit" id="submit"/>
    </form>        
   <p id="invalidmail">Ops! The address you provided is not valid. Please retry.</p>

it works till the invalid mail part but if i submit a valid email address, the browser crashes.

Any suggestion?

Thanks

Upvotes: 0

Views: 1634

Answers (3)

The Alpha
The Alpha

Reputation: 146269

You can use form submit event, it;s better

HTML

<form id="myForm" name="form" method="post">
  Email: <input type="text" name="email" id="emailaddress" />
  <input type="submit" value="Submit" id="submit"/>
</form> 
<p id="invalidmail">Ops! The address you provided is not valid. Please retry.</p>

JS

$(function() {
​$("#myForm").on('submit', function(e){
e.preventDefault();
var email=$("#emailaddress");
var filter = /^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$/;
    if (!filter.test(email.value)) {
        $('#invalidmail').show();
        var t=setTimeout(function(){
            $('#invalidmail').fadeOut("slow");},2000);
        email.focus;
        return false;
    }
    else 
    {    
        $.ajax({
            type: "POST",
            url: "my_server-page.php",
            data: email.val(),
            success: function(data){
                alert("success!");
            }
        });
    }

});​​
});

Upvotes: 0

Rizvi
Rizvi

Reputation: 1

This attributes is causing problem

url: "traces_form_handler.cgi",

I have it tested on my browser FF 10.0.2

On Error console error is : Error: uncaught exception: [Exception... "Could not convert JavaScript argument" nsresult: "0x80570009 (NS_ERROR_XPC_BAD_CONVERT_JS) :: line 7601" data: no]

Solution: url:"some_valid_url_.php"

Upvotes: 0

ShankarSangoli
ShankarSangoli

Reputation: 69915

I don't know whether that will crash the browser. But you are sending a dom element as data to the ajax request where I guess you want to just send its value.

        $.ajax({
            type: "POST",
            url: "traces_form_handler.cgi",
            data: { email: email.value },//Check here in your code
            success: function(){
                alert("success!");
            }
        });

Upvotes: 2

Related Questions