View Full Version : If checkbox is selected...

03-11-2010, 11:54 PM
Hey all. I am currently using a PayPal IPN script let me give a small background on how 2 parts work. One part takes paypal variables and populates them into the script. It does so by this:

// assign posted variables to local variables
$item_name = $_POST['item_name'];
$item_number = $_POST['item_number'];
$payment_status = $_POST['payment_status'];
$payment_amount = $_POST['mc_gross'];
$payment_currency = $_POST['mc_currency'];
$txn_id = $_POST['txn_id'];
$receiver_email = $_POST['receiver_email'];
$payer_email = $_POST['payer_email'];
$custom = $_POST['custom'];
$payment_date = $_POST['payment_date'];
$invoice = $_POST['invoice'];

I also have to use paypal variables for this. There's a list here:


Okay, onto the help part. I have my IPN script send the user's email address to a text file which I store my mailing list data in. It does so by this:

$File = "..//mailer/data/email.txt";
$Handle = fopen($File, 'a');
$MyData = "\"". $payer_email . "\"" . "," . "\"" . $invoice . "\"" . "," . "\"" . $custom . "\"" . "\r\n";
fwrite($Handle, $MyData);

Basically, I want the user to check a checkbox, and it runs that part of the script. If not, it doesn't. This allows the user to choose to subscribe or not. So I need an if function and I need to somehow see if the checkbox is checked in the IPN script. Here's my form code:

<input type="checkbox" name="os0" value="yes" checked>

Thanks! If you need more information, I can try and assist.

03-11-2010, 11:56 PM
if($_POST['os0'] == "on")

03-12-2010, 02:33 AM
so the total code would look like this:

if($_POST['os0'] == "on") {
$File = "..//mailer/data/email.txt";
$Handle = fopen($File, 'a');
$MyData = "\"". $payer_email . "\"" . "," . "\"" . $invoice . "\"" . "," . "\"" . $custom . "\"" . "\r\n";
fwrite($Handle, $MyData);

And if the checkbox wasn't checked, it would not run that piece of code?

03-12-2010, 09:13 AM

03-12-2010, 11:19 AM
And this is the correct form checkbox:

<input type="checkbox" name="os0" checked>

03-12-2010, 12:47 PM
Most browsers will accept it, but if you want to keep strict rules, you cannot have an option without a value, therefore you have to write:

<input type="checkbox" name="os0" checked="checked">

In addition any tag requires an end, but there is no </input>, therefore by strict rules it reads

<input type="checkbox" name="os0" checked="checked" />

03-12-2010, 05:24 PM
Ok, and in the PHP part, does "on" mean the same thing as checked?

03-12-2010, 11:33 PM
Yes, in HTML it is "checked", PHP receives it as "on", both meaning the same thing.
Only the counterpart doesn´t exist in PHP, in HTML "unchecked" will arrive in PHP as
$_POST['os0'] == "";

03-13-2010, 08:44 AM
In fact, the form submits checkbox and radio inputs only if they are checked/selected. Thus, the best way to check the presence of a checkbox's value (or any other input element) in a page is using isset().

Otherwise, you'll get "unknown index" warning when you execute your code in a STRICT error_reporting environment.