[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 209
  • Last Modified:

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"
0
planetemails
Asked:
planetemails
  • 2
1 Solution
 
elfe69Commented:
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."\"";
0
 
elfe69Commented:
A bad variable 'name' was in the first answer, try this:

$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 .= ",";
  }
  $payment .= $_REQUEST['payment3'];
}
$payment = "\"".$payment."\"";
0
 
planetemailsAuthor Commented:
Cool man ... Thanks ... :)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now