1

I used ajax to send to me a php variables in this way:

<script type="text/javascript">
$(document).ready(function(){
    $("#disdiciButton").click(function(){

       var log_user = <?php echo json_encode($log_user);?>;
            alert(log_user);

            $.ajax({
               type: 'POST',
               url: 'disdici.php',
               data:log_user,
               success: function(data) {
                   $("#deleteResponse").text(data); 

               }
        });
     });
});
</script>

Now I'm trying to collect this data in the php page disdici.php but I don't know how. I've tried in this way:

$log_user = "";
if(isset($_POST['data'])){
   $log_user = json_decode(data);         
}
echo 'user: '.$log_user;

but $log_user remain empty. How can I do?

2
  • data isn't a posted field. It's a constant in this case, so $log_user will be empty. Commented Jun 23, 2018 at 16:59
  • what I have to write in that place? Commented Jun 23, 2018 at 17:06

1 Answer 1

1

You have to write your data like :

data : 'var1=' + value1 + '&var2=' + value2 + '&var3=' + value3...

So, you can get the variable in php :

$_POST['var1'] = value1; 
...

In your example :

<script type="text/javascript">
$(document).ready(function(){
   $("#disdiciButton").click(function(){

      var log_user = <?php echo json_encode($log_user);?>;
           alert(log_user);

           $.ajax({
              type: 'POST',
              url: 'disdici.php',
              data:'log_user=' + log_user,
              success: function(data) {
                  $("#deleteResponse").text(data); 

              }
       });
    });
});
</script>

In PHP file, you get the value :

$_POST['log_user']
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.