How can I execute a PHP script with a Javascript variable on a Js button click? [closed]

筅森魡賤 提交于 2019-12-14 04:08:46

问题


I would like to run a PHP segment of code with veriables from a Javascript text field while using that veriable in the PHP function. All of that should be executed with a Javascript button click.

[Js Text] --> Js button click --> veriable --> PHP (MySQL DB)

An attached sample code would be grand.

Thanks.


回答1:


KEYWORD: AJAX (Asynchronous JavaScript and XML). With jQuery you can perform a POST request to a PHP script and receive a response.

$.post("test.php", { name: "John", time: "2pm" })
    .done(function(data) {
        alert("Data Loaded: " + data);
    });

See: http://api.jquery.com/jQuery.post/

PS: In your PHP you can use: $_POST['name']/$_POST['time'] to request these variables.




回答2:


use ajax...if you are using jquery check http://api.jquery.com/jQuery.ajax/ and for running the ajax event look for the click event @ http://api.jquery.com/click/




回答3:


As far as I know, there's no way to execute php by js, because the process of interpreting a php webpage is as follows:

php server ==generate==> html files ==webbrowser execute==> JS

You just cannot go back to the first stage in js. Also the js is executed at your browser rather than the server.

To do your work, you should write a new php page that execute the desired action and call this page using JS.

Also, I assume you are just calling Database, and you can definately find other ways to link to database via JS.



来源:https://stackoverflow.com/questions/16922687/how-can-i-execute-a-php-script-with-a-javascript-variable-on-a-js-button-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!