Good day. I have a php condition. And you need if this condition worked then the JS function was activated

if($num == "2") { Срабатывает функция JS } 

JS function itself

 function test12(){ $.ajax({ type: 'POST', url: 'myscript3.php', data : { OKEY3: 'OKTI3' }, success: function(data) { } }); }; 

I tried to put JS in a condition but it still did not work.

  • The given JS code is not enough for something to happen. You just defined a function ... Is there a call to this function? - Vorobyev Alexander

2 answers 2

PHP runs on the server side, JS runs on the visitor side, and there is no direct connection between them. PHP only generates a page. all ... The only approximate opportunity to solve your problem (without looking, that in itself it is wrong). Generate page according to condition

 <?if ($num==2):?> <script> function test2(){ .... }; test2(); </script> <?endif?> 

Those. The script is displayed on the page, only if the condition is met.

But I would, in your place, have thought about the correctness of the formulation of the problem.

    like this

     <?if($num == "2"){?> <script> test12(); </script> <?}?>