0

I have a javascript function that's executed on a button click and I have an included file containing all my PHP functions. One of the PHP functions returns me an array of filenames encoded as a JSON object. I'd like to call this function from my Javascript function so that I can use the array. I know I can call a PHP file which would run but in this case I only want to call one specific named method.

I tried the following but couldn't get it to work (Console error: Unexpected < )

function getPHPArray(){
    var picArray;
    picArray = <?php getPicArrayJson(); ?>;
}

Can this be done using XMLHttpRequest or do I need to find another method? (If reasonably possible I'd prefer straight Javascript over JQuery but I can use JQuery if it makes this significantly easier)

If so how?

My PHP method is as follows:

function getPicArrayJson(){
    global $fileArrayString;
    If (!empty($fileArrayString)){
        echo json_encode($fileArrayString);
    }
}

My JavaScript so far:

function getPHPArray(){
    var xmlhttp;
if (window.XMLHttpRequest){
    xmlhttp=new XMLHttpRequest();
}
else {// code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}   
xmlhttp.open("POST", "phpFunctions.php?name=getPicArrayJson", true);   
xmlhttp.send();
}
4
  • 1
    Could you explain the misunderstanding so that I can reformulate my approach/question? Commented Apr 13, 2014 at 5:30
  • Seems the downvoters and closers on the question are too lazy to bother adding a comment. Commented Apr 13, 2014 at 5:31
  • First make sure the php you actually call returns valid json Commented Apr 13, 2014 at 5:32
  • Renamed my variables for clarity - is that what you meant or should I be using return over echo? Commented Apr 13, 2014 at 5:40

1 Answer 1

2

This is essentially a Remote Procedure Call. For use with PHP, I'd check out JSON-RPC PHP.


Anyway, here's a basic way you can do it without much complexity to your app. You will have to make an ajax request to the server. The idea for you here is to use call_user_func.

call_user_func("getPicArrayJson");

You can use

call_user_func($_POST["name"]);

But I highly recommend that you only allow certain functions to be called


phpFunctions.php

$allowed_functions = array("getPicArrayJson");
$name              = $_POST["name"];

function getPicArrayJson() {
  $pics = array("1.jpg", "2.jpg");
  return json_encode($pics);
}

// legal function
if (in_array($name, $allowed_functions)) {
  header("Content-Type: application/json");
  $json = call_user_func($name);
  die($json);
}

// invalid access
else {
  // Error 400
}

Then do you Ajax request per normal

...
xmlhttp.open("POST", "phpFunctions.php?name=getPicArrayJson", true);   
xmlhttp.send();

ps my own two cents, I would use a GET request for this. Following RESTful API conventions for web services, a POST request would only be used to create a new element.

Sign up to request clarification or add additional context in comments.

7 Comments

Why the downvotes on this answer? Upvoted for attempting to answer my question - thanks! It seems this is purely PHP side though? How would my JavaScript call look?
I'm trying this solution at the moment - can you explain why you'd prefer GET over POST for this scenario?
@db579, I added a note about GET vs POST and provided a link for you to read up on. I hope this helps.
@naomik how about dropping call_user_func to avoid the overhead of the lookup and simply call the requested function if within an array of allowed functions? eg $func =$_POST["name"]; $func();
@melc, nah. I wanted db579 to see the flexibility provided with call_user_func. For example, an generic func, static method, or a instance method can all be called using call_user_func. It has a nice accompaniment, call_user_func_array, which I'm sure he would see while reading the docs, too. Both of which I believe to be far more readable than wretched variable variables that you're proposing.
|

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.