Calling a particular PHP function on form submit

Piklu Guha picture Piklu Guha · Sep 8, 2012 · Viewed 233.5k times · Source

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>

Answer

The Alpha picture The Alpha · Sep 8, 2012

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();
} 
?>