php - How to call a function onClick of a directory

788

i'm trying to make a file browser and i'm trying to find how to call a function when the user clicks on a directory. For starting i tried to use onClick inside a href on the class part (right before FUNCTION folderSize) having us a goal to open the path inside testDir if i click on any of the files or directories on the list but it does nothing. I'm searching for some hours now it but i can't find the solution. If someone could help i would be greatfull. Here is the index.php:

68

Answer

Solution:

Please Notice, that this code is not suitable for public productive use. It's good enough to learn with it.

The HTML-Part:

<a href="#" onclick="call_php_function('any_php_function', 'anyattribut')">Directory-Name</a>

The jQuery (Js) Part:

function call_php_function(php_function, anyattribut) {
  $.post("functions.php", ("call_function": php_function, "attribute_name": anyattribut), function (data) {
    // Data is what you get from the echo in the PHP-Script below
    alert(data); // Outputs "hello"
  }
}

The PHP-Part:

function any_php_function($attribut) {
  echo "hello";
}

$allowed_funcs = array("any_php_function", "any_other_function");
if (in_array($_POST["call_function"], $allowed_funcs))
  call_user_func($_POST["attribute_name"]);

This code is enough for your purposes:

HTML-Part:

<a href="#" onclick="call_php_function('directory-name')">directory-name</a>

jQuery-Part:

function call_php_function(directory) {
  $.post("your_file.php", ("dir": directory), function(data){
    alert(data); // Will output, whatever your starter-functions outputs
  });
}

PHP-Part

if (isset($_POST["dir"]) && !empty($_POST["dir"])) {
  starter($_POST["dir"]);
}

People are also looking for solutions to the problem: Insert data to table in php/mysql & automated insert date

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.