In the Company project transformation, often encounter JS and PHP function intermodulation situation, and actually JS and PHP designers do not advocate the two languages directly call, one is the client language, a service-side language, the interaction between the two often rely on the AJAX mechanism, get (), post () For data transfer. And in both the direct invocation, there are often the following limitations:
1, first, the mutual tune between PHP and JS can only be in the same file, the call to other files will fail, whether it is PHP or JS.
2, the call between PHP and JS can only be for the function, and the variables can not be referenced, that is, in the " " PHP can not reference JS variables, in contrast to "" JS can not reference PHP variables. And through the online search said can achieve intermodulation, there may be browser-compatible issues, I use the browser is Google Chrome (version 37.0.2062.103 m), which also shows from the side of the direct interaction between PHP and JS is prone to bug.
3, in the function call to have the return value, PHP through "echo" or "return" the value returned, and JS through "document.write ()" or "return" to return the value.
4, when the function is called, it is necessary to use a variable to pass back the value of the function to save. Example: In js, var y = " "; $phpB = "" in PHP; Note: Remember to add "" double quotation marks to the function call.
Attach some code:
"; echo "PHP---call---show".
"; PHP call the same file PHP function $phpA = Phptest (); echo "$phpA". "
"; PHP use the same file JS function $phpB = ""; echo "$phpB". "
"; PHP Use external file php function $phpC = phpcalloutfilephp (); echo "$phpC". "
";? >
Full code download Address: http://download.csdn.net/detail/aba13579/7874961