0

I have js function to call a php function via ajax.

$("#history").click(function(){
    $.ajax({
        url: "./function.php",
        type: "POST",
        data: {"displayBookingHistory"},
        dataType: "JSON",
        success: function(data) { 
            console.log("hellosdasdasds");
            $("#universalLogin").append(data);
        }
    }); 
})

and php function is

function displayBookingHistory () {
    $html = " ";
    ....
    echo json_encode($html);
}

and the call seems to be not successful, even when I try data: "displayBookingHistory",

Anyone knows solutions?

1
  • Try "return json_encode($html);" instead of "echo json_encode($html);". The data you submit isn't a function in PHP, it's the data you can work with in your PHP script. To call the function you have to add additional infos to the URL. Commented Feb 8, 2015 at 11:32

4 Answers 4

2
  1. You have a syntax error in your JavaScript: an object needs to consist of a series of property:value pairs.
  2. You can't call a PHP function using Ajax. Only a URL. You need to write your PHP so that it calls the function if you hit that URL.
  3. You are completely ignoring $_POST in your PHP, so there is no reason for it to do anything with the data you are sending to it.
Sign up to request clarification or add additional context in comments.

Comments

1

jQuery's AJAX method expects an object with key:value pairs (or a string) for the data field.

Like Quentin said you can't call PHP functions from AJAX, only complete pages. If you want to communicate "run this function" you will have to use $_POST data like this:

$("#history").click(function(){
    $.ajax({
        url: "./function.php",
        type: "POST",
        data: {function:"displayBookingHistory"},
        dataType: "JSON",
        success: function(data) { 
            console.log("hellosdasdasds");
            $("#universalLogin").append(data);
        }
    }); 
})

Then in your PHP page:

if(isset($_POST["function"]){
    $function = $_POST["function"];
    if($function=="displayBookingHistory"){
        displayBookingHistory();
    }
}

Comments

0

How are you invoking that function? May be you should have a switch statement that will check the data and call that function. something like this

 if(isset($_POST[data]){
 switch($_POST[data]){
 case "displayBookingHistory" : 
  displayBookingHistory();
 break;
 }
}

Comments

0

your have syntax error ajax send data.

data : {"property:value"} or data : {"property:value" , "property:value" , ...}

problem can be solved in 2 ways:

$ ("# history"). click (function () {
     $ .ajax ({
         url: "/function.php?action=displayBookingHistory",
         type: "POST",
         dataType: "JSON",
         success: function (data) {
             console.log ("hellosdasdasds");
             $ ("# universalLogin"). append (data);

             return false; //for disable a tag (link) click;
         }
     });
})

"action" is a parameter used for $_GET type to check php code.

Or

$ ("# history"). click (function () {
     $ .ajax ({
         url: "/function.php",
         type: "POST",
         data: {"action":"displayBookingHistory"},
         dataType: "JSON",
         success: function (data) {
             console.log ("hellosdasdasds");
             $ ("# universalLogin"). append (data);

             return false; //for disable a tag (link) click;
         }
     });
})

"action" is a parameter used for $_POST type to check php code.

the best way is read a tag href attribute for work in javascript disable.

a tag :

<a href="/function.php?action=displayBookingHistory" id="history">linke</a>

javascript :

$ ("# history"). click (function () {
     $ .ajax ({
         url: $(this).attr("href"),
         type: "POST",
         dataType: "JSON",
         success: function (data) {
             console.log ("hellosdasdasds");
             $ ("# universalLogin"). append (data);

             return false; //for disable a tag (link) click;
         }
     });
})

php code :

   $action = isset($_GET["action"]) ? $_GET["action"] : "";

   if($action == 'displayBookingHistory' )
       displayBookingHistory();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.