Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder
    Join Date
    Oct 2009
    Posts
    177
    Thanks
    20
    Thanked 2 Times in 1 Post

    If checkbox is selected...

    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:

    https://cms.paypal.com/us/cgi-bin/?&..._htmlvariables

    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);
    fclose($Handle);

    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.

  • #2
    New Coder
    Join Date
    Mar 2010
    Posts
    26
    Thanks
    3
    Thanked 1 Time in 1 Post
    Code:
    if($_POST['os0'] == "on")

  • #3
    Regular Coder
    Join Date
    Oct 2009
    Posts
    177
    Thanks
    20
    Thanked 2 Times in 1 Post
    so the total code would look like this:

    Code:
    if($_POST['os0'] == "on") {
    $File = "..//mailer/data/email.txt";
    $Handle = fopen($File, 'a');
    $MyData = "\"". $payer_email . "\"" . "," . "\"" . $invoice . "\"" . "," . "\"" . $custom . "\"" . "\r\n";
    fwrite($Handle, $MyData);
    fclose($Handle);
    }
    And if the checkbox wasn't checked, it would not run that piece of code?

  • #4
    New Coder
    Join Date
    Mar 2010
    Posts
    26
    Thanks
    3
    Thanked 1 Time in 1 Post
    Correct

  • #5
    Regular Coder
    Join Date
    Oct 2009
    Posts
    177
    Thanks
    20
    Thanked 2 Times in 1 Post
    And this is the correct form checkbox:

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

  • #6
    New Coder
    Join Date
    Mar 2010
    Posts
    26
    Thanks
    3
    Thanked 1 Time in 1 Post
    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:

    Code:
    <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

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

  • #7
    Regular Coder
    Join Date
    Oct 2009
    Posts
    177
    Thanks
    20
    Thanked 2 Times in 1 Post
    Ok, and in the PHP part, does "on" mean the same thing as checked?

  • #8
    New Coder
    Join Date
    Mar 2010
    Posts
    26
    Thanks
    3
    Thanked 1 Time in 1 Post
    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'] == "";

  • #9
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,853
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    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().
    PHP Code:
    if(isset($_POST['os0'])) 
    Otherwise, you'll get "unknown index" warning when you execute your code in a STRICT error_reporting environment.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •