tags:

views:

48

answers:

2

People want to use my Jquery plugin, but get a list of images from a database instead of a directory, which is the standard way the plugin works.

My Jquery plugin that uses the ajax() function to deliver & return data to a php script.

The php script pulls files from a directory with getdir(), adds those files to an array and assigns that array to a variable.

People have requested the ability to create that variable value themselves by generating it from a database (not just pulling files from a directory using scandir).

What is a good, secure way to do to allow others to replace a variable value inside my php script?

Ideally, I would like there to be a way where users can create their own script in a separate document (and they don't need to alter my document).

Here is a simplified example of my script:

HTML Document

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"&gt;&lt;/script&gt;
 <script>
$.ajax({
    type: "POST",
    url: "example.php",
    cache: false,
    data: {
        foo: "some_directory",
            },
    success: function(html){
    alert(html);
    }
});
</script>

PHP Document

<?php

$foo = $_POST["foo"];
$foo = scandir($foo);
$foo = (explode(",",$foo)); // how can I allow others to change this variable value?
foreach ($foo as $a) {
    echo "<p>$a</p>";
}

?>
+2  A: 

It sounds like you might want to look into writing an API.

Kieran Hall
Yes, that's what I'm afraid of :) I'm a PHP novice and there are many different ways to write an API. Do you have any tips to get me started?
Upvoted, this was my first thought. I'd offer an answer, but I have no idea how to implement an API.
David Thomas
I'm about to start writing an API for an application that I'm working on. To be honest, if I were you, I'd post another question on this site. I may be able to give you some advice, but I'm by no means an expert at writing APIs and you'd probably find one if you ask another question.
Kieran Hall
A: 

Use event based programming. When it comes time to assign the variable, Detect whether a function called "onGetDirAssignVar" exists. If it does, call the function and accept it's results. If it doesn't, then follow the default path.

var $foo;
if (function_exists("onGetDirAssignVar")) {
  $foo = onGetDirAssignVar(params);
} else {
  $foo = array("first","second"); // how can I allow this value to be changed?
}

foreach ($foo as $a) {
    echo "<p>$a</p>";
}
altCognito
I'm really confused now, the data submitted by who is stored in an object literal where? Maybe give more details on what your script does and it might be possible to give a better answer as to what might more sense from your _users_ perspective. I was under the impression that these were developers making the request.
altCognito
Thanks for the input. I updated my original question with more details. Please let me know if it's more clear.