Better Way to Manage HTML Tags With PHP

When building web pages or web applications, at some point you have to combine PHP and HTML to manage HTML tags with PHP. This seems complicated because PHP and HTML are two separate languages, but this is not the case. There is a better way to manage HTML tags with PHP. As you know PHP is designed to interact with HTML and PHP scripts can be included in an HTML page without a problem. There are many approaches to write your code. If you are developing a site that is based on Bootstrap, then I have a good trick for you. But first, let’s see our problem.

Table of Contents

  • Problem to manage HTML tags with PHP
  • The solution to managing HTML tags with PHP
  • How to manage HTML tags with PHP
  • Explanation of HTML Code
  • Writing PHP function for HTML Tags
  • Explanation of PHP Code

Problem to manage HTML tags with PHP

Let’s assume that you are working on a website or a web application. You are working on forms and tables and you have to write code to design an input form with many input fields. Then imagine how many lines of code will you write. Yes, you guessed right, hundreds of lines for simple 10 input fields.

The solution to managing HTML tags with PHP

So here is the solution. You will write a function/method with some parameters and bind the HTML tags in PHP script. And then you can use it.

How to manage HTML tags with PHP

First, we will start with some HTML tags. Below is code for a single input field with labels using the bootstrap framework.

<div class="form-group">
        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="f_name">
            First Name
            <span class="required"> * </span>
        </label>
        <div class="col-md-6 col-sm-6 col-xs-12">
            <input type="text" name="f_name" value="Muhammad Faryad" class="form-control col-md-7 col-xs-12" required="" />
        </div>
    </div>

Explanation of HTML Code

In the above code, there is a div tag that will hold the input field and label text. Then a label tag is opened along with label text and with span tag having required symbol/class. Then another div is opened for input fields and in the end, two divs are closed. One for holder div and second is for the input field.

Writing PHP function for HTML Tags

Now let’s move to our main goal. Now we are going to write a PHP function that will hold the HTML tags and then we will use this function as and when needed.

<?php
function html_input($input_name='',$label='',$is_required='yes',$input_type='text',$value='')
{
$is_required=='yes'?$get_required='required=""':$get_required='';
    ?>
   <div class="form-group">
        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="<?php echo $input_name;?>">
            <?php 
            echo get_phrase($label);
            if($is_required=='yes'){
            ?>
            <span class="required"> * </span>
            <?php }?>
        </label>
        <div class="col-md-6 col-sm-6 col-xs-12">
            <input type="<?php echo $input_type;?>" name="<?php echo $input_name;?>" value="<?php echo $value;?>" class="form-control col-md-7 col-xs-12" <?php echo $get_required;?> />
        </div>
    </div>
 
<?php    
}
?>

Explanation of PHP Code

At first glance, it looks scary but no need to worry, nothing is scary. We just wrote a simple PHP function/method named html_input() and passed some parameters. Where $input_name=” is our input name to identify the input field. Next $label=” is the label text. Next is $is_required=’yes’, If this field is required then this parameter will print the required symbol. At second last there is input type field parameter “$input_type=’text'”. If the field is text then no need to change this. However, if the fields are number, email, date or whatever, then the type will be changed. At last, there is $value=”, this is for the value in the input field.

Next, we wrote HTML tags wrapped in PHP code. Now, this function is ready to use. Now just with one line of code, you will get a complete and exact input field as you saw before. Usage of the function is as under:

<?php
echo html_input('f_name','First Name','yes','text','Muhammad Faryad');
?>

As you can see it is much easier than simple HTML tags. In the above PHP function, you just need to pass required parameter values and then echo it and you have done.

You may like to visit our point of sale and inventory management system HERE and School Management System HERE. Furthermore, if you need any kind of help, you can leave a comment below or contact us HERE.

Leave a Reply - I will show after approval