复选框数组到字符串帮助:内爆 $_POST 并将值返回到相同的 $_POST?
我试图将简短形式的结果存储到平面文件(例如 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
编辑:在尝试
implode()
之前,请务必先var_dump($_SESSION)
,以防其中已有其他数组。所以看来您的错误是您的
$_POST
包含一个数组,并且您将$_POST
直接转换为$_SESSION
然后尝试implode()
将其按原样转换为字符串。implode()
遍历该数组。您可以非常简单地忽略
$_SESSION['rolfz']
:然后按照您建议的其他方式处理它。
更好的是,将
$_POST
按原样存储到$_SESSION
中,但使用不同的临时数组来过滤rolfz
:EDIT 5/ 11/2011:
由于覆盖
$_POST
似乎不起作用,请尝试使用中间值并删除原始rolfz
键:EDIT: Be sure to
var_dump($_SESSION)
before attempting toimplode()
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 toimplode()
it into a string as is.implode()
trips over that array.You can ignore
$_SESSION['rolfz']
quite simply: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 outrolfz
:EDIT 5/11/2011:
Since overwriting
$_POST
doesn't seem to be working, try using an intermediate value and removing the orignialrolfz
key: