31

I want to make AJAX call when a submit button in the form pressed. InFact I cant remove the <form> because I want to made clientside validation also. I tried this code.

<form name="search" >
Name: <input type="text" name="name1"/>
Age: <input type="text" name="age1"/>
<input type="submit" name="Submit" value="Submit" onclick="makeSearch()"/>
</form>

JS

function makeSearch(){
alert("Code to make AJAX Call");
}

After using this code alert() not showing but the page is reloaded. I want to block the page reload and call the JS function.

Thanks

Sridhar
  • 2,228
  • 10
  • 48
  • 79

6 Answers6

52

Add the onsubmit attribute to the form tag:

<form name="search" onsubmit="return makeSearch()" >
  Name: <input type="text" name="name1"/>
  Age: <input type="text" name="age1"/>
  <input type="submit" name="Submit" value="Submit"/>
</form>

And javascript add return false at the end:

function makeSearch() {
  alert("Code to make AJAX Call");
  return false;
}
Jens
  • 8,423
  • 9
  • 58
  • 78
Miqdad Ali
  • 6,129
  • 7
  • 31
  • 50
32

The correct, jQuery way would be:

$("form").on('submit', function (e) {
   //ajax call here

   //stop form submission
   e.preventDefault();
});

Like you said, you could also remove the <form> element and just bind the ajax call to the button's click event.

Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
  • Great answer. IMO it's much better to keep the all the JS separated from the HTML markup. Here's a pen with a working example: http://codepen.io/martinkrulltott/pen/yNZJbp – Martin Aug 05 '15 at 02:00
1

use jQuery.post, and change submit button.

Submit button is create for send data to server via POST (native method, not ajax), I suggest using it only in special cases, for example when uploading a file.

If you continue use submit button for ajax request you will have many problems with IE.

    <html>
    <head>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
        <script language="javascript">
            function makeSearch()
            {
                if(validateIdata())
                {
                    alert("send ajax request");
                    return;
                    $.ajax({    
                      type: 'POST',
                      url: url, //- action form
                      data: {name1:$('#name1').val(),age1:$('#age1').val()},
                      success: function(){
                        alert('success');
                      }
                    });
                }
            }

            function validateIdata()
            {
                if($('#name1').val() =='')
                {
                    alert("Invalid Name");
                    return false;
                }

                if($('#age1').val() =='')
                {
                    alert("Invalid Age");
                    return false;
                }

                return true;
            }
        </script>
    </head>
    <body>
        <form name="search" >
        Name: <input type="text" id="name1" name="name1"/>
        Age: <input type="text" id="age1" name="age1"/>
        <input type="button" name="Submit" value="Submit" onclick="makeSearch()"/>
    </form>
    </body>
    </html>
greg
  • 107
  • 1
  • 4
1
<form name="search" >
Name: <input type="text" name="name1"/>
Age: <input type="text" name="age1"/>
<input type="submit" name="Submit" value="Submit" onclick="return makeSearch();"/>
</form>

function makeSearch(){
alert("Code to make AJAX Call");
}

just use return in onclick function it will do good for u

Bkay
  • 177
  • 2
  • 4
  • 16
0
document.getElementById("bt1").type='submit';
                    document.getElementById("bt1").onclick="";
                    var elem = document.getElementById("bt1");
                    if (typeof elem.onclick == "function") {
                        elem.onclick.apply(elem);
                    }


<form id="org" action="">
                        <fieldset class="log">
                            <legend>Log in</legend>
                            <label for="uname">Username</label>
                            <input type="text" name="uname" id="uname" value="" required/><br />
                            <label for="pword">Password</label>
                            <input type="password" name="pword" id="pword" value="" required/><br />
                            <input type="button" onclick="organizer()" value="LOG IN" class="red-btn" id="bt1"/>
                        </fieldset>
                    </form>
Rahul Akula
  • 135
  • 1
  • 10
-3

Finally I got the answer

$("form").live("submit", function() {
        makeSearch();
        return false;
    });
Sridhar
  • 2,228
  • 10
  • 48
  • 79
  • That's not progressive :: if js is disabled it will submit. Please see the link I provided in the first comment. – Edward J Beckett Jan 23 '13 at 04:47
  • If I removed the submit button, clientside validation will not execute. so I am not supposed to removed that. And I use the above code in my project It is perfectly working...... – Sridhar Jan 23 '13 at 05:30
  • You don't need to have a submit button for validation ... you can roll your own validation ref: [Custom Validation](http://stackoverflow.com/questions/4751780/best-javascript-solution-for-client-side-form-validation-and-interaction/4752092#4752092) – Edward J Beckett Jan 23 '13 at 06:55
  • `.live` is bad, and also: http://fuelyourcoding.com/jquery-events-stop-misusing-return-false/ – Explosion Pills Jan 23 '13 at 14:08