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 7 of 7
  1. #1
    Regular Coder
    Join Date
    Nov 2011
    Location
    Preston, UK
    Posts
    131
    Thanks
    36
    Thanked 0 Times in 0 Posts

    How can I pick up two variables at the same time.

    Hi Guys...
    I have almost finidhed my cart, but I have a problem that I don'yt know how to code.
    From the form:
    PHP Code:
    $output[] = '<tr>';  
    $output[] = '<td align="center" colspan="2"><br><br><font color="#ff0000">Please select from the following</font></td>';   if ($row['accapproved'] == '1')  {
    $output[] = '</tr>'
    $output[] = '<tr height="30">'

    $output[] = '<td align="center" colspan="2">On account: <input type="radio" name="delivery" value="account" />&nbsp;&nbsp;';
    $output[] = '</td></tr>';
        }
    $output[] = '<tr height="30">'
    $output[] = '<td align="center" colspan="2">Cash on Delivery: <input type="radio" name="delivery" value="cash" />&nbsp;&nbsp;';
    $output[] = 'Collect and Pay: <input type="radio" name="delivery" value="collect" /></td>';  
    $output[] = '</tr>'
    From the reeceiving page:
    PHP Code:
    $account="";  
    if(isset(
    $_POST['account'])){  
    $account=$_POST['account'];
    $account="Yes";
    }  
    $collect="";  
    if(isset(
    $_POST['collect'])){  
    $collect=$_POST['collect'];
    $collect="Yes";  
    }  
    $cash="";  
    if(isset(
    $_POST['cash'])){  
    $cash=$_POST['cash']; 
    $cash="Yes"

    I know that I need to change the if(isset($_POST['cash'])) to if(isset($_POST['delivery']))
    But the problem is that ALL the options are selected as YES.
    I need to somehow select:
    if(isset($_POST['cash'])) AND value = YES, in order to just select one.

    I know that one solution would be to change from radio button to select box, but some idiot would select ALL options.

    Can anybody advice me how to solve my problem ???

    Thanks,
    The MAN, The MYTH, The LEGEND:
    John C
    ________________________________
    Support your local Country Music Club

  • #2
    Master Coder mlseim's Avatar
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,373
    Thanks
    8
    Thanked 1,075 Times in 1,066 Posts
    With radio buttons they cannot select ALL of them if the "name" is the same.
    They can do that with checkboxes, but not radio buttons.

    See this test script.
    You have already given each radio button a value in your form.
    Now you just need to see which one they picked ...

    PHP Code:

    <?php

    $option
    ="none";
    if(
    $_POST['delivery'])){
    $option=$_POST['delivery'];
    )

    echo 
    "The radio button they selected was: ".$option;

    ?>

    EDIT: fixed typo



    .
    Last edited by mlseim; 11-26-2012 at 05:04 PM.

  • Users who have thanked mlseim for this post:

    countrydj (11-26-2012)

  • #3
    Regular Coder
    Join Date
    Nov 2011
    Location
    Preston, UK
    Posts
    131
    Thanks
    36
    Thanked 0 Times in 0 Posts
    Hi mlseim..
    Very many thanks for your help - ONCE AGAIN.
    You actually made a slight mistake in your code. The lst bracket was ) not }
    I soon managed to sort this out and then recode the rest of the script to work as I need it to.

    Don't know whether you remember, but back around June this year you helped me with this project.
    You have no idea how much I appreciated your help then, as well as now.
    I will NEVER FORGET !!!

    Thank you...
    The MAN, The MYTH, The LEGEND:
    John C
    ________________________________
    Support your local Country Music Club

  • #4
    Regular Coder
    Join Date
    Nov 2011
    Location
    Preston, UK
    Posts
    131
    Thanks
    36
    Thanked 0 Times in 0 Posts
    Hi
    I wonder if I can ask your advice.
    What is the difference between:
    PHP Code:
    $delivery="none";
    if(
    $_POST['delivery']){
    $delivery=$_POST['delivery'];

    and
    PHP Code:
    $delivery="none";
    if(isset(
    $_POST['delivery'])){
    $delivery=$_POST['delivery'];

    Thanks...
    The MAN, The MYTH, The LEGEND:
    John C
    ________________________________
    Support your local Country Music Club

  • #5
    Master Coder mlseim's Avatar
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,373
    Thanks
    8
    Thanked 1,075 Times in 1,066 Posts
    The best way to explain it ...

    Upload this script, call it "test.php" and try it.
    PHP Code:
    <?php

    // only test the variable if the form has been submitted.
    if(isset($_POST['submit'])){

    // try seeing the variable without using ISSET
    if($_POST['x']){
    echo 
    "I detected ".$_POST['x']." without using ISSET <br />";
    }

    // try seeing the variable using ISSET
    if(isset($_POST['x'])){
    echo 
    "I detected ".$_POST['x']." using ISSET <br />";
    }
    }
    ?>
    <br /><br />
    <form method="post">
    1) submit with an empty box.<br />
    2) submit with a zero (0) in the box.<br />
    3) submit with any other number or text in the box.<br />
    <input type="text" name="x">
    <input type="submit" name="submit" value="submit">
    </form>
    It shows what happens if someone submits with a blank box, or a box with a zero (0) integer.
    Important if you expect the integer zero (0) to be entered. PHP will see this as a 'false' in the IF statement.
    You can combine them to see if they entered nothing. If ISSET and then if it's not empty. If nothing is
    entered, you may want to do something about it ... or maybe you don't care. Depends on your specifications.

    There is also the EMPTY command. I know ... it is sort of confusing.

    Thinking about it now ... I probably mis-use it many times. Test your forms for all possible user entries.

    Fou Lu probaby can explain it better than anyone here.

    .
    Last edited by mlseim; 11-26-2012 at 03:09 PM.

  • Users who have thanked mlseim for this post:

    countrydj (11-26-2012)

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Yep, isset is simply to check the existence of a variable, and that it is not null. It otherwise does not care about the value of the variable. Input from forms cannot be null, only empty strings.
    PHP Code:
    if ($_POST['field']) 
    If you have not posted your form, or you have provided the form with false equivalent data (array(), "", "0", false, although from a form you will never get array()), that will not pass the check and it will trigger an error notice if not posted.
    PHP Code:
    if (isset($_POST['field'])) 
    Simply checks if we have been provided with a field called 'field'. It only cares if its not null, but as mentioned you cannot receive null from a form, only an empty string. I'd need to test to be 100% sure, but I believe if you managed to pump a null through it, Apache or IIS would convert it during the successful fields checks. Isset does not throw an error if the variable doesn't exist (as is its purpose).

    With the example you have here, you can use a ternary operation for it: $delivery = isset($_POST['delivery']) ? $_POST['delivery'] : 'none';. Just like any if, you can group as many conditions as you want into it.

    Empty checks that a variable exists and is an empty value (false equivalent). Although faith in my memory is kinda diminishing a bit, I'm quite sure that empty() used to trigger notice when the variable does not exist. Currently it does not. Empty can be used in place of isset if you demand that a provided value cannot be a false equivalent. This is unlike isset as isset considers "" or 0 to be valid. Empty does not. Personally, I use empty during validation phases, but not during initial verification. Don't use empty on something like a quantity, otherwise it will never pass the 0 check (for which you likely need to remove it from a cart for example instead of ignoring the input completely).

    So to sum it up (which mlseim did):
    PHP Code:
    if ($variable// condition of the VALUE of the variable
    if (isset($variable)) // $variable exists and is not null
    if (empty($variable)) // $variable exists and is false equivalent 
    Isset also has one advantage on empty; you can use many and all have to pass. Works well with forms:
    PHP Code:
    if (isset($_POST['username'], $_POST['password'])) 

  • Users who have thanked Fou-Lu for this post:

    countrydj (11-26-2012)

  • #7
    Regular Coder
    Join Date
    Nov 2011
    Location
    Preston, UK
    Posts
    131
    Thanks
    36
    Thanked 0 Times in 0 Posts
    Very many thanks to both of you.
    The MAN, The MYTH, The LEGEND:
    John C
    ________________________________
    Support your local Country Music Club


  •  

    Posting Permissions

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