Calling a particular PHP function on form submit
Asked Answered
F

8

46

I was trying to call a particular php function in submit of a form both the form and php scripts are in same page. My code is below.(it is not working and so I need help)

<html>
    <body>
    <form method="post" action="display()">
        <input type="text" name="studentname">
        <input type="submit" value="click">
    </form>
    <?php
        function display()
        {
            echo "hello".$_POST["studentname"];
        }
    ?>
    </body>
</html>
Flank answered 8/9, 2012 at 5:45 Comment(0)
M
80

In the following line

<form method="post" action="display()">

the action should be the name of your script and you should call the function, Something like this

<form method="post" action="yourFileName.php">
    <input type="text" name="studentname">
    <input type="submit" value="click" name="submit"> <!-- assign a name for the button -->
</form>

<?php
function display()
{
    echo "hello ".$_POST["studentname"];
}
if(isset($_POST['submit']))
{
   display();
} 
?>
Melicent answered 8/9, 2012 at 5:49 Comment(4)
Error : The requested URL /path/to/update() was not found on this server.Pyroxylin
@MadanBhandari Don't write the name of function in the method of the form.Betelgeuse
If you write name of function in action, it looks the this in root directory and didn't go to if(isset) statement. Kindly remove the function name.Tullis
@SalmanMushtaq, have you read the answer properly, the function name is not in action, <form method="post" action="yourFileName.php">.Melicent
S
10

you don't need this code

<?php
function display()
{
echo "hello".$_POST["studentname"];
}
?>

Instead, you can check whether the form is submitted by checking the post variables using isset.

here goes the code

if(isset($_POST)){
echo "hello ".$_POST['studentname'];
}

click here for the php manual for isset

Sinuate answered 8/9, 2012 at 5:54 Comment(2)
Wouldn't this execute the function on refreshing the page? Not that on this case is dangerous, but I am doing a save function.Rule
This will echo null values when refreshing, better use isset($_POST['submit']Pectize
S
6

Assuming that your script is named x.php, try this

<?php 
   function display($s) {
      echo $s;
   }
?>
<html>
    <body>
        <form method="post" action="x.php">
            <input type="text" name="studentname">
            <input type="submit" value="click">
        </form>
        <?php
           if($_SERVER['REQUEST_METHOD']=='POST')
           {
               display();
           } 
        ?>
    </body>
</html>
Skivvy answered 8/9, 2012 at 5:56 Comment(0)
V
2

PHP is run on a server, Your browser is a client. Once the server sends all the info to the client, nothing can be done on the server until another request is made.

To make another request without refreshing the page you are going to have to look into ajax. Look into jQuery as it makes ajax requests easy

Venose answered 8/9, 2012 at 5:47 Comment(0)
D
0

If you want to call a function on clicking of submit button then you have
to use ajax or jquery,if you want to call your php function after submission of form you can do that as :

<html>
<body>
<form method="post" action="display()">
<input type="text" name="studentname">
<input type="submit" value="click">
</form>
<?php
function display()
{
echo "hello".$_POST["studentname"];
}
if($_SERVER['REQUEST_METHOD']=='POST')
{
       display();
} 
?>
</body>
</html>
Development answered 8/9, 2012 at 5:49 Comment(0)
L
0

Write this code

<?php
    if(isset($_POST['submit'])){
        echo 'Hello World';
    } 
?>

<html>
     <body>
         <form method="post">
             <input type="text" name="studentname">
             <input type="submit" name="submit" value="click">
         </form>
     </body>
</html>
Languet answered 11/3, 2017 at 9:54 Comment(0)
S
0

An alternative, and perhaps a not so good procedural coding one, is to send the "function name" to a script that then executes the function. For instance, with a login form, there is typically the login, forgotusername, forgotpassword, signin activities that are presented on the form as buttons or anchors. All of these can be directed to/as, say,

weblogin.php?function=login
weblogin.php?function=forgotusername
weblogin.php?function=forgotpassword
weblogin.php?function=signin

And then a switch statement on the receiving page does any prep work and then dispatches or runs the (next) specified function.

Selfregard answered 5/12, 2020 at 13:48 Comment(0)
N
0

I do it with select and option. Each option-value contains the path to a php-file which contains the function to run ánd the call to that function:

function myFunction() {
    do something
}

and:

myFunction();

For every function that I want in my options I make a separate file containing that function and the call to it.

Now I declare $_POST['optionvalue'] to a variable which I called $function.

And somewhere down the page I have an article which contains:

if isset($_POST['functions']) {
include $function;
}

And the function will be executed there (like an echo or whatever (I show arrays that are created by the functions)).

I know: everything needs to be validated if you have others use it too, which I don't, I use this only on localhost.

Nuss answered 13/10, 2023 at 15:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.