Current location: Hot Scripts Forums » Programming Languages » PHP » Save data in file


Save data in file

Reply
  #1 (permalink)  
Old 07-11-06, 08:08 AM
zoliky's Avatar
zoliky zoliky is offline
Aspiring Coder
 
Join Date: Jun 2006
Posts: 537
Thanks: 0
Thanked 0 Times in 0 Posts
Save data in file

Hi,
I have more checkbox in HTML form :
Code:
<input type="checkbox" name="Question10" value="work">work<br>
<input type="checkbox" name="Question10" value="domestic responsibilities"> domestic responsibilities<br>
<input type="checkbox" name="Question10" value="holidays">holidays<br>
When user check more values, I need to write each value to file separated by ; mark.

my example save only one value
PHP Code:

$fp fopen('questdb.txt''a+');

$data "Guest\t" "{$_POST['Question10']}\n";
fwrite ($fp$data);
fclose ($fp); 
Anyone help me a bit?

Last edited by nico_swd; 07-11-06 at 08:16 AM. Reason: [php] wrappers
Reply With Quote
  #2 (permalink)  
Old 07-11-06, 08:16 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
Make an array of the checkboxes, like this
Code:
<input type="checkbox" name="Question10[]" value="work">work<br>
<input type="checkbox" name="Question10[]" value="domestic responsibilities"> domestic responsibilities<br>
<input type="checkbox" name="Question10[]" value="holidays">holidays<br>
And then use implode() to get the semicolon in between each submission for Question10.
PHP Code:

$data "Guest\t" implode(';'$_POST['Question10']) ."\n"
Reply With Quote
  #3 (permalink)  
Old 07-11-06, 09:00 AM
zoliky's Avatar
zoliky zoliky is offline
Aspiring Coder
 
Join Date: Jun 2006
Posts: 537
Thanks: 0
Thanked 0 Times in 0 Posts
thanks The only problem is, I don't want to see ; mark if only one box checked.

Thanks !
Reply With Quote
  #4 (permalink)  
Old 07-11-06, 09:07 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
The semicolon won't be imploded if only one is checked.
Reply With Quote
  #5 (permalink)  
Old 07-11-06, 10:04 AM
zoliky's Avatar
zoliky zoliky is offline
Aspiring Coder
 
Join Date: Jun 2006
Posts: 537
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks for help.

Code:
<input type="radio" name="Question1" value="strings">strings</label>
<br>
<label>
<input type="radio" name="Question1" value="wind">wind</label>
<br>
<label>
<input type="radio" name="Question1" value="brass">brass</label>
<br>
<label>
<input type="radio" name="Question1" value="percussion">percussion</label>
</p>
Code:
<input type="checkbox" name="Question4[]" value="work">work<br>
<input type="checkbox" name="Question4[]" value="domestic">domestic responsibilities<br>
<input type="checkbox" name="Question4[]" value="holidays">holidays<br>
Code:
<input type="checkbox" name="Question5[]" value="no">no<br>
<input type="checkbox" name="Question5[]" value="would prefer different day">would prefer a different day<br>
<input type="checkbox" name="Question5[]" value="would prefer earlier start">would prefer an earlier start<br>
I need to print all selected value to file.
A user select one thing with radio button, and more things with checkboxes.

PHP Code:

$fp fopen('questdb.txt''a+');

$data "Guest\t" "{$_POST['Question1']}"|" .
             
implode(';'$_POST['Question4']) . "|" .
             
implode(';'$_POST['Question5']) . "|" "\n"
I want to write each value to file. Every value need separated with | and multiple values separated with ;

When all checkbox are empty in Question4 I get the following error: Warning: implode(): Bad arguments.

I need ideas, help. Please !

Last edited by zoliky; 07-11-06 at 10:07 AM.
Reply With Quote
  #6 (permalink)  
Old 07-11-06, 10:37 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
Do you want all fields to be obligated to fill out?
Reply With Quote
  #7 (permalink)  
Old 07-11-06, 10:57 AM
zoliky's Avatar
zoliky zoliky is offline
Aspiring Coder
 
Join Date: Jun 2006
Posts: 537
Thanks: 0
Thanked 0 Times in 0 Posts
No, I don't need all fields checked.

I write this at top of page for users:

Quote:
Please complete as many of the following questions as you are able to, or are relevant to you. Leave blank anything you don't want to answer. At the end, press the Submit button.
I need a similar thing:

http://www.amherst.edu/it/software/p...stutorial.html
But I need for checkboxes.

I get an error if nothing checked in Question4. Any idea, to solve this problem?

The code:

PHP Code:

$data .= "{$_POST['Question1']} |";

$data .= implode(';'$_POST['Question4']);
$data .= "\n"
Reply With Quote
  #8 (permalink)  
Old 07-11-06, 11:03 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
Hm, give this a try.
PHP Code:

$questions = array();


if (!empty(
$_POST['Question1']))
{
    
$questions[] = $_POST['Question1'];
}    
if (
sizeof($_POST['Question4']) > 0)
{
    
$questions[] = implode(';'$_POST['Question4']);
}
if (
sizeof($_POST['Question5']) > 0)
{
    
$questions[] = implode(';'$_POST['Question5']);
}

$data implode('|'$questions); 
Reply With Quote
  #9 (permalink)  
Old 07-11-06, 11:35 AM
zoliky's Avatar
zoliky zoliky is offline
Aspiring Coder
 
Join Date: Jun 2006
Posts: 537
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks you very much work great

I have 14 box (radio and checkbox), I need to add 14 if statement ? or exist a simpler way to do this ?

Thanks again ! and sorry because I ask much
Reply With Quote
  #10 (permalink)  
Old 07-12-06, 03:02 AM
zoliky's Avatar
zoliky zoliky is offline
Aspiring Coder
 
Join Date: Jun 2006
Posts: 537
Thanks: 0
Thanked 0 Times in 0 Posts
I have the following checkboxes:

Code:
       
       <input type="checkbox" name="Question7[]" value="acoustics">
        acoustics<br>
        <input type="checkbox" name="Question7[]" value="seating">
        seating<br>
        <input type="checkbox" name="Question7[]" value="lighting">
        lighting<br>
        <input type="checkbox" name="Question7[]" value="heating">
        heating<br>
        <input type="checkbox" name="Question7[]" value="facilities">
        facilities<br>
        <input type="checkbox" name="Question7[]" value="parking">
        parking<br>
        <input type="checkbox" name="Question7[]" value="accessibility">
        accessibility<br>
        <input type="checkbox" name="Question7[]" value="other">
        other &ndash; please explain</p>
      <p>
        <textarea name="Question7_other"></textarea>
If user select Other and write in textarea I want to save in this form:

other;textareatext

I put this:

Code:
<textarea name="Question7[]"></textarea>
Now, I get a semicolon ; at end and only one value selected like "acoustics".
I think empty text area has 1 caracter. PHP think this is a new value

Any idea to solve this problem?
Thank you very much!

Last edited by zoliky; 07-12-06 at 03:07 AM.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
save form data to text file hinch JavaScript 3 11-22-11 11:57 AM
ASP upload prob minority ASP 1 06-27-05 08:35 AM
fopen() save data on text file?? mhs12grade1992 PHP 1 02-18-05 03:14 PM
write to beginning of file or reverse an array of binary data abtimoteo Perl 1 01-20-04 10:09 AM


All times are GMT -5. The time now is 07:21 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.