문제

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).

올바른 솔루션이 없습니다

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top