-3

Please help me on how to concatenate php code on jquery html:

$('div#divID').html('<?php echo $fib->getFibonacci( " + limit + "); ?>");

in order to pass the value: var limit = $('#inputID').val() of input after clicking the button. Thank you!

PHP Code

class Fibonacci {   
  public function getFibonacci($num){
    ...
    return $fib;
  }
}
$fib = new Fibonacci();

HTML

<input type="text"  id="inputID">
<button id="buttonID">
<div id="divID">

JS

$('#buttonID').click(function() {
  var limit = $('#inputID').val();
  $('div#divID').html("<?php echo $fib->getFibonacci( " +limit+");?>");
});

Goal: to pass the value on input when calling Php function using jquery syntax. Is there possible way? Thank you.

KenmacPro
  • 3
  • 1
  • There is no way to add javascript to php but you can pass php to javascript – CAllen Jun 16 '17 at 14:54
  • 2
    The strange layout/format of your question didn't bother you before posting? – Andreas Jun 16 '17 at 14:55
  • 2
    You cannot run PHP code from JS without sending a new request to the server (i.e. using AJAX). When JS takes over, PHP is long done. –  Jun 16 '17 at 15:02
  • Thank you for all your help and answers. I will try to pass it on ajax instead and follow your suggestions. Thanks – KenmacPro Jun 17 '17 at 07:19

1 Answers1

0

In your example you are trying to pass JS variable as argument to PHP function. It is not possible this way, because PHP is server side, and JS is client side (in your case) scripting language. What you can do is on click to calculate "limit" value, pass it using AJAX to "getFibonacci" function in php and to append into html tag returning value. That is one way of interaction between PHP and Front-end JS.

  • Thank you for all your help and answers. I will try to pass it on ajax instead and follow your suggestions. Thanks – KenmacPro Jun 17 '17 at 07:20