Calling a PHP function from an HTML form in the same file

后端 未结 10 1461
无人共我
无人共我 2020-12-05 05:48

I\'m trying to execute a PHP function in the same page after the user enters a text and presses a submit button.

The first I think of is using forms. When the user s

10条回答
  •  猫巷女王i
    2020-12-05 06:27

    This cannot be done in the fashion you are talking about. PHP is server-side while the form exists on the client-side. You will need to look into using JavaScript and/or Ajax if you don't want to refresh the page.

    test.php

    It will call getResult.php and pass the serialized form to it so the PHP can read those values. Anything getResult.php echos will be returned to the JavaScript function in the result variable back on test.php and (in this case) shown in an alert box.

    getResult.php

    
    

    NOTE

    This example uses jQuery, a third-party JavaScript wrapper. I suggest you first develop a better understanding of how these web technologies work together before complicating things for yourself further.

提交回复
热议问题