简体   繁体   中英

Pass data using jQuery AJAX call into function

I'm trying to call back function PHP in AJAX. But it not working for me, is it correct the code below? Any help i will appreciate it, thank you!

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);
    }}  

I want to call function addTemplate into AJAX call. How do I call the method "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. You have to call the PHP function inside 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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