Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML call PHP function on Submit without leaving the page

Tags:

html

post

php

Hello Everyone

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

/public_html/scripts/phpfunctions.php

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

And I have my 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 navigates the browser to that file instead of executing the script in my current file.

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

Is it possible to call a php function from inside a php script after pressing the submit button (the way it would normally execute in 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>
like image 365
TheLovelySausage Avatar asked Jul 26 '15 15:07

TheLovelySausage


People also ask

How to call function on submit button in PHP?

Calling a PHP function using the HTML button: Create an HTML form document which contains the HTML button. When the button is clicked the method POST is called. The POST method describes how to send data to the server. After clicking the button, the array_key_exists() function called.

How do I stay the same page after submitting PHP?

In order to stay on the same page on submit you can leave action empty ( action="" ) into the form tag, or leave it out altogether. For the message, create a variable ( $message = "Success! You entered: ". $input;" ) and then echo the variable at the place in the page where you want the message to appear with <?

How run PHP form submit?

Use isset() method in PHP to test the form is submitted successfully or not. In the code, use isset() function to check $_POST['submit'] method. Remember in place of submit define the name of submit button. After clicking on submit button this action will work as POST method.


1 Answers

HTML call PHP function on Submit without leaving the page

Yes, just don't use action and the form will be submitted to the current page.

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

To call a PHP function, check if it is submitted, then call the function

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

Is it possible to call a php function from inside a php script after pressing the submit button (the way it would normally execute in a page like the one below)?

No, because the submit button is an HTML element and when you click on it, you will need JavaScript (e.g. via Ajax) to send it to the server (PHP) and then the PHP page on the server can execute the function needed.

You may use something like <form method="POST" onsubmit="myJSFunction()">. The JavaScript function myJSFunction() is called when you click on the submit button, using this function you may send the required data to the server (e.g. via ajax) and execute your PHP function there on the server.

like image 187
Suman Banerjee Avatar answered Oct 02 '22 14:10

Suman Banerjee