The phase summary of Web development (c) JS and PHP Direct Mutual invocation

Source: Internet
Author: User
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
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.