There is a functiona ($ a) {}echo & quot; & amp; lt; inputtypesubmitonclick to call this function & amp; gt; & quot; how to write ?? Why can't I write and execute it? There is a function a ($ ){
}
Echo""
How to write ??
Why can't I write and execute it?
Reply content:
There is a function a ($ ){
}
Echo""
How to write ??
Why can't I write and execute it?
HTML cannot use PHP Functions
Echo is a piece of html text on the page. At this time, the () triggered by input. submit is a javascript function in the page. The a () defined in php cannot be called.
Onclick is an event in the browser that can call a JS function, but cannot call a PHP function, because the PHP interpreter is on the server. so you can call a JS function after onclick. This JS function sends an AJAX request to the PHP server. Then you can write a PHP function to respond and return data to the browser. we recommend that you use the $. post () and $. get.
Clear frontend and backend points,
If you want the client to call the function only when you click it,
Then the function must be written as js. The onclick interface calls the js function,
If the server runs the function, the content in onclick is fixed when it is loaded on the page,
Php is executed on the server and javascript is executed on the client. Although the Code is written together, the execution time is different.
You can use php to output javascript on the page to assign values to the front-end variables.
You can also use javascript to request url interfaces to execute php encapsulated methods on the backend.
The execution sequence of the code is to first execute the PHP code on the server, and the PHP outputs the String type HTML code to the client. At this time, after PHP is executed, it cannot execute any PHP code, then the next thing will be handed over to the browser, JS, CSS, etc. for rendering and parsing...
You can use JS to port your PHP function in HTML and then execute the JS function, for example:
/*HTML*/
/*JS*/function test(a){ console.log(a);}
Generally, html functions are called in js.
If the subject has read the previous answer, the frontend script and backend script are distinguished.
So the current demand should be implemented using ajax
There is no php code in the browser. Php first parses the file and generates the html file. Then, the browser obtains no php code in the html file. Of course it cannot be executed.
echo "";
This should be the case.
When the input type in form is submit, click to submit this form. You can change type = 'submit 'to type = 'button'