php 从javascript调用php函数并发送参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19149166/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 18:55:28  来源:igfitidea点击:

Call php function from javascript and send parameter

javascriptphp

提问by Ahmed

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!

我想将“ID”发送到 JS 函数,然后再次从 JS 函数向 php 函数发送相同的 ID。请告诉我我的代码有什么问题!

<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";
}
?>

回答by francisco.preller

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

您需要使用 AJAX,使用 jQuery 或其他库很容易,所以我将使用 jQuery 进行演示。

javascript

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文件

<?php

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

        deleteClient11($_POST['id']);

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

    }

?>

More info: jQuery.ajax()

更多信息:jQuery.ajax()