the 2 easy ways to create dynamic form inputs in php

these are 2 ways you can create dynamic form inputs without writing no more a bunch of html codes like you usually do

  1. php foreach function
  2. php for function

1. create dynamic form inputs with php foreach() function

this function for creates a dynamic form inputs with just defining the labels for each input separated with a comma.

then it uses php explode() function to convert the given labels into array which is iterated with php foreach() to give us the a dynamic form inputs.

DEMO 1

Download Resource file

 <?php
function dynamicinputs($formtitle,$formfields,$formsubmit){
	
	$formfields = explode(",", $formfields);
	$count = 1;
	$inputs = "";
	if(count($formfields)>0){
	if(empty($formtitle)){
		$formtitle = 'Form Title';
	}
	echo '<h3 class="mb-2">'.$formtitle.'</h3><form method="post">';
	foreach($formfields as $val){
		$name = str_replace(" ", "", $val);
		
		$inputs = '<label class="form-label" style="display:block;">'.$val.'</label><input type="text" class="form-control mb-2" name="'.$name.'" placeholder="'.$val.'">';
		echo $inputs;
	}
	if(empty($formsubmit)){
		$formsubmit = 'Form Button';
	}
	echo '<input class="btn btn-sm btn-primary mb-3" type="submit" name="'.str_replace(" ", "", $formsubmit).'"  value="'.$formsubmit.'"></form>';
	}else{
		echo '<div class="alert alert-danger">No label / field detected or defined</div>';
	}

}
?>

to output

 <?php dynamicinputs('Form Title', 'Label 1, Label 2, Label 3, Label...', 'Form button value'); ?>

Example: to create dynamic form inputs for building contact form

We want to build a contact form with fields such as fullname, phone, email, message

 <?php dynamicinputs('Contact form', 'fullname, phone, email, message', 'Send Message'); ?>

the above code will output a beautiful contact form as shown below

to create dynamic form inputs for contact form
picture 1 – contact form built with dynamic function

2. dynamic form inputs with php for() function

we are only going to change the foreach() function in the 1st function with for() function

this one comes with a dynamic variable if defined

DEMO 2

Download resource file

 <?php
function dynamicinputs($formtitle,$formfields,$formsubmit, $dynamic_variable){
	
	$formfields = explode(",", $formfields);
	$count = 1;
	$inputs = "";
	if(count($formfields)>0){
	if(empty($formtitle)){
		$formtitle = 'Form Title';
	}
	echo '<h3 class="mb-2">'.$formtitle.'</h3><form method="post">';
	for($i = 0; $i <= count($formfields)-1; $i++){
		$name = "".$dynamic_variable."".$i+1 ."";
		if(empty($dynamic_variable)){
			${"variable{$i}"} = str_replace(" ", "", $formfields[$i]);
			$name = ${"variable{$i}"};
		}
		
		$inputs = '<label class="form-label" style="display:block;">'.$formfields[$i].'</label><input type="text" class="form-control mb-2" name="'.$name.'" placeholder="'.$formfields[$i].'">';
		echo $inputs;
	}
	if(empty($formsubmit)){
		$formsubmit = 'Form Button';
	}
	echo '<input class="btn btn-sm btn-primary mb-3" type="submit" name="'.str_replace(" ", "", $formsubmit).'"  value="'.$formsubmit.'"></form>';
	}else{
		echo '<div class="alert alert-danger">No label / field detected or defined</div>';
	}

}
?>

to output

 <?php dynamicinputs($thetitle, $thefields, $thebutton, $theitem); ?>
 <?php dynamicinputs('Form Title', 'field 1, field 2, field 3, field... ', 'form button', 'itemize'); ?>

for example to create a contact form with itemized fields if defined

 <?php dynamicinputs('Contact Us', 'fullname, phone, email, message', 'send raven', 'field'); ?>

the above will output a nice looking contact form with names of the fields itemized as shown in picture 1 above

When you view the source code of the form produced by the second function you will notice that each form field has a name itemized as field1, field2, field3, field4

Related

How TO create a Login Form (w3schools vs bootstrap)

Every transactional website should have a login form that...

Best PHP checkdate alternative: How to do date validation in php (exclusive)

this checkdate alternative special function validates date in such...

How to validate date of birth today & get the age from the birthday using php

we have already explained how you can get age...

How to use php to check age for birthday messages

We going to check age for birthday messages using...

How to check product expiration duration of any product using php (2 proven function)

with this simple php trick you can check expiration...