redarrow Posted April 11, 2009 Share Posted April 11, 2009 <?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' ); ?> Quote Link to comment https://forums.phpfreaks.com/topic/153639-dynamic-form-via-function-just-created-fo-free-for-lazy-programmers/ Share on other sites More sharing options...
sKunKbad Posted April 11, 2009 Share Posted April 11, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/153639-dynamic-form-via-function-just-created-fo-free-for-lazy-programmers/#findComment-807331 Share on other sites More sharing options...
redarrow Posted April 11, 2009 Author Share Posted April 11, 2009 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. ); ?> Quote Link to comment https://forums.phpfreaks.com/topic/153639-dynamic-form-via-function-just-created-fo-free-for-lazy-programmers/#findComment-807396 Share on other sites More sharing options...
redarrow Posted April 11, 2009 Author Share Posted April 11, 2009 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. ); ?> Quote Link to comment https://forums.phpfreaks.com/topic/153639-dynamic-form-via-function-just-created-fo-free-for-lazy-programmers/#findComment-807451 Share on other sites More sharing options...
laffin Posted April 11, 2009 Share Posted April 11, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/153639-dynamic-form-via-function-just-created-fo-free-for-lazy-programmers/#findComment-807475 Share on other sites More sharing options...
redarrow Posted April 11, 2009 Author Share Posted April 11, 2009 Thank you, will study that it all fun. cheers. Quote Link to comment https://forums.phpfreaks.com/topic/153639-dynamic-form-via-function-just-created-fo-free-for-lazy-programmers/#findComment-807480 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.