Last active
January 31, 2017 13:53
-
-
Save tovask/611e5dad0eba7d9fe94cd711c8f407b5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$form = array( | |
array( | |
'question' => 'First section', // title of the following section | |
'type' => 'section-header', | |
'hint' => '', | |
), | |
array( | |
'question' => 'Név', | |
'type' => 'text', | |
'hint' => '', | |
'formname' => 'personname', // the name in the html form | |
'dbcolumn' => 'name', | |
), | |
array( | |
'question' => 'Nem', | |
'type' => 'boolean', // a radio with yes/no options | |
'hint' => '', | |
'formname' => 'sex', | |
'dbcolumn' => 'sex', | |
), | |
array( | |
'question' => 'Szemszín', | |
'type' => 'radio', | |
'hint' => '', | |
'options' => array(1 => 'kék',2 => 'zöld',3 => 'barna',4 => 'piros'), | |
'formname' => 'eyecolor', | |
'dbcolumn' => 'eye', | |
), | |
array( | |
'question' => 'Születés', | |
'type' => 'date', | |
'hint' => '', | |
'formname' => 'borndate', // send with $formname.'-year',$formname.'-month',$formname.'-day' | |
'dbcolumn' => 'born', | |
), | |
); | |
// if POST, build sql | |
if($_SERVER['REQUEST_METHOD']=='POST'){ | |
header('Content-Type: text/plain'); | |
$insert = array(); | |
foreach ($form as $question){ | |
if($question['type'] == 'section-header'){ | |
continue; | |
} | |
if(!isset($_POST[$question['formname']]) && ($question['type']!='date') ){ | |
print 'No data sent for '.$question['formname']."\n"; | |
continue; | |
} | |
switch ($question['type']){ | |
case 'text': | |
$insert[$question['dbcolumn']] = '\''.addslashes($_POST[$question['formname']]).'\''; // more escape pls | |
break; | |
case 'boolean': | |
if( ($_POST[$question['formname']] !== '1') && ($_POST[$question['formname']] !== '2') ){ | |
print 'unallowed value for '.$question['formname'].' : '.$_POST[$question['formname']]."\n"; | |
continue; | |
} | |
$insert[$question['dbcolumn']] = $_POST[$question['formname']]; | |
break; | |
case 'radio': | |
if( !isset( $question['options'][ $_POST[$question['formname']] ] ) ){ | |
print 'unallowed value for '.$question['formname'].' : '.$_POST[$question['formname']]."\n"; | |
continue; | |
} | |
$insert[$question['dbcolumn']] = $_POST[$question['formname']]; | |
break; | |
case 'date': | |
if( !isset($_POST[$question['formname'].'-year']) || !isset($_POST[$question['formname'].'-month']) || !isset($_POST[$question['formname'].'-day']) ){ | |
print 'Not enough data for (date) '.$question['formname']."\n"; | |
continue; | |
} | |
$year = intval($_POST[$question['formname'].'-year']); | |
$month = intval($_POST[$question['formname'].'-month']); | |
$day = intval($_POST[$question['formname'].'-day']); | |
if($year<1900 || $year>intval(date('Y'))){ | |
print 'invalid year: '.$_POST[$question['formname'].'-year'].' , using 1990'."\n"; | |
$year = 1990; | |
} | |
if($month<1 || $month>12){ | |
print 'invalid month: '.$_POST[$question['formname'].'-year'].' , using 1'."\n"; | |
$month = 1; | |
} | |
if($day<1 || $day>31){ | |
print 'invalid day: '.$_POST[$question['formname'].'-year'].' , using 1'."\n"; | |
$day = 1; | |
} | |
// Varning! Didn't check if the day exists in the month (e.g. febr. 30.) | |
$insert[$question['dbcolumn']] = mktime(0,0,0,$month,$day,$year); // $hour,$minute,$second,$month,$day,$year | |
break; | |
default: | |
print 'Error: Unknown type: '.$question['type']."\n"; // log error | |
break; | |
} | |
} | |
$columns = array(); | |
$values = array(); | |
foreach ($insert as $c => $v) { | |
$columns[] = $c; | |
$values[] = $v; | |
} | |
print 'INSERT INTO dbtable ('.implode($columns,' , ').') VALUES ('.implode($values,' , ').')'; | |
exit(); | |
} | |
// for generating the html | |
if(isset($_GET['gen'])){ | |
header('Content-Type: text/plain'); | |
}else{ | |
?> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title></title> | |
<style> | |
html, body, .container{ | |
width: 100%; | |
height: 100%; | |
margin: 0px; | |
padding: 0px; | |
text-align: center; | |
} | |
.left, .center, .right{ | |
position: absolute; | |
} | |
.left{ | |
left: 0px; | |
width: 100px; | |
} | |
.center{ | |
left: 100px; | |
right: 100px; | |
} | |
.right{ | |
right: 0px; | |
width: 100px; | |
} | |
.header{ | |
top: 0px; | |
height: 100px; | |
} | |
.main{ | |
top: 100px; | |
bottom: 100px; | |
} | |
.footer{ | |
bottom: 0px; | |
height: 100px; | |
} | |
.main.center{ | |
overflow-x: auto; | |
overflow-y: auto; | |
text-align: left; | |
} | |
/* coloring */ | |
.header.left{ | |
background-color: #ff0; | |
} | |
.header.right{ | |
background-color: #f0f; | |
} | |
.header.center{ | |
background-color: #0ff; | |
} | |
.main.left{ | |
background-color: #880; | |
} | |
.main.right{ | |
background-color: #80f; | |
} | |
.main.center{ | |
background-color: #088; | |
} | |
.footer.left{ | |
background-color: #00f; | |
} | |
.footer.right{ | |
background-color: #0f0; | |
} | |
.footer.center{ | |
background-color: #f00; | |
} | |
/* FORM */ | |
form{ | |
min-width: 660px; /* 300+300+10+10+30+10 */ | |
} | |
.section-header{ | |
padding: 10px; | |
font-size: larger; | |
} | |
.question-block{ | |
position: relative; /* for centering question vertical */ | |
margin: 10px 10px 10px 30px; | |
padding: 10px; | |
line-height: 20px; /* question's margin-top depend on this (see below) */ | |
background-color: #888; | |
} | |
.question-question{ | |
position: absolute; | |
top: 50%; /* center vertical */ | |
margin-top: -10px; /* line-height / 2 */ | |
width: 300px; | |
} | |
.question-answer{ | |
margin-left: 300px; | |
} | |
.question-answer div{ | |
display: inline-block; | |
width: 300px; | |
padding: 5px 0px 5px 0px; | |
} | |
</style> | |
<script> | |
/*window.onload = function(){ | |
var testtext = ""; | |
for(var i=0;i<60;i++){ | |
for(var j=0;j<20;j++){ | |
testtext += "include content here! "; | |
} | |
testtext += "<br>"; | |
} | |
document.getElementsByClassName("main center")[0].innerHTML = testtext; | |
document.getElementsByClassName("main center")[0].style.whiteSpace = "nowrap"; | |
};*/ | |
</script> | |
</head> | |
<body> | |
<div class="container"> | |
<div class="header left">A</div> | |
<div class="header right">B</div> | |
<div class="header center">C</div> | |
<div class="main left">D</div> | |
<div class="main right">E</div> | |
<div class="main center"> | |
<form method="POST" > | |
<?php | |
} | |
foreach ($form as $question){ | |
switch ($question['type']){ | |
case 'section-header': | |
print ' <div class="section-header" title="'.$question['hint'].'" >'.$question['question'].'</div>'."\n"; | |
break; | |
case 'text': | |
print ' <div class="question-block" title="'.$question['hint'].'" >'."\n"; | |
print ' <div class="question-question">'.$question['question'].'</div>'."\n"; | |
print ' <div class="question-answer"><input type="text" name="'.$question['formname'].'" id="'.$question['formname'].'" value="" class="answer-text" /></div>'."\n"; | |
print ' </div>'."\n"; | |
break; | |
case 'boolean': | |
print ' <div class="question-block" title="'.$question['hint'].'" >'."\n"; | |
print ' <div class="question-question">'.$question['question'].'</div>'."\n"; | |
print ' <div class="question-answer">'."\n"; | |
print ' <div><label><input type="radio" name="'.$question['formname'].'" id="'.$question['formname'].'-1" value="1" class="answer-boolean" />Igen</label></div>'."\n"; | |
print ' <div><label><input type="radio" name="'.$question['formname'].'" id="'.$question['formname'].'-2" value="2" class="answer-boolean" />Nem</label></div>'."\n"; | |
print ' </div>'."\n"; | |
print ' </div>'."\n"; | |
break; | |
case 'radio': | |
print ' <div class="question-block" title="'.$question['hint'].'" >'."\n"; | |
print ' <div class="question-question">'.$question['question'].'</div>'."\n"; | |
print ' <div class="question-answer">'."\n"; | |
//for($i=0; $i<count($question['options']); $i++){ | |
foreach($question['options'] as $i => $value){ | |
print ' <div><label><input type="radio" name="'.$question['formname'].'" id="'.$question['formname'].'-'.$i.'" value="'.$i.'" class="answer-radio" />'.$value.'</label></div>'."\n"; | |
} | |
print ' </div>'."\n"; | |
print ' </div>'."\n"; | |
break; | |
case 'date': | |
print ' <div class="question-block" title="'.$question['hint'].'" >'."\n"; | |
print ' <div class="question-question">'.$question['question'].'</div>'."\n"; | |
print ' <div class="question-answer">'."\n"; | |
// years: | |
print ' <select name="'.$question['formname'].'-year">'."\n"; | |
print ' <option value="" selected>-</option>'."\n"; | |
for( $i=1900; $i<=intval(date('Y')); $i++){ | |
print ' <option id="'.$question['formname'].'-year-'.$i.'" value="'.$i.'">'.$i.'</option>'."\n"; | |
} | |
print ' </select>'."\n"; | |
// months: | |
print ' <select name="'.$question['formname'].'-month">'."\n"; | |
print ' <option value="" selected>-</option>'."\n"; | |
$months = array('Jan','Feb','Mar','Apr','Maj','Jun','Jul','Aug','Sep','Okt','Nov','Dec'); | |
for( $i=0; $i<count($months); $i++ ){ | |
print ' <option id="'.$question['formname'].'-month-'.($i+1).'" value="'.($i+1).'">'.$months[$i].'</option>'."\n"; | |
} | |
print ' </select>'."\n"; | |
// days: | |
print ' <select name="'.$question['formname'].'-day">'."\n"; | |
print ' <option value="" selected>-</option>'."\n"; | |
for( $i=1; $i<32; $i++){ | |
print ' <option id="'.$question['formname'].'-day-'.$i.'" value="'.$i.'">'.$i.'</option>'."\n"; | |
} | |
print ' </select>'."\n"; | |
print ' </div>'."\n"; | |
print ' </div>'."\n"; | |
break; | |
default: | |
print 'Error: Unknown type: '.$question['type']."\n"; // log error | |
break; | |
} | |
} | |
if(!isset($_GET['gen'])){ | |
print ' <input type="submit" value="Mentés" />'."\n"; | |
print ' </form>'."\n"; | |
// load data for modify | |
if(isset($_GET['mod'])){ | |
//$result = SELECT * FROM dbtable; | |
// fill up test with datas: | |
$result = array(); | |
foreach ($form as $question){ | |
switch ($question['type']){ | |
case 'section-header': | |
break; | |
case 'text': | |
$result[$question['dbcolumn']] = 'Test text.'; | |
break; | |
case 'boolean': | |
$result[$question['dbcolumn']] = 1; | |
break; | |
case 'radio': | |
$result[$question['dbcolumn']] = 1; | |
break; | |
case 'date': | |
$result[$question['dbcolumn']] = mktime(0,0,0,3,2,1992); | |
break; | |
default: | |
print 'Error: Unknown type: '.$question['type']."\n"; // log error | |
break; | |
} | |
} | |
print '<script type="text/javascript">'; | |
foreach ($form as $question){ | |
if( ($question['type'] == 'section-header') || !isset($result[$question['dbcolumn']]) || !$result[$question['dbcolumn']] ){ | |
continue; | |
} | |
switch ($question['type']){ | |
case 'text': | |
print 'document.getElementById("'.$question['formname'].'").value = "'.$result[$question['dbcolumn']].'";'."\n"; | |
break; | |
case 'boolean': | |
case 'radio': | |
print 'document.getElementById("'.$question['formname'].'-'.$result[$question['dbcolumn']].'").checked = true;'."\n"; | |
break; | |
case 'date': | |
$year = date('Y',$result[$question['dbcolumn']]); | |
$month = date('n',$result[$question['dbcolumn']]); | |
$day = date('j',$result[$question['dbcolumn']]); | |
print 'document.getElementById("'.$question['formname'].'-year-'.$year.'").setAttribute("selected", "selected")'."\n"; | |
print 'document.getElementById("'.$question['formname'].'-month-'.$month.'").setAttribute("selected", "selected")'."\n"; | |
print 'document.getElementById("'.$question['formname'].'-day-'.$day.'").setAttribute("selected", "selected")'."\n"; | |
break; | |
default: | |
print 'Error: Unknown type: '.$question['type']."\n"; // log error | |
break; | |
} | |
} | |
print '</script>'; | |
print '<br> <a href="?">Üres nézet</a><br><br>'."\n"; | |
}else{ | |
print '<br><a href="?mod">Módosító nézet</a><br><br>'."\n"; | |
} | |
?> | |
</div> | |
<div class="footer left">G</div> | |
<div class="footer right">H</div> | |
<div class="footer center">I</div> | |
</div> | |
</body> | |
</html> | |
<?php | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment