tags:

views:

925

answers:

2

Hi,

$.ajax({
    method: "post"
    , url: "save.php"
    , data: "id=453&action=test" 
    , beforeSend: function(){

    } 
    , complete: function(){ 
    }  
    , success: function(html){ 
        $("#mydiv").append(html);        
    }
});

I have set method type as post but in Save.php I just get values either in $_GET or $_REQUEST but not in $_POST.

My form looks like

<form method="post" id="myform" action="save.php">

It was not working, looked around here and on Google, tried adding enctype

<form method="post" id="myform" action="save.php" enctype="application/x-www-form-urlencoded">

but still $_POST empty?

How do I make it working? Thanks.

+2  A: 

Instead of method: "post" you need to use type: "POST"

So this should work without any alterations to your form HTML:

$.ajax({
    type: "POST"
    , url: "save.php"
    , data: "id=453&action=test" 
    , beforeSend: function(){

    } 
    , complete: function(){ 
    }  
    , success: function(html){ 
        $("#mydiv").append(html);        
    }
});

Not sure why it doesn't work but this works for me:

save.php

<?php
print_r($_POST);

file.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <title>Example</title>

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"&gt;&lt;/script&gt;
    <script type="text/javascript">
     $(function() {
      $('input').click(function() {
       $.ajax({
        type: "POST"
        , url: "save.php"
        , data: "id=453&action=test" 
        , beforeSend: function(){

        } 
        , complete: function(){ 
        }  
        , success: function(html){ 
         alert(html);        
        }
       });
      });
     });
    </script>
</head>

<body>
    <div id="main"><input type="button" value="Click me"></div>
</body>
</html>

Your error must lie somewhere else.

Sbm007
nope .. doesnt work.
Wbdvlpr
@Wbdvlpr: open firebug console and check requests being sent. You can see what method it uses (get/post) and what parameters being sent to which url. Then go from there.
serg
But I get values in $_GET and $_REQUEST .. but not in the $_POST .. any idea how to check if "somewhere else" is wrong??
Wbdvlpr
You aren't doing cross domain requests, are you?
serg
btw there is a good plugin that automatically gets all fields values from the form and submits it via ajax: http://jquery.malsup.com/form/#ajaxForm
serg
no its on the same domain .. one thing I haven't mentioned is this form is in a dialog window
Wbdvlpr
@serg555: thanks for the link .. everything works fine except this POST so I dont think I need to use that plugin.
Wbdvlpr
I don't understand though how this problem is related to a form. Where your ajax is being called? Maybe it is not called at all and you are just submitting a form itself instead of doing ajax call?
serg
@serg555 .. checked in firebug console .. when I click the submit button it sends a GET request. Not sure why it does so.
Wbdvlpr
Are you sure that it is ajax call that sending this request, not form itself? Try it without a form, just use a link or a button like in this answer. Maybe your ajax is not being called at all.
serg
@serg55 - just tried $.post it works!! But .ajax still not.
Wbdvlpr
In the above example, the values are hardcoded but I am fetching values from FORM using serialize method.
Wbdvlpr
Yes I am sure its ajax as in save.php i dont send page_header and footer when its an ajax request. Also on request completion I update (appendTo) html of dialog window.
Wbdvlpr
Ok I give up then :p I've had some issues with ajax posts before but they were related to cross domain stuff.
serg
@serg555 - thanks a lot for your efforts .. its a bit strange as if I use .post it sends it as POST, ($.post uses .ajax itself!!) .. thnx
Wbdvlpr
+1  A: 

Why not call jQuery.post() directly?

$.post("save.php",
  $("#myform").serialize(),
  function(html) { 
    $("#mydiv").append(html);        
  },
  "html"
);

In regards to jQuery.ajax(), changing to type: "POST" instead of method: "POST" will cause a proper POST request:

$.ajax({
        type: "POST",
        url: "test.mhtml",
        data: $("#myform").serialize(),
        success: function(html){ 
                $('#mydiv').html(html);
        }
});

This shows up in the Apache logs as:

::1 - - - [30/Oct/2009:09:44:42 -0700] "POST /test.php HTTP/1.1" 200 9 "http://localhost:10501/test.mhtml" "(sic)"

Possible alternate issue:
I found this question on StackOverflow while looking around at your problem. Maybe it isn't the jQuery which is giving you trouble, it is PHP? The top voted answer has some suggestions for ensuring that PHP isn't interfering, and the second highest answer offers some code to see if the request is actually a POST or not:

<?php
  if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    echo 'POSTed';
  }
?>
Jack M.
how about beforesend, complete, success ?? with .post ??
Wbdvlpr
just use:$.post("save.php", $("#myform").serialize(),function(data){$('.msg').html(data););(3rd arugment is a function callback.)
CodeJoust
I edited to include the third and fourth arguments. The third argument is the callback function for when information comes back, and the fourth argument is the type of data coming back. jQuery will do some parsing for you, if you change to JSON return type.
Jack M.
@Jack thanks for your example. I can see the .post works. But if you help me debug .ajax as well that would be great!!
Wbdvlpr
Mostly just giving you what Sbm007 said above. They are correct, as this generates a POST request.
Jack M.