Run PHP code inside JavaScript? Would it be ok to use?

I'm not sure if a PHP string can be run inside a JavaScript function. For instance:

<script language=javascript>
var int=self.setInterval("message()",1000);
function message()
  {
<?PHP mysql_query("SELECT * FROM example"); ?>
  }
</script>

I have not tried to run this, but I do not think you can run PHP like this. Can anyone help. Can I run a PHP script inside a javascript function in some way without using an external PHP file call through Ajax?

+3
source share
4 answers

Tell us about the basics :)

PHP - , -. -, , PHP , . , : -.

Javascript - , -. , , . Javascript - -: .

, Javascript , Javascript HTTP- . , AJAX. PHP - , PHP script Javascript, , PHP json_encode, - jQuery $.getJSON .

, , . , , , ... !

+12

. - PHP, PHP HTML-, JavaScript JavaScript. , JavaScript.

JavaScript = .

PHP = .

+5

- , JavaScript .

, message , JavaScript eval() .

+2

- php. , . php javascript templating, ajax.

I would use the jQuerys.ajax () function on the javascript side and echo json_encode($result_array)on the php side.

+1
source

Source: https://habr.com/ru/post/1769655/


All Articles