ahmedsaber111
ahmedsaber111

Reputation: 822

pass form data via jquery post

I have a form and what i need to do is to pass this form data to another page via jquery .post and below you will find the javascript

    <script type="text/javascript">
$("form").submit(function(){
    $(".application_container").append('<div class="preloading" style="padding-top:250px;"><img src="<?php echo base_url() . "assets/images/loading.gif";?>" alt="تحميل" /></div>').show();
    var str = $("form").serialize();
    var url = "<?php echo base_url() . "hawaa/confirm_applicant.html"; ?>";
    $.post(url, function(str){
        $(".application_container").append(str).show();
        $(".preloading").remove();
    });
    return false;
});
   </script> 

the error that its request the another page but without sending the form submitted data to it, so it displays the validation error

Upvotes: 1

Views: 524

Answers (2)

Evan Davis
Evan Davis

Reputation: 36612

You are using $.post() incorrectly. You need to pass your data str as the data parameter, like so:

$.post(url, str, function(data, status, jqXHR) {
    // do stuff with response data here
});

Upvotes: 4

fsenart
fsenart

Reputation: 5901

You have to pass the str variable as the second parameter to the $.post call. The documentation states that:

  • The first parameter is the URL to which you want to post the data
  • The second parameter is the data you want to post
  • The third parameter is the results from requesting URL (HTML or XML, depending on what was returned).

So the correct way is:

<script type="text/javascript">
  $("form").submit(function(){
    $(".application_container").append('<div class="preloading" style="padding-top:250px;"><img src="<?php echo base_url() . "assets/images/loading.gif";?>" alt="تحميل" /></div>').show();
    var str = $("form").serialize();
    var url = "<?php echo base_url() . "hawaa/confirm_applicant.html"; ?>";
    $.post(url, str, function(result){
      $(".application_container").append(result).show();
      $(".preloading").remove();
    });
    return false;
  });
</script>

Upvotes: 3

Related Questions