Quantcast
Viewing latest article 1
Browse Latest Browse All 6

Answer by Asef Hossini for How to call a php function from ajax?

As a structure for this kind of purposes, I suggest this:

PHP

<?php    if(isset($_POST['action'])){        if ($_POST['action'] == "function1") { func1(); }        if ($_POST['action'] == "function2") { func2(); }        if ($_POST['action'] == "function3") { func3(); }        if ($_POST['action'] == "function4") { func4(); }    }    function func1(){        //Do something here        echo 'test';    }?>

jQuery

var data = { action: 'function1' };$.post(ajaxUrl, data, function(response) {    if(response != "") {        $('#SomeElement').html(response);    }else{        alert('Error, Please try again.');    }});

As mentioned, you can't call a PHP function directly from an AJAX call.


Viewing latest article 1
Browse Latest Browse All 6

Trending Articles