1

Possible Duplicate:
Call php function from javascript

can anyone help me on how can I call the php function on JS function?

Here is the event:

I have 2 selection box (e.g projects and task) when selecting on project selection box, the task selection box will be populated. values populated on the task selection box is in my PHP DAO.

Here is my javascript but it doesn't work but my idea is like that

function getProjTask()
{
    var taskList = <?php $projTask->getProjectTask("ProjectName");  ?>

    //values for task selection box has been populated here

}
Community
  • 1
  • 1
Mark
  • 314
  • 1
  • 5
  • 17
  • you need to use ajax for that – COLD TOLD Apr 16 '12 at 03:51
  • see http://stackoverflow.com/questions/4362589/can-javascript-call-a-php-function-directly-or-do-i-need-separate-php-file-to-c/4362611#4362611 – Andreas Wong Apr 16 '12 at 03:53
  • possible duplicate of [Call php function from javascript](http://stackoverflow.com/questions/7165395/call-php-function-from-javascript) and http://stackoverflow.com/questions/221396/javascript-and-php-functions – Matt Ball Apr 16 '12 at 03:57

4 Answers4

3

For this to work, you need to use AJAX. This can be done using just plain old javascript or your favorite library. Using a library would be much easier. I tend to use YUI myself.

What you need to do is quite simple:

  1. Listen to project selection box for changes.
  2. When a change happens, send a request using AJAX to your PHP endpoint.
  3. Your endpoint then returns a JSON response containing the values of the task selection box.
  4. Use javascript to decode the JSON response and create <option> elements for your task selection box.

A quick example:

PHP side:

<?php

$list = array('list' => array('task1' => 1, 'task2' => 2));

header('Content-Type: application/json; charset=utf-8');
echo json_encode($list);

exit();

JS side (I am using YUI, and this is for the AJAX only, you will still need to write code to listen for the selection box changing):

<script>
// Create a YUI instance using io-base module.
YUI().use("io-base", function(Y) {
    var uri = "get.php?foo=bar";

    // Define a function to handle the response data.
    function complete(id, o) {
        var data = Y.JSON.parse(o.responseText);

        //Parse the JSON, loop and insert the options here.
    };

    // Subscribe to event "io:complete"
    Y.on('io:complete', complete);

    // Make the request
    Y.io('http://mysite.com/myendpoint.php');
});
<script>
F21
  • 32,163
  • 26
  • 99
  • 170
1

Either use ajax to make a request dynamically. If you just want the value of the name to be in that function, then the error will be because you have no quotes (and it being a string)

function getProjTask()
{
    var taskList = '<?php $projTask->getProjectTask("ProjectName");  ?>';

    //values for task selection box has been populated here

}
Menztrual
  • 40,867
  • 12
  • 57
  • 70
1

maybe you could find this useful. You need AJAX for this. jQuery is the easiest way for me but if you want to use ajax using php try http://www.xajaxproject.org/

A friend of mine uses it all the time and it works good for him. I preffer js and jquery

chepe263
  • 2,774
  • 22
  • 38
-1

If you don't mind learning a new language you can use haxe. Its a cross platform language. You can develop for js and php using haxe and link them together using haxe itself ( haxe remoting ).

carboncopy
  • 37
  • 9
  • Haxe is a great tool, but I don't think it answers the question here at all. The question is a much more fundamental one about server-side and client-side code. – Spudley Apr 16 '12 at 20:50