-5

I have php function and I want to call it from a js function. Consider this:

<?php
function phpPrint(){
    echo '<h1>1111</h1><br>';
}
?>

<script>
function doSomthing(){
<?php
phpPrint();
?>
}
</script>

And it didn't work. I hope this question is not duplicate, I saw many questions with the same title, but no one answered my question. I will appreciate if you answer my question and not mark as duplicate. thks

hhgjgj
  • 3
  • 1
  • 4
  • 3
    PHP executes first, it is server side. JS executes after, on the users computer, it is client side. You will need to send an ajax request to PHP script. http://i.stack.imgur.com/Ax87s.png – chris85 Jul 27 '15 at 21:36
  • How can I do it, can it be done easily(without knowing ajax?) – hhgjgj Jul 27 '15 at 21:40
  • Yes, there are many tutorials online for it. Have a look here https://learn.jquery.com/ajax/. There are also threads on this site about it.. here's an answer I gave a while ago that was not used, maybe it'll be useful for you, http://stackoverflow.com/questions/28666973/how-can-i-concatenate-a-value-inside-function-in-jquery-in-this-situation/28667255#28667255. – chris85 Jul 27 '15 at 21:41
  • "`can it be done easily(without knowing ajax?)`" it can't be done at all without ajax – developerwjk Jul 27 '15 at 21:47
  • @developerwjk: well, you CAN Do a full-blown round-trip to the server, so never say never... – Marc B Jul 27 '15 at 21:49
  • @MarcB Well, not like what he's trying to do. Yes, if you submit a form or click a link you can do it without ajax. – developerwjk Jul 27 '15 at 21:51
  • If its simple, can you show me a simple example to make it work.I hope I am not asking for too much... – hhgjgj Jul 27 '15 at 22:15

1 Answers1

0

To call a php function from javascript you need to use ajax. Keep in mind jQuery needs to be included for this script to work: PHP file foo.php

<?php
    function phpPrint() {
        //do something to get results;
        echo $results; //it's often useful to json_encode this, if it's an array. But whatever is echoed onto the page will be availabe to ajax
        //example of json_encode
        echo json_encode($results);
    }

Ajax script:

$.ajax({
    url: 'foo.php',
    method: 'POST',
    data: { }, //if data needs to be sent to the php script it can be retrieved in the $_POST super global
    success: function(data) { //if the ajax call was successful this method will be called and whatever data was echoed in the php script will be in the data variable
        console.log(data);
        var result = JSON.parse(data); //if you used json_encode for an array or object
    },
    error: function() { //called if the ajax method fails check link for parameters and usages

    },
    complete: function() { //called regardless of success or failure

    }
});

You can check this out http://api.jquery.com/jquery.ajax/ to see other usages or options of the $.ajax method. Ajax is very easy and very useful, there is no reason not to learn ajax, if you want to do web development, you need to learn it.

Evadecaptcha
  • 1,403
  • 11
  • 17