Can I use PHP in JavaScript to connect to MySQL? -Php Tutorial

Source: Internet
Author: User
Tags what php
My current code is like this & amp; lt; html & amp; gt; & amp; lt; head & amp; gt; & amp; lt; title & amp; gt; untitledDocument & amp; lt; title & amp; gt; {code ...} & amp; lt; head & amp; gt; & amp; lt; body & amp; gt; & amp; lt; inputtype & quot; button & quot; onClick & quot; javascript: test (); & quot; & amp; gt; my current code is like this



Untitled Document

《script》    function test(){        
   {$result} 

>"; ?> }《script》




But this is wrong.
What should I do?

Reply content:

My current code is like this.



Untitled Document

《script》    function test(){        
   {$result} 

>"; ?> }《script》




But this is wrong.
What should I do?

After php is executed on the backend, it is returned to the front-end, and the php code is invisible to the front-end. Do not believe you right-click to view the source code?

A simple method is to write a php file and call it using ajax at the front end. I feel that you lack a lot of knowledge. I don't suggest learning php first. it is easy to learn. You can first learn about C # or Java website development and then come back to learn about php and then feel what php has done.

When the subject asks this question, you do not understand B/s or HTTP.
Each time a browser sends a request, the server processes the request and returns the result to the browser. Only relevant resource files (such as HTML, CSS, JS, and images) can be viewed at the front end, and the server-side code cannot be seen.

To run the code on the server, you must send a request to the server through a browser, such as opening a page, submitting a form, and AJAX.

It seems like you want to send an asynchronous request on the browser side, you can implement it through AJAX.

PHP is parsed on the server side, while js is parsed on the browser. The php code segment sends the html file to the browser only after it is run on the server side, at this time, it has nothing to do with the trigger button. we recommend that you use $ According to your actual needs. get (), $. post (), $. ajax () these encapsulated ajax functions can easily meet your needs.

The error is caused by your PHP code. The final output content is not JavaScript code.
So the error.



《script》 function test(){ {$result}

>"; ?> }《script》

The final output content of your PHP code block may be
"

Xxxxxx

"'
Is HTML, not JS code, so the error ..

There is a method called rpc that is exactly what you need

This is not the case. the frontend and backend are separated.

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.