0

I was creating a webpage that calls a different PHP function which is on a single PHP page. let me explain with a example: page1 contains this jscript:

            $.post("function.php", {
    func: "fun1",
    fun_var: $('#fun1').val() });

and page2 contains

            $.post("function.php", {
    func: "fun2",
    fun_var2: $('#fun2').val() });

and function page contains

            fun1()
            {
                return $result1
             }
              fun2()
            {
                return $result2
             }  

my problem none of the function is called, and I could not found where I am making mistake can any one please provide me the simple skeleton of this type of problem

BenMorel
  • 34,448
  • 50
  • 182
  • 322
  • Why are you trying to use AJAX? None of this code will work. – Dany Caissy Jul 16 '13 at 20:42
  • 1
    You're trying to have JavaScript run a PHP function ...? – BLaZuRE Jul 16 '13 at 20:43
  • then pls tell what should i do any other site reference –  Jul 16 '13 at 20:46
  • Is you intent to have actual logic in `fun1()` and `fun2()` in PHP at some point, rather than just echo back the values? If this is the case you need to look for `$_POST['func']` to determine which function to run. – Mike Brant Jul 16 '13 at 20:47
  • We can't tell you what you should do - there's not enough information in your question about what you're trying to achieve. –  Jul 16 '13 at 20:47

3 Answers3

0

You can't call a PHP function from a HTTP request.

You need to pass the function names as request variables either via POST or GET. In PHP make sure to not execute some weird function (e.g. with a whitelist).

feeela
  • 29,399
  • 7
  • 59
  • 71
0

This is how you call a PHP function :

function printHello()
{
    print 'hello';
}

printHello();

That's it, if it's in another file and you include it, then you will still call it the same way.

If you want to call a PHP function depending on the user's interaction with the site, THEN you need to use AJAX.

Your code looks like pseudo AJAX and it doesn't really make sense, there is an AJAX example here : jQuery Ajax POST example with PHP

Community
  • 1
  • 1
Dany Caissy
  • 3,176
  • 15
  • 21
0

page.html:

$.post("function.php", { func: "fun1", fun_var: $('#fun1').val() });

page.php:

if (isset($_POST['func']) && $_POST['func'] === 'fun1')
{
    function fun1()
    {
        return (isset($_POST['func_var'])) ? $_POST['func_var'] : false;
    }

    fun1();
}
etc...

Try this!

RomanGor
  • 3,835
  • 2
  • 19
  • 26