Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling a particular PHP function on form submit

Tags:

php

isset

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> 
like image 507
Piklu Guha Avatar asked Sep 08 '12 05:09

Piklu Guha


People also ask

How do you call a function on submit of form 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 you call a function on form submit?

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.

How do you call a function in PHP?

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 );

Can I call function in function PHP?

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.


1 Answers

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(); }  ?> 
like image 182
The Alpha Avatar answered Sep 23 '22 07:09

The Alpha