0

I need to put echo as condition of if statement. So, for example:

<html>

...
...

<body>

  <form onsubmit"<?php $c=false; if(echo "<script>example();</script>";){ $c=true; } echo "\""; if($c){ echo "action=\"./\"";} method="post">
    ...
 </form>

...
...

<script>
 function example()
 {
   alert("This is only an example");
   if(..) return true;
   return false;
 }
</script>

</body>
</html>

So I need to create a function in JavaScript (this top is only an example, but, in realty, it is more complicated) and call it in the if statement.

Update The code should be execute "action" of the form only if the Javascript function is true.

MrMe
  • 59
  • 7

1 Answers1

0

PHP runs in the server. JavaScript runs in the client. So php can't use a js variable or its function to server side action. What u can do is , u can call JS function from PHP like

echo "<script> myFunction() </script>";

But in the same same case, u can use php values for JS actions, eg

<script>
var x = <?php echo $myVar ?>
if(x == true){ 
   alert("yahooo"); 
}
</script>
Aravind Pillai
  • 739
  • 7
  • 21
  • 1
    Actually, this won't "call" the js-function. It will just print that string out, send it to the client that will parse it and execute the function. It's a big big difference. – M. Eriksson Jan 09 '17 at 12:08
  • @Sav - Like multiple people already told you, _you can **not** call javascript functions from PHP_. PHP is executed on the server, returning the result to the browser that will execute any javascript. – M. Eriksson Jan 09 '17 at 12:10
  • @MagnusEriksson : Yea that will call the function, but no use.. It will jus print only ..(as of my knowledge) – Aravind Pillai Jan 09 '17 at 12:11
  • No, the javascript engine in the browser will _call_ the function. This will just print the string. Call and print are two totally different things. – M. Eriksson Jan 09 '17 at 12:12
  • @MagnusEriksson I understand... and for this I asked this question... I need to check if the JS function is "true" or "false" from PHP. Is it possible? – MrMe Jan 09 '17 at 12:12
  • @Sav - Again, no! When your javascript is executed on the _client_, all PHP code has already been executed on the _server_. These are two totally different environments. – M. Eriksson Jan 09 '17 at 12:14
  • @MagnusEriksson Perfect, but Is there a method that do that I would do? **Update** So, I could put PHP code (includes the form...) in JS code? – MrMe Jan 09 '17 at 12:15