复选框数组到字符串帮助:内爆 $_POST 并将值返回到相同的 $_POST?

发布于 2024-11-06 04:59:16 字数 2780 浏览 1 评论 0原文

我试图将简短形式的结果存储到平面文件(例如 .text 文件)。该文件将作为分隔文件导入到 Excel 中。该表单具有复选框,因此可以将其设置为数组:

<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 />

是否可以从 $_POST['rolfz'] 内爆数组并将结果返回到 $_POST['rolfz'] ?所需的结果是一个可以存储到文本文件中的字符串。请参阅下面的代码:

<?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']);
    }

总体目标是将 $_SESSION 中的所有值存储到文本文件中。但是,当我尝试运行代码时,我收到“注意:第 14 行 E:\cgi-bin\thankyou.php 中的数组到字符串转换”。该错误告诉我,在下面的代码中我试图将数组用作字符串。

// 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();
?>

如果我所做的事情是错误的或不可能的,是否可以使用替代语句来代替 foreach($_POST as $k=>$v) 以便它忽略 $ _POST['rolfz']?这样我就可以自己处理 $_POST['rolfz'] 吗?

编辑 (2011 年 5 月 10 日): 内爆前:

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" } } 

内爆后:

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" } } 

编辑 (5/10/11): 如果您尝试执行类似的解决方案,请参阅迈克尔的解决方案。请注意,我是个白痴,在代码中使用了错误的复选框名称(应该是 roflz 而不是 rolfz)。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

诗酒趁年少 2024-11-13 04:59:16

编辑:在尝试 implode() 之前,请务必先var_dump($_SESSION),以防其中已有其他数组。

所以看来您的错误是您的 $_POST 包含一个数组,并且您将 $_POST 直接转换为 $_SESSION 然后尝试 implode() 将其按原样转换为字符串。 implode() 遍历该数组。

您可以非常简单地忽略 $_SESSION['rolfz']

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

然后按照您建议的其他方式处理它。

更好的是,将 $_POST 按原样存储到 $_SESSION 中,但使用不同的临时数组来过滤 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
由于覆盖 $_POST 似乎不起作用,请尝试使用中间值并删除原始 rolfz 键:

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

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文