Link to home
Start Free TrialLog in
Avatar of planetemails
planetemails

asked on

Only CHECKED checkboxes saved to CSV

This is a submit form which will save all submitted infos to a CSV file named 'data.csv'
Problem is on the checkboxes (there are 3 checkboxes) ...
What I want, if user checked 1 of them, or 2 of them, OR all of them then it should save ONLY the checked infos to the CSV file.
Please help me fix/modify this code below:

<?php

if (isset($_REQUEST['name']))
  $name = $_REQUEST['name'];

if (isset($_REQUEST['email']))
  $email = $_REQUEST['email'];

// Generate CSV line
$line = $name.",".$email."," ????? "\r\n";

// open file
$f=@fopen("data.csv","a");
if (!$f) {
echo "File open error!";
} else {
fwrite($f,$line);
fclose($f);
}

?>
<form method="post">
Name: <input type="text" name="name" /><br/>
Email: <input type="text" name="email" /><br/>
Payment method:<br />
<input type="checkbox" name="payment1" value="Paypal" />Paypal<br/>
<input type="checkbox" name="payment2" value="Neteller" />Neteller<br/>
<input type="checkbox" name="payment3" value="eGold" />eGold<br/>
</form>

If ALL checkboxes are CHECKED then the data on the csv file should looks like this:
John Doe,johndoe@yahoo.com,"Paypal,Neteller,eGold"

OR if only 2 are CHECKED then data adjusted to:
John Doe,johndoe@yahoo.com,"Paypal,eGold"
Avatar of elfe69
elfe69
Flag of Switzerland image

Unchecked boxes are not sent to the server by the browser so you have to know them all, you could add the following code:

$payment = "";
if (isset($_REQUEST['payment1'])) {
  $payment .= $_REQUEST['payment1'];
}
if (isset($_REQUEST['payment2'])) {
  if ($payment !== "") {
    $payment .= ",";
  }
  $payment .= $_REQUEST['payment2'];
}
if (isset($_REQUEST['payment3'])) {
  if ($payment !== "") {
    $payment .= ",";
  }
  $name .= $_REQUEST['payment3'];
}
$payment = "\"".$payment."\"";
ASKER CERTIFIED SOLUTION
Avatar of elfe69
elfe69
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of planetemails
planetemails

ASKER

Cool man ... Thanks ... :)