2

I would like to send 'ID' to JS function then send the same ID again from JS function to php function. Please tell me what is the wrong in my code!

<script type="text/javascript">
function deleteclient(ID){    //I receive the ID correctly until now!
var x = "<?php deleteclient11('ID');?>";
return false;
}
</script>

<?php
function deleteclient11($x)
{
echo "$x";
}
?>
Ahmed
  • 255
  • 2
  • 7
  • 17

1 Answers1

8

You need to use AJAX, it's easy with jQuery or another library, so I'll demonstrate with jQuery.

javascript

var deleteClient = function(id) {
    $.ajax({
        url: 'path/to/php/file',
        type: 'POST',
        data: {id:id},
        success: function(data) {
            console.log(data); // Inspect this in your console
        }
    });
};

php file

<?php

    if (isset($_POST['id'])) {

        deleteClient11($_POST['id']);

        function deleteClient11($x) {
           // your business logic
        }

    }

?>

More info: jQuery.ajax()

francisco.preller
  • 6,559
  • 4
  • 28
  • 39