PHP Script to get input values from a HTML form!

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

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

<?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:

demo

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top