How to submit a form in PHP?
|While doing programming, forms are very common to implement on any website. When you sign up or create an account on any job site then you need to fill the form. Form data can be sent on email id or we can store form data in the database. This totally depends on the form of data requirement.
Today I will show an example, how to create a form and send form data to an email id. Below form have few fields but our main objective will be to create a smart HTML form so that we should not write too much PHP code.
<section class="formSection row">
<div class="col-sm-12 col-md-12 col-lg-12">
<div class="sectionTitle">
<h2>Sample FORM</h2>
</div>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"
<div class="col-xs-12 col-sm-6 col-md-6 col-lg-6 pl0">
<div class="form-group">
<label for="com_name" class="introLabel">Company Name:</label>
<input class="form-control introInput" name="Temp[Company Name]" type="text" >
</div>
<div class="form-group">
<label for="first_name" class="introLabel">First Name:</label>
<input class="form-control introInput date_range_filter" type="text" name="Temp[Owner First Name]" >
</div>
<div class="form-group">
<label for="last_name" class="introLabel">Last Name:</label>
<input class="form-control introInput" type="text" name="Temp[Owner Last Name]" >
</div>
<div class="form-group">
<label for="emp_count" class="introLabel">Employee Count:</label>
<input class="form-control introInput" type="text" name="Temp[Employee Count]" >
</div>
</div>
<input type = "submit" name = "submit" value = "Submit">
</form>
</div>
</section>
We can see that in the above form, we are using an Array Temp for the name field. Usually, we don’t use array in name field but as we are not storing data in the database and just sending form data to email id so it’s good to use Array with proper form field name to represent Form field name as Label in the email body.
This method usually used with lengthy form that have many fields and it’s time consuming when to keep each form filed value in a PHP variable.
In PHP, isset() method is used to test if the form is submitted successfully or not. Remember that submit is Submit button name. After clicking the submit button action will work as the POST method.
if (isset($_POST['submit'])) {
echo "form success";
}
PHP Code Block –
if (isset($_POST['submit'])) {
foreach ((array)$_POST['Temp'] as $key=> $value) {
$content.= "$key: $value";
}
}
Above, we have validated that form is submitted and run a foreach loop on Temp array and stored form field array name value as Email content’s form filed label and value is form field filled value.
Now we have email content in $content variable and we are one click away to send an email. Let’s see below how to send form field content by simple PHP mail function.
$to = "gaurav17kumar@gmail.com";
$subject = "Sample Email";
$message = $content;
$header = "From:test@ecomspark.com \r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-type: text/html\r\n";
$email= mail ($to,$subject,$message,$header);
if( $email== true ) {
echo "Message sent successfully...";
}else {
echo "Message could not be sent...";
}
Final Code Block –
<?php
if (isset($_POST['submit'])) {
foreach ((array)$_POST['Temp'] as $key=> $value) {
$content.= "$key: $value";
}
$to = "gaurav17kumar@gmail.com";
$subject = "Sample Email";
$message = $content;
$header = "From:test@ecomspark.com \r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-type: text/html\r\n";
$email= mail ($to,$subject,$message,$header);
if( $email== true ) {
echo "Message sent successfully...";
}else {
echo "Message could not be sent...";
}
}
?>
<section class="formSection row">
<div class="col-sm-12 col-md-12 col-lg-12">
<div class="sectionTitle">
<h2>Sample FORM</h2>
</div>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"
<div class="col-xs-12 col-sm-6 col-md-6 col-lg-6 pl0">
<div class="form-group">
<label for="com_name" class="introLabel">Company Name:</label>
<input class="form-control introInput" name="Temp[Company Name]" type="text" >
</div>
<div class="form-group">
<label for="first_name" class="introLabel">First Name:</label>
<input class="form-control introInput date_range_filter" type="text" name="Temp[Owner First Name]" >
</div>
<div class="form-group">
<label for="last_name" class="introLabel">Last Name:</label>
<input class="form-control introInput" type="text" name="Temp[Owner Last Name]" >
</div>
<div class="form-group">
<label for="emp_count" class="introLabel">Employee Count:</label>
<input class="form-control introInput" type="text" name="Temp[Employee Count]" >
</div>
</div>
<input type = "submit" name = "submit" value = "Submit">
</form>
</div>
</section>
For any query/suggestion, please write in the comment section. Thanks for reading.