Jump to content

dynamic form via function (just created fo free for lazy programmers.


Recommended Posts

<?php

function form_creater($form_action,$number_of_fields,$input_type,$field_names,$button_name,$button_value){

$f=explode(',',$field_names);

$form= "<center> <form method='POST' action='$form_action'>";

    for($i=0; $i<$number_of_fields; $f++, $i++){


	$form.="{$f[$i]}<br><input type='$input_type' name='{$f[$i]}'><br><br>";
}

$form.="<input type='submit' name='$button_name' value='$button_value'></center>";


return($form);
}



echo form_creater(
$form_action=' ',
$number_of_fields=3,
$input_type='text',
$field_names="name,username,password",
$button_name='submit',
$button_value='Send'
);

?>

It is a good start, but lacks true labels, and should include other form elements, such as textarea, checkboxes, selection boxes, radio buttons, etc. Also, if the form doesn't pass validation, then you might consider prefilling the form fields when the user gets directed back to the form.

yes agree let's improve this one please..

 

<?php session_start();

// if post submit the form.

if($_POST['submit']){


// post the form entrys.

$name=$_POST['name'];
$username=$_POST['username'];
$password=$_POST['password'];


// echo form entry.

echo " name: $name\n <br> username: $username\n  <br> password: $password\n  ";


// create a array to catch post's

$catch_posts[]=$_POST['name'];
$catch_posts[]=$_POST['username'];
$catch_posts[]=$_POST['password'];


}

// function name.

function form_creater($form_action,$number_of_fields,$input_type,$field_names,$button_name,$button_value,$catch_posts){

// explode the field names.

$f=explode(',',$field_names);

// create the form.

    $form= "<center> <form method='POST' action='$form_action'>";

    // for loop throw variables.
    
    for($i=0; $i<$number_of_fields; $f++, $catch_posts++, $i++){
    	
    	// continue with the form, with the amount off inputs needed.
    	
    $form.="{$f[$i]}<br><input type='$input_type' name='".$f[$i]."' value='{$catch_posts[$i]}'><br><br>";
}

// end the form .
$form.="<input type='submit' name='$button_name' value='$button_value'></center>";

// return the function.

return($form);
}



// echo function and set settings.

echo form_creater( //call the function.
$form_action=' ', // set the form action to pst to.
$number_of_fields=3, // set number of form fields.
$input_type='text', // set type of form.
$field_names="name,username,password", // crete the form names.
$button_name='submit', // name the submit button.
$button_value='Send', // name the value off the submit button.
$catch_posts // leave this, it tell the function to catch form info.
);

?>

 

 

only got so far on this.

 

select box, text area, text,

 

<?php session_start();


// function name.

function form_creater($form_action,$number_of_fields,$field_names,$button_name,$button_value,$catch_posts,$form_type,$select_name){


// explode the field names.

$f=explode(',',$field_names);

// create the form.

    $form= "<center> <form method='POST' action='$form_action'>";

    
    // if text is selected.
    
    if( isset($form_type) && ($form_type=='text')){    	
    
    	
    	// for loop throw variables.
    
    for($i=0; $i<$number_of_fields; $f++, $catch_posts++, $i++){
    	
    	// continue with the form, with the amount off inputs needed.
    	
    $form.="{$f[$i]}<br><input type='text' name='".$f[$i]."' value='{$catch_posts[$i]}'><br><br>";

    }
    
    // if select_box selected.
    
    }elseif(isset($form_type) && ($form_type=='select_box')){
     	

    	// create a select box.
    	$form.= "<select name='$select_name'>";
     	
     	// default saying.
    	
    	$form.="<br><option value=''>please select one</option><br><br>";
     	
     	  for($i=0; $i<$number_of_fields; $f++, $catch_posts++, $i++){
    	
    	// continue with the form, with the amount off selects needed.
    	
    $form.="<br><option value='".$f[$i]."'>{$f[$i]}</option><br><br>";
}

// end select statement.
     	
     	$form.= "</select><br><br>";
     	
     	// if textarea is selected.
     	
     }elseif(isset($form_type) && ($form_type=='textarea')){
     	
     	  
     	// number of textarea's needed.
     	
     	for($i=0; $i<$number_of_fields; $f++, $catch_posts++, $i++){
    	
    	// continue with the form, with the amount off textarea needed.
    	
    $form.="$f[$i]<br><textarea name='".$f[$i]." $text_area_cols $text_area_cols'></textarea><br><br>";
}
     	
     }
// end the form .
$form.="<input type='submit' name='$button_name' value='$button_value'></center>";

// return the function.

return($form);
}



// echo function and set settings.

echo form_creater( //call the function.
$form_action=' ', // set the form action to post to.
$number_of_fields=4, // set number of form fields.
$field_names="male,female,gay,bio", // create the form names.
$button_name='submit', // name the submit button.
$button_value='Send', // name the value off the submit button.
$catch_posts, // leave this, it tell the function to catch form info.
$form_type='textarea', // the type of form // text or //select_box or // textarea
$select_name='redarrow', // for select box only.
$text_area_cols='50', // textarea cols  only.
$text_area_rows='50' // textarea rows only.
);

?>

I've done something similar to this a number of years ago.

Had a couple of different form fields.

validation and stuff

 

it still was a bit rough, but I left it as is as it did wut it did magnificently.

I made it to maintain a config file.

 

u can browse the source at sourceforge tbdev.net

 

It shud provide some ideas on creating a more robust form creator :)

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.