I'm trying to store the results of a short form to a flat-file (e.g. a .text file). The file will be imported into Excel as a delimited file. The form has checkboxes so it can be set into an array:
<input type="hidden" name="roflz" value="noresponse">
<input type="checkbox" name="roflz[]" value="Yesroflz" id="Yesroflz" <?php if (isset($_SESSION['roflz']) && $_SESSION['rolfz'] == "Yesroflz") { echo 'checked="checked"'; } ?>> <label for="Yesroflz">Yesroflz</label> <br />
<input type="checkbox" name="roflz[]" value="Moreroflz" id="Moreroflz" <?php if (isset($_SESSION['roflz']) && $_SESSION['roflz'] == "Moreroflz") { echo 'checked="checked"'; } ?>> <label for="Moreroflz">Moreroflz</label><br />
Is it possible to implode an array from $_POST['rolfz']
and return the results to $_POST['rolfz']
? The desired result is a string that I can store into a text file. See code below:
<?php
// begin the session
ini_set('session.cache_limiter', 'private');
session_start();
//Turn checkboxes (an array) into a string. So we can later store it into a strong.
if(isset($_POST['rolfz'])){
$_POST['rolfz'] = implode(",",$_POST['rolfz']);
}
The overall goal is to store all the values from $_SESSION into a text file. However, when I try to run the code I get "Notice: Array to string conversion in E:\cgi-bin\thankyou.php on line 14". The error telling me that in the below code I'm trying to use an array as a string.
// Take each input name and create a variable for it
foreach($_POST as $k=>$v) {
$_SESSION[$k]=$v;
}
//Implodes the array so we can store the values as a string (not the variables name though!)
$results = implode("|", $_SESSION);
//Open up the file that we will store the collected information to
$datafile=fopen("V1.txt","a") or exit("Unable to open file to record data!");
//Write the data on a new line ("\r\n") so we don't over-write the existing data
fwrite($datafile, "\r\n".$results);
//Close out the file
fclose($datafile);
//reset session variables and destroy the session
$_SESSION = array();
session_destroy();
?>
If what I'm doing is wrong or impossible, is it possible to use an alternative statement instead for the foreach($_POST as $k=>$v)
so that it ignores $_POST['rolfz']
? That way I can handle $_POST['rolfz']
on its own?
Edit (5/10/11): Before imploding:
array(5) { ["date"]=> string(10) "05/10/2011" ["time"]=> string(11) "09:11:20 AM" ["gender"]=> string(4) "male" ["lolz"]=> string(7) "YesLOLZ" ["roflz"]=> array(2) { [0]=> string(8) "Yesroflz" [1]=> string(7) "Noroflz" } }
After imploding:
array(5) { ["date"]=> string(10) "05/10/2011" ["time"]=> string(11) "09:11:20 AM" ["gender"]=> string(4) "male" ["lolz"]=> string(7) "YesLOLZ" ["roflz"]=> array(2) { [0]=> string(8) "Yesroflz" [1]=> string(7) "Noroflz" } }
Edit (5/10/11): Please see Michael's solution if you are attempting to do a similar solution. And note that I'm an idiot and used the wrong checkbox name in my code (it should be roflz not rolfz).
$_SESSION
as a string to write to your file instead of using theserialize()
function, which is designed for that purpose? us.php.net/manual/en/function.serialize.php – Michael Berkowski May 9 '11 at 20:29serialize()
at one point but if I recall it ended up with a bunch of text unreadable to most people. Since others will be importing the text file into Excel (don't have the ability to write directly to an excel file) and using pipe delimination to break it into columns I'm trying to do it this way. – OneBigNewbie May 9 '11 at 20:35