我正在尝试在 AJAX 中回调 PHP 函数。但它对我不起作用,下面的代码正确吗?任何帮助我将不胜感激,谢谢!
JavaScript AJAX call
//AJAX call for button
$("#primaryTextButton").kendoButton();
var button = $("#primaryTextButton").data("kendoButton");
button.bind("click", function(e) {
var test = $("#dropdown").val()
$.ajax({
url: "../DesignationProgramTemplate/testjson.php",
type: "POST",
data: function(){
return {
method: "getAddTemplate",
}
},
success: function (respond) {
// you will get response from your php page (what you echo or print)
kendo.alert('Success'); // alert notification
},
});
});
PHP function (testjson.php)
function addTemplate(){
global $ehorsObj;
$employeeID = $_SESSION['employeeID'];
$propertyID = $_SESSION['propertyID'];
$hrsPositionID = (isset($_POST['id']) ? $_POST['id'] : '');
$programID = (isset($_POST['progid']) ? $_POST['progid'] : '');
// $id = $_POST['id'];
// $progid = $_POST['progid'];
for($x=0; $x< sizeof($progid); $x++ )
{
$array = array();
$sqlSelect = "SELECT * FROM tblHrsPositionProgramTemplate WHERE hrsPositionID = '".$id."'
AND programID = '".$progid[$x]."'";
$GetResult = $ehorsObj->FetchData($sqlSelect, $ehorsObj->DEFAULT_PDO_CONNECTIONS);
$positionTemplateID = $ehorsObj->EHORS_PK("tblHrsPositionProgramTemplate");
$sqlAdd = "INSERT IGNORE INTO tblHrsPositionProgramTemplate
SET positionTemplateID = '" . $positionTemplateID . "',
programID = '" . $progid[$x] . "',
hrsPositionID = '" . $id . "',
propertyID = '" . $propertyID . "',
employeeID = '" . $employeeID . "',
dateTimeEmployee = NOW() ,
active = 'y' ";
$ehorsObj->ExecuteData($sqlAdd, $ehorsObj->DEFAULT_PDO_CONNECTIONS);
$positionTemplateIDLog = $ehorsObj->EHORS_PK("tblHrsPositionProgramTemplateLog");
$sqlAddLog = "INSERT IGNORE INTO tblHrsPositionProgramTemplateLog
SET positionTemplateIDLog = '" . $positionTemplateIDLog . "',
positionTemplateID = '" . $positionTemplateID . "',
programID = '" . $progid[$x] . "',
hrsPositionID = '" . $id . "',
propertyID = '" . $propertyID . "',
employeeID = '" . $employeeID . "',
dateTimeEmployee = NOW(),
active = 'y' ";
$ehorsObj->ExecuteData($sqlAddLog, $ehorsObj->DEFAULT_PDO_CONNECTIONS);
}}
我想将函数 addTemplate 调用到 AJAX 调用中。如何调用方法“getAddTemplate”。我真的很想知道代码是否有错误。或者我错过了一些东西。谢谢!
最佳答案
你不能直接从Javascript调用PHP的函数。你必须在PHP内部调用PHP函数
<?php
function yourPhpFunction() {
// Definition here
}
yourPhpFunction();
关于javascript - 使用 jQuery AJAX 调用将数据传递到函数中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57487010/