homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

Not getting complete form data to save to text

 4:16 am on Oct 22, 2011 (gmt 0)


I've been up for a couple of nights trying to get the following code to save the complete form data.

I am using the following code to save from a form survey


// Collect all the form data into an easier variable.
$form_data = ($_POST);

$timestamp = date('Y-m-d-H-i-s');
$mount_point = "c:/test";

$survey_filename = $mount_point ."/" .$form_data[':SURVEY:'] ."-" .$timestamp .".txt";

if (($survey_fp = fopen($survey_filename, "wb")) === FALSE)

//Could not write to flash trying internal disk
$survey_filename = "c:/test" .$form_data[':SURVEY:'] ."-" .$timestamp .".txt";
if (($survey_fp = fopen($survey_filename, "wb")) === FALSE)
die('FATAL ERROR: Could not save survey');

// Write form data
foreach ($form_data as $name=>$value)

fwrite($survey_fp, "$name=$value\r\n");

// close the survey text file
fclose ($survey_fp);


The problem is that the code would post single response, but would only output the last response from multiple response questions, like this:


But I want to get multiple response questions to show up and they should look like this:


I'm still looking into it, but my experience with php script is limited, so please let me know if you have any hits as to what the problem might be.

Thanks in advance



 6:45 am on Oct 22, 2011 (gmt 0)

I think we might need to see the HTML form... how are the 'multiple response questions' marked up? Mutliple select? Separate forms?


 2:56 pm on Oct 22, 2011 (gmt 0)

It's marked up as multiple select. The HTML is very long because I used a survey software to crate it, but if you still want to see it the link to the html is bellow:

[mediafire.com ]


 4:20 pm on Oct 24, 2011 (gmt 0)

But I want to get multiple response questions to show up and they should look like this:

Multiple selects and other elements can in the form of an array.

foreach ($form_data as $name=>$value){
fwrite($survey_fp, "$name=");
if ($name=='my_multiple_select') {
$selected_count = count($value)-1;
for ($i=0;$i<=$selected_count;$i++) {
if ($i<$selected_count) {
else { $fwrite($survey_fp,$value\r\n"); }
// close the survey text file
fclose ($survey_fp);

Something "like that" should lead the way.


 11:19 pm on Oct 25, 2011 (gmt 0)

Thanks rocknbil, I will test that out and hopefully all goes well, but I will let you know how it turns out.

Thank you very much!


 10:45 pm on Oct 26, 2011 (gmt 0)

...but would only output the last response from multiple response questions

As rocknbil suggests "multiple" select's need to return an array that you then step through in order to get all the values that the user has selected. However, if you are currently getting back just the last value selected (a single string) then that sounds as if you have omitted the [] (square brackets) from the end of name for that select control in the HTML...

For example,
<select name="myselect[]" id="myselect" multiple="multiple" size="10">

The square brackets on the name are required in order to return an array of values. Otherwise the selected values will overwrite the preceding one and you will only end up with the last value selected.


 3:49 pm on Oct 27, 2011 (gmt 0)

I wasn't sure if you needed [] due to the select object being a single element, but you do. :-)

if (isset ($_POST['mytest']) and ! empty($_POST['mytest'])) {
foreach ($_POST['mytest'] as $value) {
echo "<p>$value selected</p>\n";
else {
echo '
<form action="test-multiple.php" method="post">
<p>Select multiple items with CTRL or shift.</p>
<p><select multiple="multiple" name="mytest[]" id="mytest" size="12">
<option value="">Select from below</option>
<option value="red">red</option>
<option value="green">green</option>
<option value="blue">blue</option>
<option value="yellow">yellow</option>
<option value="orange">orange</option>
<option value="violet">violet</option>
<option value="gold">gold</option>
<option value="silver">silver</option>
<option value="Always Sunny">Always Sunny</option>
<p><input type="submit" value="test it"></p>

' multiple="multiple" ' only needed for XHTML doctypes, just use ' select multiple name="... ' for any HTML doctype. Which you probably should be using.


 2:09 pm on Feb 28, 2012 (gmt 0)

I know this might be a little late, but I just want to let rocknbil know that the solution he provided WORKED! We ended up creating some sort of small script that would insert the "[]" after multiple objects

Thanks Rocknbil ;)


 3:54 pm on Feb 28, 2012 (gmt 0)

I hope you don't mind me pointing out that the script is somewhat insecure. If someone replaces the value of $_POST[':SURVEY:'] with for example '../windows/notepad.exe', the script will overwrite your NotePad with the survey. You really need to check the value of $_POST[':SURVEY:'].

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved