开发者

Checkbox array to string help: Implode $_POST and return values to same $_POST?

开发者 https://www.devze.com 2023-03-04 13:47 出处:网络
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:

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"开发者_如何学C,"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).


EDIT: Be sure to var_dump($_SESSION) before attempting to implode() it, in case there's some other array already in there.

So it seems your error is that your $_POST contains an array, and you are converting $_POST directly into $_SESSION then attempting to implode() it into a string as is. implode() trips over that array.

You can ignore $_SESSION['rolfz'] quite simply:

foreach($_POST as $k=>$v) {
  if ($k != 'rolfz') {
    $_SESSION[$k]=$v;
  }
}

Then handle it with other means as you have suggested.

Better yet, store $_POST into $_SESSION as is, but use a different temporary array to filter out rolfz:

$_SESSION = $_POST; // will overwrite anything already in $_SESSION

$tmp_array = array();
foreach($_POST as $k=>$v) {
  if ($k != 'rolfz') {
    $tmp_array[$k]=$v;
  }
}

// Then implode() $tmp_array and write to file.

EDIT 5/11/2011: Since overwriting $_POST doesn't seem to be working, try using an intermediate value and removing the orignial rolfz key:

if (isset($_POST['rolfz'])) {
  $temp = implode(",", $_POST['rolfz']);
  unset($_POST['rolfz']);
  $_POST['rolfz'] = $temp;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消