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>
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.
You can put your form validation against this event type. The following example shows how to use onsubmit. Here we are calling a validate() function before submitting a form data to the webserver. If validate() function returns true, the form will be submitted, otherwise it will not submit the data.
There are two methods for doing this. One is directly calling function by variable name using bracket and parameters and the other is by using call_user_func() Function but in both method variable name is to be used. call_user_func( $var );
So as long as you have your function defined when you hit a certain spot in your code it works fine, no matter if it's called from within another function. Show activity on this post. If you define function within another function, it can be accessed directly, but after calling parent function.
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(); } ?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With