#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    25
    Rep Power
    0

    Arrow PHP if/else can't detect option value (please help)


    Hello DevShed Forums,

    I have been spending countless hours tackling this bit of code but I have unfortunately failed many times. The idea seems to be simple, and I could've sworn I've done this before. Something has to be wrong... I decided I would get some help and hopefully someone here would be able to find out why this code is not working.

    I want to create a PHP if/else statement that will determine the value of $postage. I have a simple HTML select option form and I was using PHP to figure out what selection the user has made upon clicking the submit button.

    Here is the PHP:
    PHP Code:
    $delivery $_POST['delivery_speed'];

    if (
    $delivery == "0" || "FREE - Standard (2-3 business days)") { 
    $postage 100
    } else if (
    $delivery == "1" || "$15.00 - Next Day") { 
    $postage 15


    $total $pcost $postage
    Here is the HTML bit:
    Code:
    <form action="https://www.paypal.com/cgi-bin/webscr" method="POST">				
    <select id="delivery_speed" style="float:right;" name="delivery_speed" onChange="calcForm(this.form);"> 
    <option value="0">FREE - Standard (2-3 business days)</option> 
    <option value="1">$15.00 - Next Day</option> 
    </select>
    I would appreciate all the help I can get!

    Thanks,
    Saves
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Hi,

    the syntax and the content of the conditions makes no sense. The || operator is the logical "or". You use it to combine two boolean expressions:

    PHP Code:
    $age >= 21 || $bribe >= 100 
    It seems you have tried to literally translate human language into code ("The delivery is this or that"). This doesn't work.

    The conditions themselves also make no sense. The form submits the values of the options (i. e. "0" or "1"), not the labels.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    25
    Rep Power
    0
    Originally Posted by Jacques1
    Hi,

    the syntax and the content of the conditions makes no sense. The || operator is the logical "or". You use it to combine two boolean expressions:

    PHP Code:
    $age >= 21 || $bribe >= 100 
    It seems you have tried to literally translate human language into code ("The delivery is this or that"). This doesn't work.

    The conditions themselves also make no sense. The form submits the values of the options (i. e. "0" or "1"), not the labels.

    Hello, thank you for replying. A couple clarifications I would like to make... I originally tried if($delivery == "0") {} but it would not run the function. However, the only time I ever got it to work was when I typed if($delivery == "0" || "FREE - Standard (2-3 business days)") {}

    I don't know why and I know that it would've made more sense to type it this way: if($delivery == "0" || $delivery == "FREE - Standard (2-3 business days)" {}

    I've also tried it where it was just | $delivery == "FREE - Standard (2-3 business days)" {} and that didn't work either.

    For some reason I am not able to retrieve the selected value from the dropdown menu. Why is that? Is there anything else wrong with the code?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2004
    Posts
    2,972
    Rep Power
    375
    you just need to do

    PHP Code:

    if ( $delivery == "0" ) {
     echo 
    "1";
      
    //set the value

    } else {
    echo 
    "2";
     
    //set thevalue 


    those echoes are just to test which "branch" is executed so you can test/debug

    or You can do it a bit more sophisticated, create a function something like

    function testing ( $message, $flag){
    echo $message;
    }

    then instead of echoing.

    do testing ("message", $flag);

    then set flag somewhere at the top, this way you dont constantly have to comment/uncomment debug code and instead set it using flag, which can come from a session OR $_GET/$_POST variable
  8. #5
  9. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Originally Posted by Saves
    However, the only time I ever got it to work was when I typed if($delivery == "0" || "FREE - Standard (2-3 business days)") {}
    That's because this condition is always true. It's a tautology.

    Non-empty strings (except "0") always count as true in a boolean context. So what you have there is

    PHP Code:
    $some_expression || true 
    And this is always true, regardless of what $some_expression says.

    "Fixing" code through trial-and-error generally isn't a good idea. If there's a bug, then investigate it. In your case, the most obvious thing to do would be to check the POST parameters:

    PHP Code:
    var_dump($_POST); 
    So what does that say?
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    25
    Rep Power
    0
    It says
    PHP Code:
    Plan array(0) { } 
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    25
    Rep Power
    0
    Originally Posted by Jacques1
    That's because this condition is always true. It's a tautology.

    Non-empty strings (except "0") always count as true in a boolean context. So what you have there is

    PHP Code:
    $some_expression || true 
    And this is always true, regardless of what $some_expression says.

    "Fixing" code through trial-and-error generally isn't a good idea. If there's a bug, then investigate it. In your case, the most obvious thing to do would be to check the POST parameters:

    PHP Code:
    var_dump($_POST); 
    So what does that say?
    I don't understand what is wrong with my code. It looks correct. I made sure I didn't spell anything wrong and I didn't forget and semicolons or of that sort...
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    25
    Rep Power
    0
    Originally Posted by paulh1983
    you just need to do

    PHP Code:

    if ( $delivery == "0" ) {
     echo 
    "1";
      
    //set the value

    } else {
    echo 
    "2";
     
    //set thevalue 


    those echoes are just to test which "branch" is executed so you can test/debug

    or You can do it a bit more sophisticated, create a function something like

    function testing ( $message, $flag){
    echo $message;
    }

    then instead of echoing.

    do testing ("message", $flag);

    then set flag somewhere at the top, this way you dont constantly have to comment/uncomment debug code and instead set it using flag, which can come from a session OR $_GET/$_POST variable
    Oh I'm sorry I totally missed your message! I really appreciate you taking your time and reading my problem. I am at church at the moment but as soon as ill get out ill try out your suggestion and let you know what results I get
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    25
    Rep Power
    0
    It was echoing out "2" at the top of the webpage before I even made a selection and hit submit
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2004
    Posts
    2,972
    Rep Power
    375
    that would happen if you do not have the a check to see whether the form has been posted or not so all this logic should go inside:

    [code]
    if ( form has been submitted ) {
    // your code here

    } else {
    // display the form
    }
    [/php]

    obv its not proper code as i do not want to do the work for you

    btw you should have tested that code by filling in the form and clicking submit
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    25
    Rep Power
    0
    Are you sure that is what I need to do? It doesn't really make sense to me... Perhaps I need to show you more of my html code. The only part of the form is the drop down menu. And the submit button is a paypal ipn image input.

    Code:
    <div style="margin-top: 10px; margin-bottom: 10px; float:right; width: 720px;"> 
    <form action="https://www.paypal.com/cgi-bin/webscr" method="POST">				
    <select id="delivery_speed" style="float:right;" name="delivery_speed" onChange="calcForm(this.form);"> 
    	<option value="0">FREE - Standard (2-3 business days)</option> 
    	<option value="1">$15.00 - Next Day</option> 
    </select><br />                                
    </div>
        
    <div style="float:right;width: 720px; height:16px; background-image:url(images/total_bg.png);"><font style="float:right; margin-right: 5px;">Total: $<font style="display: inline-block;" type="text" name="total" id="total"><font id="totalPrice"></font></font></font></div><br /></div>
                
    <div style="margin-top: 10px; float:right;">
    <input type="hidden" name="cmd" value="_xclick">
    <input type="hidden" name="business" value="psomas_1351918316_biz@gmail.com">
    <input type="hidden" name="item_name" value="<?php echo $pname; ?> Mastering Plan">
    <input type="hidden" name="item_number" value="<?php echo $id; ?>">
    <input type="hidden" name="amount" value="<?php echo $total; ?>">
    <input type="hidden" name="no_shipping" value="1">
    <input type="hidden" name="no_note" value="1">     <!--- MAYBE --->
    <input type="hidden" name="currency_code" value="USD">
    <input type="hidden" name="lc" value="GB">
    <input type="hidden" name="bn" value="PP-BuyNowBF">
    <input type="hidden" name="return" value="../complete.php">
    <input type="hidden" name="cancel_return" value="">
    <input type="hidden" name="rm" value="2">
    <input type="hidden" name="notify_url" value="" />
    <input type="hidden" name="custom" value="">
    <input type="image" src="https://www.paypal.com/en_US/i/btn/btn_xpressCheckout.gif" name="submit" alt="Make payments with PayPal - its fast, free and secure!">
    
    </form>
    </div>
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2004
    Posts
    2,972
    Rep Power
    375
    yes because the first time i see this page, i havent made a decision yet on what postage i will choose as your customer so how can you run that code? that code will only get run & you can choose to show the form AGAIN after i choose the correct postage.
  24. #13
  25. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,614
    Rep Power
    1945
    You are sending the information to PayPal, have you validated/checked that PayPal does send back the information? using POST or GET?
    (have no experience with PayPal and its "API")
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2004
    Posts
    2,972
    Rep Power
    375
    aah I see. In this case the easiest solution would be (and i assume you will update a hidden form field when i choose a postage option, right?) to use jquery/ajax to change the value of the hidden field relating to the postage when/if i change the value of select.
  28. #15
  29. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    25
    Rep Power
    0
    Originally Posted by MrFujin
    You are sending the information to PayPal, have you validated/checked that PayPal does send back the information? using POST or GET?
    (have no experience with PayPal and its "API")
    I'm sending information(variables) to PayPal. And yes I know that you can because all my other variables are working. But for some reason this form cannot recognize the Value for each <option></option> tag.

IMN logo majestic logo threadwatch logo seochat tools logo