Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery .ajax method="post" but $_POST empty

Tags:

jquery

ajax

php

$.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 work?

like image 681
TigerTiger Avatar asked Oct 30 '09 15:10

TigerTiger


4 Answers

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">
<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"></script>
    <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.

like image 196
Waleed Amjad Avatar answered Nov 15 '22 02:11

Waleed Amjad


**Add below piece of code in your code before success **

beforeSend: function(xhr){xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded")}
like image 40
Rajnish Avatar answered Nov 15 '22 02:11

Rajnish


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';
  }
?>
like image 4
Jack M. Avatar answered Nov 15 '22 03:11

Jack M.


I was having trouble with the variables getting lost in the mix as well and discovered that while using Rewrite Conditions to externally redirect dir/foo.php to dir/foo Apache was dropping the POST request altogether and redirecting.

So when writing the JavasSript you must reference the file in a way that it can bypass any external redirecting.

eg. Drop the extension name inline with the JavaScript.

Write

url: "dir/foo"

instead of

url: "dir/foo.php"

I hope that this helps others who found this page like me.

like image 4
jkofron.e Avatar answered Nov 15 '22 04:11

jkofron.e