0

I'm trying to update shipping costs based off of which radio button the user checks. I am not doing this inside of a form element so $_POST['shipping'] isn't working. Here's my code:

PHP:

    if(isset($_POST['shipping']) === "free") {
        $_SESSION['shipping_charges'] = 0;
        $ShippingCharges = 0;
        $grand = $cart->total() + 0;
    } else if(isset($_POST['shipping']) == "overnight") {
        $_SESSION['shipping_charges'] = USPSPriorityExpress($cart->total_items(), 90210);
        $ShippingCharges = USPSPriorityExpress($cart->total_items(), 90210);
        $grand = $cart->total() + USPSPriorityExpress($cart->total_items(), 90210);
    } else if(isset($_POST['shipping']) == "overnight") {
        $_SESSION['shipping_charges'] = USPSPriority($cart->total_items(), 90210);
        $ShippingCharges = USPSPriority($cart->total_items(), 90210);
        $grand = $cart->total() + USPSPriority($cart->total_items(), 90210);
    };

HTML:

  <tr>
    <td>
      <input type="radio" id="free" name="shipping" value="free" class="shippingHover" checked="checked" />
    </td>
    <td colspan="2">
      <label for="free" class="shippingHover">$0 Free shipping (2-8 business days)</label>
    </td>
  </tr>
  <tr>
    <td class="shippingHover">
      <input type="radio" id="overnight" name="shipping" class="shippingHover" value="overnight" />
    </td>
    <td colspan="2">
      <label for="overnight" class="shippingHover">
      <?php echo '$' . USPSPriorityExpress($cart->total_items(), 90210); ?>&nbsp;&nbsp;USPS Overnight:</label>
    </td>
  </tr>
  <tr>
    <td class="shippingHover">
    <input type="radio" id="3day" name="shipping" class="shippingHover" value="3day" />
    </td>
    <td colspan="2">
      <label for="3day" class="shippingHover">
      <?php echo '$' . USPSPriority($cart->total_items(), 90210); ?>&nbsp;&nbsp;USPS Priority 1-3 business days:
      </label>
    </td>
  </tr>

If you'd like to see what this looks like, go to http://www.chicmaternity.dfwcraftbeer.com/viewCart.php to view the page.

ANTVirGEO
  • 124
  • 1
  • 16
Jeff Mcbride
  • 453
  • 2
  • 6
  • 19
  • If the value isn't inside a form, how is it being sent to the server? – David Jul 12 '17 at 01:40
  • I don't need the value sent to the server. I just need to know which one is selected. I am calculating the values from a PHP function using usps's API. – Jeff Mcbride Jul 12 '17 at 01:42
  • All of the values are stored in session variables so nothing needs to be sent to the server. – Jeff Mcbride Jul 12 '17 at 01:43
  • Now it's not clear to me at all what you're trying to do. If all of the values you're comparing are in server-side session variables, then what do the radio buttons have to do with it? Why are you looking for anything in `$_POST` if you're not trying to post any values to the server? – David Jul 12 '17 at 01:44
  • 2
    PHP is server side, unless you post your data within form, you won't be able to validate using server side language. – Farrukh Ayyaz Jul 12 '17 at 01:48
  • I just need to know which shipping option the user wants (choices being: free, overnight, 3 day). I have those values already being calculated by my PHP function: USPSPriorityExpress();. But I need a way for the user to tell me which one they want and I am using radio buttons. Understand? – Jeff Mcbride Jul 12 '17 at 01:49
  • @JeffMcbride: Well, radio buttons are client-side form elements. So generally you would put them in a form and post that form to the server. Which is how they become part of the `$_POST` collection. So I guess the answer is to put them in a form? – David Jul 12 '17 at 01:53
  • PHP runs on the server. It produces the HTML to send to the user. So a couple ms AFTER PHP has ran, User see it and can check a `radio`. That is on the client side. No PHP here. You have to submit the data back to server to do PHP stuff using the data. [Read here](https://stackoverflow.com/a/13840431/2159528) – Louys Patrice Bessette Jul 12 '17 at 01:55
  • Are you asking how to get the value when you click the submit button or how to get it via jQuery AJAX call? – Machavity Jul 12 '17 at 01:58
  • That's just the problem. I am not submitting anything. I don't need a form with a submit button. I didn't develop this site but I am continuing where the developer left off. I know PHP is server side and radio buttons are client side. There has to be a way for the user to tell me which option they want without having a form tag with a submit button. I would even go with a dropdown box. Anything. – Jeff Mcbride Jul 12 '17 at 01:59
  • Machavity. Yes, I want the value. But there is no submit button, I update the shipping price and total price on the fly using PHP so no submit button is needed. – Jeff Mcbride Jul 12 '17 at 02:01
  • I've marked a couple of duplicates here. 1. You will have to use AJAX to do what you're talking about 2. You really need to read up on the differences between client side and server side – Machavity Jul 12 '17 at 02:02
  • I know the difference between client side and server side programming. This is not a duplicate of the one you posted. I have no form tag and don't intend to use a form tag and submit button as there is no need for that. AJAX isn't going to give me what I need either since I am not submitting anything. What if I put a form tag around the entire table, can I then use $_POST to get the radio button . Mind you there would be no submit button, just the form tag as I am not submitting anything. – Jeff Mcbride Jul 12 '17 at 02:28
  • @JeffMcbride You don't seem to get it. You HAVE to SUBMIT. Once PHP has drawn the page it's done. Finito. Over and out. You have to make a new request to send any data back at that point. Could be a form submit or could be AJAX (where you don't need a form at all). Take your pick. So, no, you don't understand the difference between client and server side programming. What you're describing, using those methods, is impossible. – Machavity Jul 12 '17 at 03:24
  • Maybe this'll help: I have all shipping calculations done in a php on the server side. I need the end user to tell me which shipping charge to use so I can add that to the total charge. How do I give that user the choice? – Jeff Mcbride Jul 12 '17 at 05:46
  • I have the solution for this but cannot add a correct answer because it's marked as an exact duplicate (which is isn't) of another question. How do I change this to not a duplicate and answer the question? – Jeff Mcbride Jul 12 '17 at 10:40

0 Answers0