eire web design home page contact eire-web design eire web design and development site map
stepping on in business
• Welcome to Eire Web Design Ireland

Posts Tagged ‘PHP create form fields function’

PHP Form creation and basic validation

Saturday, January 2nd, 2010

As a developer and web designer,  the most frustrating part I have found  is when working with forms.
Long time ago (about 2 years now) I start putting togheter few PHP function trying to make my life a lot easier, but never got the time to finish it off.

Finally I have put everything on the side and got cracking on this due to a new project that involves the making a few very long forms.

The thought of writing all the html code (tables or DIVs) and validate them made me go back to this little project and decided to give it more thoughts.

This is the first release of the PHP code for this project so, if you would like to give it a bash and make use of it please do so…

A sample can be found here: SAMPLE PHP FORM

Some of the features:

  • Choice of using TABLE or DIVs for layout
  • You can create more than one form on the same page making sure the right one is validated on submission
  • Basic form validation
  • Option to make the field required or not
  • Show ERROR message  beside the required field – standard
  • Option to set the ERROR message text – set to REQUIRED in the sample form
  • Show ERROR message at the top of the form as well - optional
  • Cuts form creation time in more than half
  • Automatically sets the values of the field after posting the form
  • Option to add javascript code to the field (e.g. for use with ajax, etc)
  • and much more…

If you find any errors and anything that could help improve the functionality of this project, please leave a comment and I’ll make sure it gets done ASAP.

PHP create form fields

Friday, October 19th, 2007

The other day I was looking for a solution to help me create a form without the hassle to check it again and again for missing arguments or miss-typing.
Because i couldn’t find any ready made solutions I created this little function that made my life less miserable.

I hope it could be useful to you too.

/*
$ewd_type - could be text, password, submit, hidden, textarea or select
$ewd_name - the name and id of the input filed
$ewd_value - input value default or returned by post
$ewd_size - the size of the input type text
$ewd_cols - number of colums for the textarea type field - default = 30
$ewd_rows -  number of rows for the textarea type field - default = 6
*/    

function ewd_make_form_field($ewd_type,$ewd_name,$ewd_value='',$ewd_size='',$ewd_cols='',$ewd_rows=''){    

 global $_POST;
 $ewd_size = $ewd_size != "" ? $ewd_size : 30;
 $ewd_cols = $ewd_cols != "" ? $ewd_cols : 30;
 $ewd_rows = $ewd_rows != "" ? $ewd_rows : 6;
     

 switch ($ewd_type) {
  case "text":
   echo "<input type='$ewd_type' name='$ewd_name' id='$ewd_name' size='$ewd_size' value='$ewd_value' onfocus='this.style.borderColor=\"#0072BC\";' onblur='this.style.borderColor=\"silver\";' />";
   break;
  case "password":
   echo "<input type='$ewd_type' name='$ewd_name' id='$ewd_name' size='$ewd_size' value='$ewd_value' />";
   break;
  case "textarea":
   echo "<textarea name='$ewd_name' id='$ewd_name' cols='$ewd_cols' rows='ewd_rows'>$ewd_value</textarea>";
   break;
  case "submit":
   echo "<input type='$ewd_type' name='$ewd_name' id='$ewd_name' value='$ewd_value' />";
   break;
  case "hidden":
   echo "<input type='$ewd_type' name='$ewd_name' id='$ewd_name' value='$ewd_value' />";
   break;
  case "select":
   echo "<select name='$ewd_name' id='$ewd_name'> 
     <option value=''>Please Select</option>";
     $ewd_y_value = explode(" ",$ewd_value);
     foreach ($ewd_y_value as $ewd_word) {
     echo "<option value='$ewd_word'";
      if($ewd_word == trim($_POST[$ewd_name])){
       echo " selected='selected'";
      }
     echo ">".$ewd_word."</option>";
     }    

   echo "</select>";
   break;
  default:
   echo "<input type='text' name='$ewd_name' id='$ewd_name' size='$ewd_size' value='$ewd_value' />";
 }
}

Method for using the above function:

<form method="post" action="<?php $_SERVER['REQUEST_URI'];?>" name="x_form" id="x_form">
 <table summary="some form">
  <tr>
   <td colspan="2" style="background-color:#f5f5f5; padding:3px;">
    <b>About yourself</b>
   </td>
  </tr>
  <tr>
   <td>Name:</td>
   <td><?php ewd_make_form_field('text','name',isset($_POST['name']) ? $_POST['name'] : '');?></td>
  </tr>
  <tr>
   <td>Surname:</td>
   <td><?php ewd_make_form_field('text','surname',isset($_POST['surname']) ? $_POST['surname'] : '');?></td>
  </tr>
  <tr>
   <td>Email:</td>
   <td><?php ewd_make_form_field('text','email',isset($_POST['email']) ? $_POST['email'] : '');?></td>
  </tr>
  <tr>
   <td>Phone:</td>
   <td><?php ewd_make_form_field('text','phone',isset($_POST['phone']) ? $_POST['phone'] : '');?></td>
  </tr>
  <tr>
   <td>Type of Sale:</td>
   <td><?php ewd_make_form_field('select','sale_type','Private Garage Both');?></td>
  </tr>
  <tr>
   <td valign="top">Extra details:</td>
   <td><?php ewd_make_form_field('textarea','additional',isset($_POST['additional']) ? $_POST['additional'] : '');?>(eg. Engine cc: 1.1 to 1.4)</td>
  </tr>
  <tr>
   <td colspan="2" align="center">
    <?php ewd_make_form_field('submit','submit','Send');?>
    <?php ewd_make_form_field('hidden','country','Ireland');?>
   </td>   
  </tr>
</table>
</form>
Links...

Chicklets...
  • http://www.eire-webdesign.ie/blog/feed/
    http://www.eire-webdesign.ie/blog/feed/
    Google Reader or Homepage
    Add to My Yahoo!
    Subscribe with Bloglines
    Subscribe in NewsGator Online
    add to msn
    Add to My AOL
    Add to Technorati Favorites!
    pageflakes
    windows live