views:

142

answers:

4

I want to post the Form but don't want to use the Submit method. If I use JQuery, how to handle the Form input controls?

+1  A: 

I am not sure if I understood the question correctly.

If you don't want to use submit(), you can do the same thing via jQuery.post() using Ajax. The main difference is you have to construct the key value data from the input fields yourself rather than the browser doing it automatically and you won't get a page refresh.

Yacoby
Can u please illustrate jquery post?
RPK
The link I gave is full of examples, which makes me posting an example kinda pointless.
Yacoby
+2  A: 

Just create a function that is triggered by whatever event you want, for example: (found this code in another question)

function example() {
// get all the inputs into an array.
    var $inputs = $('#myForm :input');

    // not sure if you wanted this, but I thought I'd add it.
    // get an associative array of just the values.
    var values = {};
    $inputs.each(function() {
        values[this.name] = $(this).val();
    });
}

After that you can do whatever you want with the input values. You might want to consider using more advanced processing though, there are plenty of plugins that can provide this kind of functionality.

Jasperdc
+2  A: 

You can use the jQuery AJAX .post function functions. An example (untested, but should be working):

<script>
function postit(obj) {
  var data = $(obj).serialize();
  $.post($(obj).attr("action"), data, function() {
    //Put callback functionality here, to be run when the form is submitted.
  });
}
</script>
<form action="posthandler.php" onsubmit="postit(this); return false;">
<input type="text" name="field">
<input type="submit">
</form>

Also, read about serialize

(Of course you need to include the jQuery library in your code before using this code).

Emil Vikström
+1  A: 

Either Post function or Load function will work.

@PRK are you trying to post the Form when the page loads or when a user hit a button?

load(url, parameters, callback)

eg:

$("#loadItHere").load("some.php", {somedata: 1});
nolabel