Calling PHP scripts from Javascript without leaving current page

前端 未结 7 1360
别跟我提以往
别跟我提以往 2021-01-18 14:57

I\'m having some trouble calling PHP scripts from Javascript without leaving the current HTML page (if it is at all possible). I understand it is possible using AJAX, althou

7条回答
  •  没有蜡笔的小新
    2021-01-18 15:42

    PHP is a server-side language. JavaScript is a client-side language.

    If you want to execute server-side code, you don't have the choice to do a round-trip to the server. If you don't want to leave the page, your only option is doing an asynchronous request (aka AJAX).

    Using a JavaScript library such as jQuery or MooTools greatly simplifies that kind of task. For example, you could use MooTools to do a request at the end of your script as such:

    var req = new Request({url: '/backend/doPHPInsert.php'});
    req.send();
    

    There are ways to do so without AJAX by, for example, creating an iFrame dynamically (or any other element that fetches a resource).

提交回复
热议问题