-3

Am new in php, so please explain me this code,I want to call a Php function using this code,is it possible,and my function name is remove_db();(no parameters) , What is the myphpscript.php? Please explain.

<script type="text/javascript" src="./jquery-1.4.2.js"></script>
<script type="text/javascript"> 
    function compute() {
        var params="session=123";
        $.post('myphpscript.php',params,function(data) { 
            alert(data); //for testing if data is being fetched
            var myObject = eval('(' + data + ')');
            document.getElementById("result").value = myObject(addend_1,addend_2);
        }); 
    } 
</script>
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Ameen
  • 13
  • 2
  • 1
    Learn about HTTP protocol :)) – moonwave99 Oct 09 '13 at 10:11
  • possible duplicate of [Reference: Why does the PHP (or other server side) code in my Javascript not work?](http://stackoverflow.com/questions/13840429/reference-why-does-the-php-or-other-server-side-code-in-my-javascript-not-wor) – deceze Oct 09 '13 at 10:12

3 Answers3

1

You can NOT call a PHPfunction from javascript. Javascript is CLIENTside (=executes in the browser) and PHP is SERVERside (=executed on server).

Php has llready done its job, and sent everything to the browser, therefor it cant do anything

This is where the AJAX comes in. The ajax sends a request to a phpfile. The php gets executed on the server and gives javascript the result(if any).

You need to put the php function in that file to be executed (just as php works). If you want to send some info to php, use that data option, which is explained in the jQuery documentation

Martijn
  • 15,791
  • 4
  • 36
  • 68
0

You should read up on AJAX calls (jQuery Docs).

Another library that I've found useful is XAJAX it makes AJAX calls to PHP functions nice and easy.

piddl0r
  • 2,431
  • 2
  • 23
  • 35
-1

you can call php function in json object . for example:

i want to show a captcha, and my captcha is created by a php function, so in my javascript code I use this codes:

     var $img = $("#form_img);
     $img.attr("src", "/json-req:path\to\phpfile?req=" + escape("{\"id\":1, 
    \"method\":\"method name\", \"params\": [\"parameter\"]}"));
Alireza Fallah
  • 4,609
  • 3
  • 31
  • 57
Samira Khorshidi
  • 963
  • 1
  • 9
  • 29
  • 2
    this answer is wrong, you never ever can call a php function from javascript or html , you can only call a php FILE, not a FUNCTION. look at @martijn 's answer – Alireza Fallah Oct 09 '13 at 11:01
  • sorry for my answer,i understood wrong your question,i offer you see this framework,it could help you.http://sourceforge.net/projects/pome-framework/ – Samira Khorshidi Oct 10 '13 at 05:53