Please login or register.

Login with username, password and session length
Advanced search  

News:

Latest sNews - sNews 1.7 - with its own forums - for discussion and user mods.

Author Topic: Easier Forms?  (Read 2750 times)

centered

  • Guest
Easier Forms?
« on: December 31, 2008, 08:06:55 pm »

Well, in wanting an option other than html_input in sNews, and in thinking that straight html may be too much, I made the mistake of making another html_input....

So, I am trashing the idea and giving the code away here if anyone wants it.  Please note, it does follow snLite and sNews code (s(charset) and urls) but it can be changed. This code is supposed to help give better html as it follows many of the x/html specs (certain tags get certain elements and so forth).. not sure if this was 100% accomplished but again, I am trashing the idea since it may be as confusing as html_input is....

Code: (formBuilder.php) [Select]
<?php

function sForm($action$method$css) {
global $subpage;
if ($action == 'start') {
$method = ((strtolower($method) == "post") || (strtolower($method) == "get")) ? ' method="'.$method.'"' '';
$ctype  = ($subpage == 'files') ? ' enctype="multipart/form-data"' '';
$charset ' accept-charset="'.s('charset').'" ';
$css = (substr($css02) == 'id' || substr($css05) == 'class') ? ' '.$css '';
$options $method.$ctype.$charset.$css;
}
switch ($action) {
case 'start' :
$form '
<form'
.$options.' action="'.__site.'admin/'.$subpage.'/process/">';
break;
case 'end' :
$form '
</form>'
;
break;
default: 
break;
}
return $form;
}

function 
sFieldset($action) {
switch ($action) {
case 'start' :
$fieldset '
<fieldset>'
;
break;
case 'end' :
$fieldset '
</fieldset>'
;
break;
default: break;
}
return $fieldset;
}

function 
sLegend($text) {
return '
<legend>'
.$text.'</legend>';
}

function 
sLabel($for$text$css) {
$for = !empty($for) ? ' for="'.$for.'"' '';
$css = !empty($css) ? ' class="'.$css.'"' '';
return '
<label'
.$for.$css.'>'.$text.'</label>';
}

function 
sButton($name$type$value$css) {
$name = !empty($name) ? ' name="'.$name.'"' '';
$type = ($type == 'button' || $type == 'reset' || $type == 'submit') ? ' type="'.$type.'"' '';
$css = !empty($css) ? ' class="'.$css.'"' '';
$options $name.$type.$css;
return '
<button'
.$options.'>'.$value.'</button>';
}

function 
sTextArea($cols$rows$name$value$label$css) {
if (!empty($cols) && !empty($rows)) {
$label = (!empty($name) && !empty($label)) ? sLabel($name$label$css) : '';
$name = !empty($name) ? ' id="'.$name.'" name="'.$name.'"' '';
return $label.'
<textarea cols="'
.$cols.'" rows="'.$rows.'"'.$name.'>'.$value.'</textarea>';
}
}

function 
sButtonInput($type$name$value$css) {
$type = ($type == 'button' || $type == 'submit' || $type == 'reset') ? $type '';
$name = ($type == 'button') ? ' name="'.$name.'"' '';
$value = !empty($value) ? ' value="'.$value.'"' '';
$css = !empty($css) ? ' class="'.$css.'"' '';
return '
<input type="'
.$type.'"'.$name.$value.$css.' />';
}

function 
sTextInput($type$name$value$label$css) {
$type = ($type == 'password' || $type == 'text') ? $type '';
$label = (!empty($name) && !empty($label)) ? sLabel($name$label$css) : '';
$name = !empty($name) ? ' id="'.$name.'" name="'.$name.'"' '';
$value = !empty($value) ? ' value="'.$value.'"' '';
return $label.'
<input type="'
.$type.'"'.$name.$value.$css.' />';
}

function 
sImgInput($src$alt$name) {
if (!empty($src) && !empty($alt) && !empty($name)){
return '
<input type="image" name="'
.$name.'" src="'.$src.'" alt="'.alt.'" />';
}
}

function 
sFileInput($name$label$css) {
global $subpage;
if ($subpage == 'files') {
$label = (!empty($name) && !empty($label)) ? sLabel($name$label$css) : '';
$name = !empty($name) ? ' id="'.$name.'" name="'.$name.'"' '';
return $label.'
<input type="file"'
.$name.' />';
}
}

function 
sRadioInput($type$name$value$checked$label) {
if (!empty($value) && !empty($name)) {
$type = ($type == 'checkbox' || $type == 'radio') ? $type '';
$check = !empty($checked) ? ' checked="checked"' '';
$label = (!empty($name) && !empty($label)) ? sLabel($name$label'') : '';
return '
<input type="'
.$type.'" id="'.$name.'" name="'.$name.'" value="'.$value.'"'.$check.' />'.$label;
}
}

function 
sHiddenInput($name$value) {
if (!empty($name) && !empty($value)) {
return '
<input type="hidden" name="'
.$name.'" value="'.$value.'" />';
}
}

?>

Code: (test.php) [Select]
<?php

require('formBuilder.php');

$subpage 'files';
define('__site''http://www.domain.com/');

?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<style type="text/css">
body

{

background:#fffff;

font-family:"Lucida Grande", Tahoma, Arial, Verdana, sans-serif;

font-size:small;

margin:8px 0 16px;

text-align:center;

}
form

{

background:#fff;

border:1px solid #ccc;

margin:0 auto;

text-align:left;

width:640px;

}
fieldset
{
border: none;
}
label.label

{

border:none;

color:#222;

display:block;

font-size:95%;

font-weight:700;

line-height:150%;

padding:0 0 1px;

}
</style>
</head>
<body>

<?php
echo sForm('start''post''');
echo 
sFieldset('start');
echo 
sLegend('Test Form');
echo 
sTextArea(205'myTextArea''Some default text in the text area''Text Area''label');
echo 
sRadioInput('radio''myRadio''True''ok''A Radio Button');
echo 
sButtonInput('reset''''Reset Me!''');
echo 
sButton('button''myButton''aButton''');
echo 
sFileInput('blah''Upload a File''label');
echo 
sTextInput('text''blah_blah''''Name'''); 
echo 
sFieldset('end');
echo 
sForm('end''''');

?>

</body>
</html>

« Last Edit: December 31, 2008, 08:09:02 pm by equilni »
Logged

Joost

  • Guest
Re: Easier Forms?
« Reply #1 on: December 31, 2008, 10:28:54 pm »

Great idea....

.... trashing it. ;)

I did some experiments too. It starts with something simple. But at the end of the day, after extending it, adding more functionality, you end up with another script language. No one wants that.
Logged