1

I have a HTML element. When I click on it, it calls a javascript function and inside the function I want to make a database update and hence for that I need php scripting. I tried doing the following

<script>
function myMethod(){
<?php 
MyPHP code
?>
}
</script>

But it did not work. What can I do?

Kraken
  • 23,393
  • 37
  • 102
  • 162
  • 3
    PHP executes on the sever, Javascript executes on the client. What you want is impossible, unless you want to use an AJAX system or do full-blown form submissions. – Marc B Feb 08 '14 at 20:56
  • are you sure you want to go down this way..You could PHP at the backend and pass the data to it using ajax to save it in the DB. – Works On Mine Feb 08 '14 at 20:56
  • Ouch! That was my bad. I've been reading so much about these stuff lately, that I guess I messed up big time. Yeah, will probably use AJAX for my purpose. – Kraken Feb 08 '14 at 21:00

4 Answers4

2

You can use jQuery.post() or jQuery.get() to call a dedicated remote file. These methods are jQuery's AJAX shorthands to load/execute data from the server using a HTTP request.

For example:

<script type="text/javascript">
    function myMethod()
    {
        $.post("your_php_file.php");
    }
</script>

The remote file should include your desired commands.

Surely don't forget to include the jQuery javascript file in your code: aside from download and put it locally, you can import it remotely.

Community
  • 1
  • 1
Reflection
  • 1,936
  • 3
  • 21
  • 39
  • Surely you mean AJAX (or XmlHttpRequest?), and not jQuery, which is just a library that uses these JavaScript communication methods? – Mark Feb 08 '14 at 21:07
  • @Kraken I recommend you to read about AJAX in Wikipedia and jQuery's documentation. – Reflection Feb 08 '14 at 21:09
  • https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest and https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest?redirectlocale=en-US&redirectslug=DOM%2FXMLHttpRequest%2FUsing_XMLHttpRequest – Mark Feb 08 '14 at 21:12
  • 1
    @Reflection I have one more question though, if in the php that i am calling, in the end i want to redirect the page. I should not be doing that in the php right? – Kraken Feb 08 '14 at 21:22
  • 1
    @Kraken Right; you can perform JavaScript commands after succeeding the AJAX request with jQuery.post's callback/lambda, like that: `$.post("your_php_file.php", function() { redirect_command(); });`. But, for new questions - you have to ask separately. – Reflection Feb 08 '14 at 21:29
1

you cannot do that because php is executed before the page is sent to the client. you have to make a second php script containing the function. you can then use XMLHttpRequest or jQuery to access this php script.

Palle
  • 11,511
  • 2
  • 40
  • 61
1

JavaScript is client side scripting (code executes inside the browser e.g. IE, Chrome, Firefox etc.). PHP is server side scripting. You can not put these two together. If you want to update something on the server side from client side, you have to make a POST to the server from JavaScript, probably with AJAX technology.

Gašper Sladič
  • 867
  • 2
  • 15
  • 32
0

The only way to interact with PHP from JS is AJAX. If you use the jQuery lib for AJAX, the easiest way is the .load() function.

<script type="text/javascript">
function myMethod()
    {
    var loadTo = "#id_of_the_div_or_other_element_to_hold_any_PHP_output";
    var loadFrom = "the_php_file_you_want_to_run.php";

    $(loadTo).load(loadFrom); 
    }
<script>

Link to jQuery: http://jquery.com/

Link to jQuery's .load() function: https://api.jquery.com/load/

IKM
  • 147
  • 1
  • 5