0

I'm trying to call a PHP funtion after onclick event, the function needs a DOM element's value as parameter (this is why i have to use jquery as well)

I want to have something which looks like this :

<button type="button" onclick="document.write('<?php hello("parameter") ?>');">Submit</button>

Here's what i've been able to do so far, but it's not working :

<button type="button" onclick="document.write('<?php hello("<script>$(#idproduct).text()</script>") ?>');">Submit</button>

Any help would be appreciated, thank's.

codeless
  • 145
  • 5
  • 14
  • 3
    impossible. php runs on the server. that php code will execute **ONCE** when the page is generated, and get replaced with its output. it will never reach the client's browser. and even if it did reach the browser, it is incredibly unlikely that the user's browser could execute PHP in the first place. – Marc B Mar 23 '16 at 21:33
  • It is indeed not possible - PHP runs on the server, Javascript in the client's browser. You need to look up sending the data to the backend server via AJAX - there's a hundred ways to do this, like http://stackoverflow.com/questions/2269307/using-jquery-ajax-to-call-a-php-function – Andy Hoffner Mar 23 '16 at 21:50
  • You will have to use `ajax` and send a post/get request – James111 Mar 23 '16 at 21:51
  • Thank you guys, i wanted to avoid the AJAX method and thought there could be an easier way to do it .. but it seems there is no alternative. – codeless Mar 23 '16 at 21:52

0 Answers0