Dynamic form field creation in PHP


In some cases we need to create dynamic fields in a form. Like text box, select box or check box etc. the dynamic field creations will depends upon the previous field value basically.

Dynamic form field creation using PHP and Ajax
Click image to view Larger
So in this example I will explain you how to create a dynamic form field for a TV company. Below image will explain you this concept.

Dynamic form creation will be working in the basic Ajax principle. You can read “basic Ajax and PHP” article to get more idea. This type of dynamic form creation system is working on Ajax PHP bases. Find the example file and demo below.

Download FileDemo

Below is the Ajax script HTML form page:

function getXMLHTTP()
{
var xmlhttp=null;
try {
xmlhttp=new XMLHttpRequest();
}
catch(e)
{
try {
xmlhttp=new ActiveXobject("Microsoft.XMLHTTP");
}
catch(e)
{
try {
xmlhttp=new ActiveXObject("msxml2.XMLHTTP");
}
catch(e1)
{
xmlhttp=false;
}
}
}
return xmlhttp;
}
var strurl="dynamic-form.php?cate="+cat;
var req=getXMLHTTP();
function getCat(cat) {
//alert(cat);
$("#flash").show();
$("#flash").fadeIn(400).html(' loading.....');
var strurl="dynamic-form.php?cate="+cat;
//alert(strurl);
var req=getXMLHTTP();
if(req==null)
{
alert("browser error");
}
if(req)
{
req.onreadystatechange=function() {
if(req.readyState ==4 || req.readyState=="complete") {
$("#flash").hide();
document.getElementById("ajaxresult").innerHTML=req.responseText;
}
}
req.open("GET",strurl,true);
req.send(null);
}
}

HTML form:


PHP script for the above HTML form:


$cat=$_REQUEST['cate'];
if($cat==3)
{

echo '
Sub category :
'; }

Now you have to make PHP file which will show the dynamic select box related to parent select box. So while you selecting the one particular option in parent select box the Ajax will get the value and send it to the PHP. In PHP we are checking for on particular value. If it satisfied PHP will fetch the relevant record from database and print it in a select box.

You can also use this Ajax PHP system to various web development processes as above. For e.g. dynamic select box, dynamic form submission, dynamic image upload, find online user etc.

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>