-1

i have problem with php.

AJAX

    $(document).ready(function(){

$.ajax({
  type: "POST", // i test post, get...
  url:'TESTING.php', 
  data: {name: "alfred"},
  success: function(){  
      alert("success"); // it show "success" !... :/
    } 
});
});

And php

<?php 
//var_dump($_POST); array is empty
$name = $_POST['name']; // error line
echo $name;
?>

I test it on Localhost (if it is the problem). And i dont know, where is mistake. Thanks for any help

user3254997
  • 3
  • 1
  • 3

4 Answers4

1

Along with what the comments say about $_GET not grabbing $_POST data you sent, you are not using the return data.

success: function(data){ //response param
    alert(data);
}
Sterling Archer
  • 22,070
  • 18
  • 81
  • 118
0
  1. You are sending the data via POST, not GET .

    Change

    $name = $_GET['name'];
    

    To

    $name = $_POST['name'];
    
  2. Your callback function must have argument,

    Change

    success: function(){  
      alert("success");
    }
    

    To

    success: function(data){  
      alert("success"); // or whatever you wanna do
    }
    
Alireza Fallah
  • 4,609
  • 3
  • 31
  • 57
0

Here is all code

    <!DOCTYPE html>
<html>
    <head>
      <title>Test</title>
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
      <script src="//code.jquery.com/jquery.js"></script>
    </head>
    <body>
 </body>
<script>
$(document).ready(function(){

$.ajax({
  type: "POST",
  url:'TESTING.php', 
  data: {name: "Alfred"},
success: function(data){ //response param
    alert(data);
}

});

});
</script>

<?php 
echo "Hi ";
//var_dump($_POST);
$nick = $_POST['name'];

echo $nick;
 ?>
    </body>
</html>
user3254997
  • 3
  • 1
  • 3
0
var weightd = $("#weight").val();
var user_id = <?php echo $current_user->ID; ?>;
$.ajax({
                 type: "POST",
                url:"<?php bloginfo('template_directory')?>/ajax/index.php",
                data: { weight:weightd,user_ids:user_id},
                success:function(result){
                  $("#result1").html(result);
                  setInterval(function() {
                    $('#result1').hide('slow');
                    }, 5000);
                }});


<div id="result1"></div>

try to user like this

Vikas Gautam
  • 997
  • 7
  • 23