Hi

I am building a simple shopping cart and working on the shipping fees. In a previous page, I obtain the customer's address and pass on the value $Postcode through the form. Next, I use the mb_substr function to check the first 4 characters of the postcode. If the value of $postcode1 is SE22, then delivery is free, otherwise I will charge £3. However, the code below doesn't seem to work. Where am I going wrong?

Mayan

// check if eligible for free delivery
$postcode1 = mb_substr($Postcode,0,4);
if($postcode != "SE22"  ){
$delivery = 3;
} else {
$delivery = 0;
}

    What 'doesn't seem to work'? It's hard to tell from your code.

    Having said that ...

    1. Turn on error_reporting ... put

      error_reporting(E_ALL);

      at the top the page you are trying to debug. Remove

    2. Are you getting the form variables correctly ... you should have something like

      $Postcode = $_POST['postcode'];

      I say 'something like' because I don't know what the name of the form element is.

    3. Should this line

      if($postcode != "SE22"  ){

      be

      if($postcode1 != "SE22"  ){

      ?

    Paul

      Hi Paul

      Checked the code and it seems correct to me. However I am still obtaining the wrong delivery amount.

      // check if eligible for free delivery
      $Postcode = $_POST['Postcode']; // the value of this variable is SE22 5JP
      $postcode1 = mb_substr($Postcode,0,4);
      if($postcode1 != "SE22"  ){
      $delivery = 3;
      } else {
      $delivery = 0;
      echo $Postcode; // prints SE22 5JP
      echo $postcode1 // prints SE22
      echo $delivery // prints 3 when it should be 0 
      } 

        Sorry, previous post had some syntax errors. This now seems correct to me but not getting the result of 0.

        // check if eligible for free delivery
        $Postcode = $_POST['Postcode']; // the value of this variable is SE22 5JP
        $postcode1 = mb_substr($Postcode,0,4);
        if($postcode1 != "SE22"  ){
        $delivery = 3;
        } else {
        $delivery = 0;
        } 
        echo $Postcode; // prints SE22 5JP
        echo $postcode1; // prints SE22
        echo $delivery; // prints 3 when it should be 0
        

          erased... OP posted updated while i was typing

            I haven't used the mb_ string functions before - are you sure it's necessary? If you're not sure then try the standard substr() function instead.

            Also, you might try 'trimming' the $POST var just in case extra whitespace is coming along for the ride ...

            $Postcode = trim($_POST['Postcode']);

            P.

              works for me:

              $Postcode = 'SE22 5JP'; // the value of this variable is SE22 5JP
              $postcode1 = mb_substr($Postcode,0,4);
              if($postcode1 != "SE22"  ){
              $delivery = 3;
              } else {
              $delivery = 0;
              echo "Postcode: $Postcode<br/>"; // prints SE22 5JP
              echo "Substr: $postcode1<br/>"; // prints SE22
              echo "Delivery: $delivery"; // prints 0
              }
              

                I was in fact typing in a postcode that had 3 characters then a space, so the trim function fixed the problem. Thanks!

                  Write a Reply...