HTML call to PHP function when submitted without leaving the page

Hello everybody

I am trying to make some of my php scripts multiple by storing them in a separate file on the server.

/public_html/scripts/phpfunctions.php

<?php echo 'This function has been called' ?>

And I have an HTML form

<form action="scripts/phpfunctions.php" method="post">
    <input type="text" name="user_input">
    <button type="submit">Submit</button>
</form>

Now that the function is a separate file, it translates the browser into this file instead of running the script in my current file.

Normally I would solve this using AJAX and call the php function instead of the php page.

Is it possible to call the php function from within the php script after clicking the submit button (the way it would normally be done on a page like the one below).

<!DOCTYPE html>
<head><title>No Title</title></head>
<?php
    if($_SERVER['REQUEST_METHOD'] == 'POST') {
       echo 'function has been called'
    }
?>
<body>
    <form action="index.php" method="post">
        <input type="text" name="user_input">
        <button type="submit">Submit</button>
    </form>
</body>
</html>
+4
source share
2 answers

HTML call to PHP function on Submit without leaving the page

, action, ​​ .

<form method="POST">
    <!-- your input tags -->
</form>

PHP, , ​​ ,

if(isset($_POST['user_input'])){
   myFunction(); //here goes the function call
}

php php script ( , )?

, HTML, JavaScript (, Ajax), (PHP), PHP .

- <form method="POST" onsubmit="myJSFunction()">. JavaScript myJSFunction() , , , (, ajax) PHP .

+3

javascipt onclick = "function()" . , html CAN

<button type="submit" onclick="action()">Submit</button>

<script>
     function action() {
         //Your code here
     }
</script>
0

Source: https://habr.com/ru/post/1599736/


All Articles