Uncategorized

Easier PHP forms, part 2

In part 1 I showed you my method for simplifying form building and maintenance. Now I’d like to show you a few of the things that can be simplified by using this system. Included are:

  • prices
  • passwords
  • emails
  • textareas
  • selects
  • yes/no (booleans)
  • files
  • hiddens
  • cancel buttons
  • submit buttons
  • “are you sure you want to do that?” submit buttons
  • addresses

I use this code for 95% of the forms I build and then modify the CSS to get the effects a customer wants.

In part 3 I’ll show you how to use jQuery to automatically validate user data.

//------------------------------------------------------------------------------
function create_form_price($name,$value=null,$label=null,$hint=null) {
  return create_form_row("\$<input type='text' name='$name' value='".htmlspecialchars($value,ENT_QUOTES)."'>",$label,$name,$hint);
}

//------------------------------------------------------------------------------
function create_form_password($name,$value=null,$label=null,$hint=null) {
  return create_form_row("<input type='password' name='$name' value='".htmlspecialchars($value,ENT_QUOTES)."'>",$label,$name,$hint);
}

//------------------------------------------------------------------------------
function create_form_password_confirm($name,$value=null,$label=null,$hint=null) {
  add_form_test_password_confirm("pass",$label);  // we'll cover this in part 3

  return create_form_row("<input type='password' name='$name' value='".htmlspecialchars($value,ENT_QUOTES)."'>",$label,$name,"").
  create_form_row("<input type='password' name='{$name}_confirm' value=''>","&nbsp;",$name."_confirm",$hint);
}

//------------------------------------------------------------------------------
function create_form_email($name,$value=null,$label=null,$hint=null) {
 // @TODO: add email validation.
 $test="    regex_{$name}=/^[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)*@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)*(\.[a-zA-Z]{2,4})$/\n"
 ."    compare_to=f.{$name}.value.replace(/^\s+|\s+$/g,'');\n"
 ."    if(compare_to!='' && !compare_to.match(regex_{$name})) {\n"
 .add_form_error($name,"$label does not appear to be a valid email.\n")
 ."    }\n";
 add_form_test($test,$name);

 return create_form_row("<input type='text' name='$name' value='".htmlspecialchars($value,ENT_QUOTES)."'>",$label,$name,$hint);
}

//------------------------------------------------------------------------------
function create_form_textarea($name,$value=null,$label=null,$hint=null) {
 return create_form_row("<textarea name='$name' rows='10' cols='80'>".htmlspecialchars($value,ENT_QUOTES)."</textarea>",$label,$name,$hint);
}

//------------------------------------------------------------------------------
function create_form_select_internal($name,$options,$value=null,$multiple=0,$extra='') {
 $sel="<select id='$name' name='$name' $extra";
 if($multiple>0) {
 $sel.=" multiple='yes' size='$multiple'";
 }
 $sel.=">\n";
 if(is_array($options)) {
 foreach($options as $k=>$v) {
 $k=htmlspecialchars($k,ENT_QUOTES);
 $v=htmlspecialchars($v,ENT_QUOTES);
 $sel.="  <option value='$k'>$v</option>\n";
 }
 }
 $sel.="</select>";
 $v=isset($value)?htmlspecialchars($value,ENT_QUOTES):"";
 return str_replace("value='$v'","value='$v' selected",$sel);
}

//------------------------------------------------------------------------------
function create_form_select($name,$options,$value=null,$label=null,$hint=null,$multiple=0,$extra='') {
 $sel=create_form_select_internal($name,$options,$value,$multiple,$extra);
 return create_form_row($sel,$label,$name,$hint);
}

//------------------------------------------------------------------------------
function create_form_bool($name,$value=null,$label=null,$hint=null,$extra='') {
 $sel="<select name='$name' $extra>\n";
 $sel.="  <option value='yes'>"._t("Yes")."</option>\n";
 $sel.="  <option value='no'>"._t("No")."</option>\n";
 $sel.="</select>";
 $v=isset($value)?htmlspecialchars($value,ENT_QUOTES):"";
 $sel=str_replace("value='$v'","value='$v' selected",$sel);
 return create_form_row($sel,$label,$name,$hint);
}

//------------------------------------------------------------------------------
function create_form_file($name,$label='',$hint='') {
 $inner="<input name='$name' type='file'>";

 return create_form_row($inner,$label,$name,$hint);
}

//------------------------------------------------------------------------------
function create_form_address($name,$value=null,$label=null,$hint=null) {
 if(!isset($value['address1'])) $value['address1']="";
 if(!isset($value['address2'])) $value['address2']="";
 if(!isset($value['city'])) $value['city']="";
 if(!isset($value['region'])) $value['region']="";
 if(!isset($value['country'])) $value['country']="";
 if(!isset($value['postalcode'])) $value['postalcode']="";

 $str =create_form_text($name."_address1",$value['address1'],"Street");
 $str.=create_form_text($name."_address2",$value['address2'],"");
 $str.=create_form_text($name."_city",$value['city'],"City");
 $str.=create_form_text($name."_region",$value['region'],"State/Province");
 $str.=create_form_text($name."_country",$value['country'],"Country");
 $str.=create_form_text($name."_postalcode",$value['postalcode'],"Postal Code");

 return create_form_row($str,$label,null,null);
}

//------------------------------------------------------------------------------
function create_form_hidden($name,$value=null) {
 return "  <input type='hidden' id='$name' name='$name' value='".htmlspecialchars($value,ENT_QUOTES)."'>\n";
}

//------------------------------------------------------------------------------
function create_form_submit($name,$value,$extra='') {
 global $last_form_name;  // we'll cover this is in part 3

 return "  <input type='submit' name='$name' value='".htmlspecialchars($value,ENT_QUOTES)."' onclick='javascript:return validate_$last_form_name(this.form);' $extra>\n";
}

//------------------------------------------------------------------------------
function create_form_cancel($name,$value,$redirect) {
 return "  <input type='button' name='$name' value='".htmlspecialchars($value,ENT_QUOTES)."' onclick='javascript:window.location=\"".htmlspecialchars($redirect,ENT_QUOTES)."\";'>\n";
}

//------------------------------------------------------------------------------
function create_form_submit_confirm($name,$value,$confirm="Are you sure?",$validate=0) {
 global $last_form_name;  // we'll cover this is in part 3

 $str="  <input type='submit' name='$name' value='".htmlspecialchars($value,ENT_QUOTES)."' onclick='javascript:return (confirm(\"".str_replace(array("'","\n"),array("\'","\\n"),$confirm)."\")";
 if($validate!=0) {
 $str.=" & validate_$last_form_name(this.form)";
 }
 $str.=");'>\n";
 return $str;
}