| | |
checkbox output
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Jan 2008
Posts: 1
Reputation:
Solved Threads: 0
Can someone help me? I have a enquete script with checkbox answers for question when more than one anwer can be choisen but de script will only write de latest answer to my txt file. Can someone help me? This is the first time i work with PHP.
<?php
# -------------------------------------------------------------------
# Get URL-based and absolute-based directory and filename.
$thisfile_URL = "http://".$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF'];
$thisdir_URL = substr($thisfile_URL, 0, strrpos($thisfile_URL, '/')).'/';
$thisfile_ABS = $_SERVER['SCRIPT_FILENAME'];
$thisdir_ABS = substr($thisfile_ABS, 0, strrpos($thisfile_ABS, '/')).'/';
# -------------------------------------------------------------------
# ***** CONFIGURATION SETTINGS *****
# The Email address you want results mailed to.
$email_to_send_to = '';
# The name of the survey. What do you want to call it?
$name_of_survey = 'Test Survey';
# Template files to load. The opening template, the close template.
# The template displayed before the survey form is printed and
# The template displayed after the email is sent. Or leave empty.
$open_template = $thisdir_ABS."templates/sss.open.php";
$close_template = $thisdir_ABS."templates/sss.close.php";
$survey_heading_page = $thisdir_ABS."templates/sss.head.php";
$survey_complete_page = $thisdir_ABS."templates/sss.done.php";
# The charset being used for all messages sent out. ISO-8859-1 by default,
# but you can use anything you want (Unicode, etc).
$charset = 'ISO-8859-1';
# -------------------------------------------------------------------
# ----------------------- Survey Form Information -------------------
# Type: checkbox
# Deft: Y or N for default check.
#
# Type: radio
# Deft: Default selected option
# Opts: Array of option values
#
# Type: select
# Deft: Default selected option
# Opts: Array of option values
#
# Type: text
# Deft: Default field text
# Opts: maxl, size - Maxlength and field size
#
# Type: textarea
# Deft: Default textarea field text
# Opts: rows, cols - row size, column size
#
# $hidden is an array of hidden fields.
# key being the name of the field, value as the value.
#
# $button_text is the submit button text
# -------------------------------------------------------------------
# ***** SAMPLE DATA *****
$question['1']['type'] = 'checkbox2';
$question['1']['desc'] = 'test';
$question['1']['deft'] = '';
$question['1']['opts'][] = 'test1';
$question['1']['opts'][] = 'test2';
$question['1']['opts'][] = 'test3';
$hidden['test1'] = "TEST!";
$hidden['test2'] = "TESTO!";
$button_text = 'Send it!';
# -------------------------------------------------------------------
function my_remove_slashes($st) {
if (get_magic_quotes_gpc()) { return stripslashes($st); }
else { return $st; }
}
function security_filter($st) {
$attribs = 'javascript
onclick|ondblclick|onmousedown|onmouseup|onmouseover|'.
'onmousemove|onmouseout|onkeypress|onkeydown|onkeyup';
$st = my_remove_slashes($st);
$st = stripslashes(preg_replace("/$attribs/i", 'forbidden', $st));
$st = strip_tags($st);
$st = htmlentities($st);
return $st;
}
# -------------------------------------------------------------------
if (!(empty($open_template))) { include($open_template); }
if (isset($_POST['button'])) {
foreach ($_POST as $key => $value) {
if ($key != 'button') {
if (preg_match('/^hidden_(.*)/i',$key)) {
$value = security_filter($value);
$key = trim(strstr($key,'_'),'_');
if (isset($hidden[$key])) {
$hidden_data[$key] = $value;
}
}
else {
if (isset($question[$key])) {
$value = security_filter($value);
if ($question[$key]['type'] == 'checkbox') { $value = "YES"; }
$results[$key] = $value;
}
}
}
}
if (isset($question)) {
$lineflag = FALSE;
foreach ($results as $key => $value) {
if ($lineflag == FALSE) {
$msg .= "----------------- Questions -----------------\n\n";
$lineflag = TRUE;
}
$msg .= "Question: ". $key ." - ". $question[$key]['desc'] ."\n";
$msg .= "Response: ". $value . "\n\n";
}
$msg .= "---------------------------------------------\n";
}
$msg .= "\n";
if (isset($hidden_data)) {
$lineflag = FALSE;
foreach ($hidden_data as $key => $value) {
if ($lineflag == FALSE) {
$msg .= "---------------- Hidden vars ----------------\n\n";
$lineflag = TRUE;
}
$msg .= $key ." - ". $value ."\n";
}
$msg .= "\n---------------------------------------------\n";
}
$target_dir = "enquetes/" ;
$aantal_files = count(scandir($target_dir)) ;
$aantal_files++ ;
$handle = fopen($target_dir.$aantal_files.".txt","a+") ;
fwrite($handle,$msg);
fclose($handle);
# Include template file.
if (!(empty($survey_complete_page))) { include($survey_complete_page); }
}
else {
if (!(empty($survey_heading_page))) { include($survey_heading_page); }
print "<form action=\"$thisfile_URL\" method=\"POST\"> \n";
foreach ($question as $key => $value) {
$type = strtolower($question[$key]['type']);
$desc = $question[$key]['desc'];
$deft = $question[$key]['deft'];
$opts = $question[$key]['opts'];
print $desc."\n";
if ($type == "checkbox") {
if (strtolower($deft) == 'y') { $box_value = "1"; $CHECKED = ' CHECKED'; }
else { $box_value = "0"; $CHECKED = ''; }
print "<input type=\"checkbox\" name=\"$key\" value=\"$box_value\"$CHECKED>\n<br>";
}
if ($type == "radio") {
print "<br>\n";
$i = 1;
foreach ($opts as $opt_key => $opt_value) {
if ($deft == $i) { $CHECKED = " checked=\"checked\""; }
else { $CHECKED = ''; }
print "<input type=\"RADIO\" name=\"$key\" value=\"$opt_value\"$CHECKED> - $opt_value <br>\n";
$i++;
}
}
if ($type == "checkbox2") {
print "<br>\n";
$i = 1;
foreach ($opts as $opt_key => $opt_value) {
if ($deft == $i) { $CHECKED = ' CHECKED'; }
else { $CHECKED = ''; }
print "<input type=\"checkbox\" name=\"$key\" value=\"$opt_value\"$CHECKED> - $opt_value <br>\n";
}
}
if ($type == "select") {
print "<br>\n";
print "<select name=\"$key\">\n";
$i = 1;
foreach ($opts as $opt_key => $opt_value) {
if ($deft == $i) { $CHECKED = ' SELECTED'; }
else { $CHECKED = ''; }
print "<option value=\"$opt_value\"$CHECKED>$opt_value</option>\n";
$i++;
}
print "</select><br>\n";
}
if ($type == "text") {
print "<br>\n";
$size = $opts['size'];
$maxl = $opts['maxl'];
print "<input maxlength=\"$maxl\" size=\"$size\" name=\"$key\" value=\"$deft\"><br>\n";
}
if ($type == "textarea") {
print "<br>\n";
$colz = $opts['cols'];
$rowz = $opts['rows'];
print "<textarea name=\"$key\" rows=\"$rowz\" cols=\"$colz\">$deft</textarea><br>\n";
}
# Spacing between survey questions -----------
print "<br>\n";
# --------------------------------------------
}
foreach ($hidden as $key => $value) {
print "<input type=\"hidden\" name=\"hidden_$key\" value=\"$value\">\n";
}
print "<input type=\"submit\" name=\"button\" value=\"$button_text\">\n";
print " -- \n";
print "<INPUT TYPE=\"reset\" VALUE=\"Clear\">\n";
print "</form>";
print "<center><font size=\"2\">Powered by <A HREF=\"http://www.ibasics.biz/sss\">Super Simple Survey</A></font></center><br>\n";
}
if (!(empty($close_template))) { include($close_template); }
?>
<?php
# -------------------------------------------------------------------
# Get URL-based and absolute-based directory and filename.
$thisfile_URL = "http://".$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF'];
$thisdir_URL = substr($thisfile_URL, 0, strrpos($thisfile_URL, '/')).'/';
$thisfile_ABS = $_SERVER['SCRIPT_FILENAME'];
$thisdir_ABS = substr($thisfile_ABS, 0, strrpos($thisfile_ABS, '/')).'/';
# -------------------------------------------------------------------
# ***** CONFIGURATION SETTINGS *****
# The Email address you want results mailed to.
$email_to_send_to = '';
# The name of the survey. What do you want to call it?
$name_of_survey = 'Test Survey';
# Template files to load. The opening template, the close template.
# The template displayed before the survey form is printed and
# The template displayed after the email is sent. Or leave empty.
$open_template = $thisdir_ABS."templates/sss.open.php";
$close_template = $thisdir_ABS."templates/sss.close.php";
$survey_heading_page = $thisdir_ABS."templates/sss.head.php";
$survey_complete_page = $thisdir_ABS."templates/sss.done.php";
# The charset being used for all messages sent out. ISO-8859-1 by default,
# but you can use anything you want (Unicode, etc).
$charset = 'ISO-8859-1';
# -------------------------------------------------------------------
# ----------------------- Survey Form Information -------------------
# Type: checkbox
# Deft: Y or N for default check.
#
# Type: radio
# Deft: Default selected option
# Opts: Array of option values
#
# Type: select
# Deft: Default selected option
# Opts: Array of option values
#
# Type: text
# Deft: Default field text
# Opts: maxl, size - Maxlength and field size
#
# Type: textarea
# Deft: Default textarea field text
# Opts: rows, cols - row size, column size
#
# $hidden is an array of hidden fields.
# key being the name of the field, value as the value.
#
# $button_text is the submit button text
# -------------------------------------------------------------------
# ***** SAMPLE DATA *****
$question['1']['type'] = 'checkbox2';
$question['1']['desc'] = 'test';
$question['1']['deft'] = '';
$question['1']['opts'][] = 'test1';
$question['1']['opts'][] = 'test2';
$question['1']['opts'][] = 'test3';
$hidden['test1'] = "TEST!";
$hidden['test2'] = "TESTO!";
$button_text = 'Send it!';
# -------------------------------------------------------------------
function my_remove_slashes($st) {
if (get_magic_quotes_gpc()) { return stripslashes($st); }
else { return $st; }
}
function security_filter($st) {
$attribs = 'javascript
onclick|ondblclick|onmousedown|onmouseup|onmouseover|'.'onmousemove|onmouseout|onkeypress|onkeydown|onkeyup';
$st = my_remove_slashes($st);
$st = stripslashes(preg_replace("/$attribs/i", 'forbidden', $st));
$st = strip_tags($st);
$st = htmlentities($st);
return $st;
}
# -------------------------------------------------------------------
if (!(empty($open_template))) { include($open_template); }
if (isset($_POST['button'])) {
foreach ($_POST as $key => $value) {
if ($key != 'button') {
if (preg_match('/^hidden_(.*)/i',$key)) {
$value = security_filter($value);
$key = trim(strstr($key,'_'),'_');
if (isset($hidden[$key])) {
$hidden_data[$key] = $value;
}
}
else {
if (isset($question[$key])) {
$value = security_filter($value);
if ($question[$key]['type'] == 'checkbox') { $value = "YES"; }
$results[$key] = $value;
}
}
}
}
if (isset($question)) {
$lineflag = FALSE;
foreach ($results as $key => $value) {
if ($lineflag == FALSE) {
$msg .= "----------------- Questions -----------------\n\n";
$lineflag = TRUE;
}
$msg .= "Question: ". $key ." - ". $question[$key]['desc'] ."\n";
$msg .= "Response: ". $value . "\n\n";
}
$msg .= "---------------------------------------------\n";
}
$msg .= "\n";
if (isset($hidden_data)) {
$lineflag = FALSE;
foreach ($hidden_data as $key => $value) {
if ($lineflag == FALSE) {
$msg .= "---------------- Hidden vars ----------------\n\n";
$lineflag = TRUE;
}
$msg .= $key ." - ". $value ."\n";
}
$msg .= "\n---------------------------------------------\n";
}
$target_dir = "enquetes/" ;
$aantal_files = count(scandir($target_dir)) ;
$aantal_files++ ;
$handle = fopen($target_dir.$aantal_files.".txt","a+") ;
fwrite($handle,$msg);
fclose($handle);
# Include template file.
if (!(empty($survey_complete_page))) { include($survey_complete_page); }
}
else {
if (!(empty($survey_heading_page))) { include($survey_heading_page); }
print "<form action=\"$thisfile_URL\" method=\"POST\"> \n";
foreach ($question as $key => $value) {
$type = strtolower($question[$key]['type']);
$desc = $question[$key]['desc'];
$deft = $question[$key]['deft'];
$opts = $question[$key]['opts'];
print $desc."\n";
if ($type == "checkbox") {
if (strtolower($deft) == 'y') { $box_value = "1"; $CHECKED = ' CHECKED'; }
else { $box_value = "0"; $CHECKED = ''; }
print "<input type=\"checkbox\" name=\"$key\" value=\"$box_value\"$CHECKED>\n<br>";
}
if ($type == "radio") {
print "<br>\n";
$i = 1;
foreach ($opts as $opt_key => $opt_value) {
if ($deft == $i) { $CHECKED = " checked=\"checked\""; }
else { $CHECKED = ''; }
print "<input type=\"RADIO\" name=\"$key\" value=\"$opt_value\"$CHECKED> - $opt_value <br>\n";
$i++;
}
}
if ($type == "checkbox2") {
print "<br>\n";
$i = 1;
foreach ($opts as $opt_key => $opt_value) {
if ($deft == $i) { $CHECKED = ' CHECKED'; }
else { $CHECKED = ''; }
print "<input type=\"checkbox\" name=\"$key\" value=\"$opt_value\"$CHECKED> - $opt_value <br>\n";
}
}
if ($type == "select") {
print "<br>\n";
print "<select name=\"$key\">\n";
$i = 1;
foreach ($opts as $opt_key => $opt_value) {
if ($deft == $i) { $CHECKED = ' SELECTED'; }
else { $CHECKED = ''; }
print "<option value=\"$opt_value\"$CHECKED>$opt_value</option>\n";
$i++;
}
print "</select><br>\n";
}
if ($type == "text") {
print "<br>\n";
$size = $opts['size'];
$maxl = $opts['maxl'];
print "<input maxlength=\"$maxl\" size=\"$size\" name=\"$key\" value=\"$deft\"><br>\n";
}
if ($type == "textarea") {
print "<br>\n";
$colz = $opts['cols'];
$rowz = $opts['rows'];
print "<textarea name=\"$key\" rows=\"$rowz\" cols=\"$colz\">$deft</textarea><br>\n";
}
# Spacing between survey questions -----------
print "<br>\n";
# --------------------------------------------
}
foreach ($hidden as $key => $value) {
print "<input type=\"hidden\" name=\"hidden_$key\" value=\"$value\">\n";
}
print "<input type=\"submit\" name=\"button\" value=\"$button_text\">\n";
print " -- \n";
print "<INPUT TYPE=\"reset\" VALUE=\"Clear\">\n";
print "</form>";
print "<center><font size=\"2\">Powered by <A HREF=\"http://www.ibasics.biz/sss\">Super Simple Survey</A></font></center><br>\n";
}
if (!(empty($close_template))) { include($close_template); }
?>
Hi Nick,
it's a bit difficult to debug your script when you didn't use the syntax highlighting provided by this forum. Could you please edit your post insert the CODE tags?
Could I see the script at work somewhere? In general, the problem is either in HTML (answers are overwriting one another) or in processing the response. It would be easier for me to check the first area if I could see the HTML directly.
Also, your sample data contains only one item so it cannot show the behaviour you've described, right? Could you add more and use some real life data? The problem could be there too.
it's a bit difficult to debug your script when you didn't use the syntax highlighting provided by this forum. Could you please edit your post insert the CODE tags?
Could I see the script at work somewhere? In general, the problem is either in HTML (answers are overwriting one another) or in processing the response. It would be easier for me to check the first area if I could see the HTML directly.
Also, your sample data contains only one item so it cannot show the behaviour you've described, right? Could you add more and use some real life data? The problem could be there too.
Petr 'PePa' Pavel
The more information you give the more relevant answer you get.
Please consider using "Add to ... Reputation" and mark your thread as Solved if you found what you were looking for. By giving feedback you help others.
The more information you give the more relevant answer you get.
Please consider using "Add to ... Reputation" and mark your thread as Solved if you found what you were looking for. By giving feedback you help others.
![]() |
Similar Threads
- Parse a concatenated variable and string? (PHP)
- Custom Database Admin Tool. (PHP)
- c# window application using checkbox and label (C#)
- Change output in cgi script (Perl)
- simple checkbox form array help! (PHP)
- passing checkbox value to next page (PHP)
- Processing Checkbox Information From A Form (JavaScript / DHTML / AJAX)
- Forms authorization, only want a few links (ASP.NET)
Other Threads in the PHP Forum
- Previous Thread: nearest number
- Next Thread: PHP Drop down ordering
| Thread Tools | Search this Thread |
Tag cloud for PHP
# .htaccess 5.2.10 access ajax apache api array beginner binary broken cakephp checkbox class cms code cron curl database date directory display dissertation download dynamic echo email error file files folder form forms function functions google href htaccess html image images include insert integration ip java javascript joomla ldap legislation limit link login loop mail menu mlm mod_rewrite multiple mysql mysqlquery oop open parse paypal pdf persist php problem query radio random recursion regex remote script search server sessions sms soap sockets source space sql structure syntax system table tutorial update upload url validation validator variable video web xml youtube





