Have you ever wanted to put a form in a php function? You can do it and it is fast and easy. You can use this function anywhere on your website. 


Steps to Creating a form in a PHP Funtion

First create 2 files, index.php and functions.php. These files will work together to get the job done.

Functions.php File

In your functions.php file put in the code example beow for this exercise.
<?php

function form(){
echo '
<form method="post" action="">
<input type="text"name="firstName"/>
<input type="submit" name="submit" value="submit"/>
</form>

';
}

?>

Index.php File

In your index.php file put the following lines of code. Make sure that your functions.php file and your index.php file are in the same folder.
<?php if(isset($_POST['submit'])){ echo 'The Name Entered is'.$_POST['firstName']; } include('functions.php'); form(); ?>
Congratulations, you have now learned how to put a form in a php function. Feel free to comment below.


Sign Up To Comment