6

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>
2
  • Why don't you just require the file? Commented Jul 26, 2015 at 15:37
  • the script will have database connection details, is not insecure to include/require the php file? Commented Jul 26, 2015 at 15:41

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This did not work for me, it still took me to www.mywebsite.com/myfile.php (blank page)
1

Use javascipt onclick="function()" and leave the form action empty. The function will be performed on the client side but the html CAN be modified

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

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

1 Comment

Could you include an example? this is sounding a bit ajaxy to me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.