In this post we are going to see a very basic PHP script which might be useful for beginners who wants to learn PHP language.
Let’s create a simple HTML form (index.html):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<html> <head> <title>Simple PHP Script to get Input values from a HTML Form!</title> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous"> </head> <body> <div class="container"> <br /> <h1>Simple PHP Script to get Input values from a HTML Form!</h1> <br /> <form action="formvalues.php" method="post"> <div class="form-group"> <label for="name">Name:</label> <input type="text" class="form-control" name="name" required> </div> <div class="form-group"> <label for="email">Email address:</label> <input type="email" class="form-control" name="email" required> </div> <div class="form-group"> <label for="desc">Description:</label> <input type="text" class="form-control" name="desc" required> </div> <button type="submit" name="submit" class="btn btn-default">Submit</button> </form> </div> </body> </html> |
I have used Bootstrap CSS to make the form look neat.
If you see the above code in the <form> tag action=”formvalues.php” file is not yet created. This is the php file which is going to capture our form values and print it.
Let’s write the PHP code (formvalues.php):
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php //let's check if the form is submitted if(isset($_POST['submit'])){ //now print input values one by one echo 'Form Details:<br>'; print 'Name: '. $_POST['name'] . '<br>'; print 'Email: '. $_POST['email']. '<br>'; print 'Desc: '. $_POST['desc']. '<br>'; }else{ echo "No input"; } ?> |
You can see the demo here: