eire web design home page contact eire-web design eire web design and development site map
stepping on in business
• PHP create form fields

PHP create form fields



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>
Share and Enjoy:
  • Digg
  • del.icio.us
  • blogmarks
  • Reddit
  • StumbleUpon
  • Technorati
  • Blogosphere News
  • Facebook
  • Google Bookmarks
  • Mixx
  • MySpace
  • Yahoo! Bookmarks
  • Yahoo! Buzz
  • LinkedIn

Tags:

This entry was posted on Friday, October 19th, 2007 at 9:22 pm and is filed under PHP. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

1 Star2 Stars3 Stars4 Stars5 Stars (2 votes, average: 3.50 out of 5)
Loading ... Loading ...


  • 2 Responses to “PHP create form fields”

paul Says:
October 21st, 2007 at 2:21 pm

That’s pretty nice ! Good work Louie !

I like the update to your site. But 1 think, I hate the “Notify me of followup comments via e-mail” , which is checked by default. You can change it in the plugin settings if you want.

Paul

Louie @ Eire Web Design Says:
October 21st, 2007 at 3:36 pm

Thanks Paul.
I never looked into the setup properly yet, but I have disabled the checkbox already.

The function itself can be a lot smaller and I will change it when I get a chance as “text”, “password” and “hidden” could be grouped into just one “case” statement.

Yeah the website had to be changed and it was on my list for ages now, but you know how it is, you never have time for yourself.

  • Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

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