简体   繁体   English

使用jQuery AJAX调用将数据传递给函数

[英]Pass data using jQuery AJAX call into function

I'm trying to call back function PHP in AJAX. 我试图在AJAX中回调函数PHP。 But it not working for me, is it correct the code below? 但这对我不起作用,是否正确下面的代码? Any help i will appreciate it, thank you! 任何帮助,我将不胜感激,谢谢!

JavaScript AJAX call JavaScript AJAX调用

//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) PHP函数(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);
    }}  

I want to call function addTemplate into AJAX call. 我想将函数addTemplate调用到AJAX调用中。 How do I call the method "getAddTemplate". 如何将方法称为“ getAddTemplate”。 I'm really want to know if there is any code is wrong. 我真的很想知道是否有任何代码是错误的。 Or I'm missing something. 或者我想念一些东西。 Thank you! 谢谢!

You cannot directly call PHP's function from Javascript. 您不能直接从Javascript调用PHP的函数。 You have to call the PHP function inside PHP 您必须在PHP中调用PHP函数

<?php
function yourPhpFunction() {
 // Definition here
}

yourPhpFunction();

you should send the function name of controller with full path, check this! 您应该发送具有完整路径的控制器功能名称,请检查此!

$.ajax({
    type: "POST",
    url: baseUrl + 'invoices/order/searchServiceByProvider',
    data: {
        extra_service_id: extra_service_id
    },
    success: function (response) {
     var data = response.data;
     order.push(data);
    }
 });

So, in the url I send the full path of my controller with the name function and finally get the response on success property the ajax method. 因此,在url中,我使用name函数发送了控制器的完整路径,并最终通过ajax方法获得了对成功属性的响应。

This is my answer that solve my problem. 这是解决我的问题的答案。 Hope can help the others! 希望可以帮助别人!

                $.ajax({
                        type: "POST",
                        url: "../DesignationProgramTemplate/testjson.php",
                        data: {
                                method: "deleteTemplate" ,
                                id: test,
                                progid: array
                                },
                    //  data: {'id':test,'progid':array},
                        success: function(){
                            kendo.alert("Data updated");
                        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM