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,"Pay pal,Netell er,eGold"
OR if only 2 are CHECKED then data adjusted to:
John Doe,johndoe@yahoo.com,"Pay pal,eGold"
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,"Pay
OR if only 2 are CHECKED then data adjusted to:
John Doe,johndoe@yahoo.com,"Pay
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Cool man ... Thanks ... :)
$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."\"";