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";
}
?>
4
  • 2
    The only wany I know to send data to php from js is AJAX Commented Oct 3, 2013 at 0:55
  • 3
    Why do so many people seem to think you can just call PHP functions in JavaScript...? Commented Oct 3, 2013 at 0:56
  • 3
    WHat you are doing here could possibly cause an irreversible rip in the time-space fabric. Try doing some research on AJAX and how to use it with PHP and Javascript. Commented Oct 3, 2013 at 0:57
  • 1
    @DevlshOne Too funny! Commented Oct 3, 2013 at 1:02

1 Answer 1

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()

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.