1

I have a form that submits using POST and I want to covert it to an ajax submission of the form. When I use the .submit function with the .ajax function inside when I serialize the data fom the form is it going to be the same as just doing a POST

Some code:

<script type="text/javascript">
    $("#optionsForm").submit(function(){
            var data= $("#optionsForm").serialize();
            $.ajax({
                    url: 'filter.php',
                    type: 'post',
                    data: data,
                    success: function(data){
                            alert(data);
                    }
            });
            return false;
    });
</script>

<?php
$html = "";
$html .= "<div id='options'>";
$html .= "<form id='optionsForm' method='post'>";
foreach($selectValues as $key => $value){
    $title=new MODEL\String($key);
    $html.= "<fieldset class='optionsBox'>";
    $html.= "<legend>".$title->friendlify()."</legend><br/>";
    foreach($value as $option){
            $html .= "<input type='checkbox' name='".$key."[]' value='$option'>".htmlspecialchars($option)."</input>";
    }
    $html.= "</fieldset>";
}
 $html .= "</select><input type='submit' value='submit'></form></div>";
 $html .= "<div id='tables'></div>";

Where I retrieve the data:

if(!empty($_POST)){
    $filterValue=$_POST;
}
else{
    $filterValue="y";
}
SolutionYogi
  • 31,807
  • 12
  • 70
  • 78
waa1990
  • 2,365
  • 9
  • 27
  • 33
  • you can see what your posting using the console tab in firebug. just check view xml requests/errors. – Rooster Dec 16 '11 at 19:29
  • 0.o .... English please – Mike Robinson Dec 16 '11 at 19:29
  • possible duplicate of [what is the variable name that stores a POST when done through jquery .ajax?](http://stackoverflow.com/questions/8538931/what-is-the-variable-name-that-stores-a-post-when-done-through-jquery-ajax) – mario Dec 16 '11 at 19:30

2 Answers2

0

using .submit(function) just binds the ajax call to the submit, so you're probably leaving the page before the submit. You need to bind the ajax call to a click event on the submit button (as an example) and prevent the default submit.

Evan Davis
  • 35,493
  • 6
  • 50
  • 57
0

Yes, all form variables are serialized and sent in a POST request just like a form action="post" would submit. Nothing to change on the back end except what output is passed from the form processing script, and how JavaScript should handle that output (i.e. Success, or Missing Field X)

Patrick Moore
  • 13,251
  • 5
  • 38
  • 63