简体   繁体   中英

How to pass javascript variable into php function on an Ajax url?

I am trying to insert the value of parent into the "getFaculties()" function when i call the function using Ajax.

    function ajaxfunction(parent)
    {
        $.ajax({
            type: 'GET',
            url: 'Connection.php?getFaculties('+parent')',
            success: function(data) {
                $("#selFaculty").html(data);
            }
        });
    }

The correct syntax is

url: 'Connection.php?faculties='+getFaculties(parent),

Since that is query parameter, given a name to it.

use like this function Declaration was wrong

function ajaxfunction(parent)
    {
        $.ajax({
            type: 'GET',
            url: 'Connection.php?getFaculties='+getFaculties(parent),
            success: function(data) {
                $("#selFaculty").html(data);
            }
        });
    }

please use a proper way to pass data from ajax to php

function ajaxfunction(parent)
    {
        $.ajax({
            type: 'GET',
            url: 'Connection.php',
            data: {method:'getFaculties', value:parent}
            success: function(data) {
                $("#selFaculty").html(data);
            }
        });
    }

Call your function first and get return value in variable and then send your ajax request.

function ajaxfunction(parent)
{
    var data_in = getFaculties(parent);
    $.ajax({
        type: 'GET',
        url: 'Connection.php?getFaculties='+data_in,
        success: function(data) {
            $("#selFaculty").html(data);
        }
    });
}

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