Jump to content

Adding an IF to post on a checkbox form


Mal1

Recommended Posts

Trying to fix existing some code (complete beginner here) on the company I work for website.

 

There's a check-box which when ticked should apply delivery charges and if not it shouldn't - currently it's working in that on the same page it calculates the total cost correctly when checked or not checked but when passing on information it always passes on the delivery charge even if un-ticked...

 

The simplest thing I can see would be to put an if statement in to say if (not ticked) then del_charges = 0.00. Any idea how I can do this in this instance?

 

 

<?php
      // add a checkbox for delivery charges
		echo '<span class="form"><label>Delivery Charges</label>';
      echo '<input type="checkbox" name="del_charges" id="del_charges" value="1" class="boxes" ';
      if ( isset($_POST['del_charges']) && !empty($_POST['del_charges']) )
        echo 'checked="checked"';
      echo '/>';                 
?>  

 

Thanks.

Link to comment
Share on other sites

<?php
      // add a checkbox for delivery charges
		echo '<span class="form"><label>Delivery Charges</label>';
      echo '<input type="checkbox" name="del_charges" id="del_charges" value="1" class="boxes" ';
      if ( isset($_POST['del_charges']) && !empty($_POST['del_charges']) )
        echo 'checked="checked"';
      else
        //do whatever you need to do here to remove the delivery charge
      echo '/>';                 
?>  

Link to comment
Share on other sites

<?php
      // add a checkbox for delivery charges
		echo '<span class="form"><label>Delivery Charges</label>';
      echo '<input type="checkbox" name="del_charges" id="del_charges" value="1" class="boxes" ';
      if ( isset($_POST['del_charges']) && !empty($_POST['del_charges']) )
        echo 'checked="checked"';
      else
        //do whatever you need to do here to remove the delivery charge
      echo '/>';                 
?>  

 

Thanks, so it's probably a case of fixing what calculates $del_charges earlier on in the code? Or can I do something like: $_POST['del_charges' = 0.00];?

Link to comment
Share on other sites

I have no clue how this code is set up..  :psychic:

I highly doubt that re-defining a $_POST value is the best way to go about this.

But without seeing the entire relevant code, I cannot give a better answer.

 

This is the code from this page. I'm more concerned with how it passes it on to other pages. I have view order and view invoices sections where delivery is being displayed - when that checkbox is not ticked the delivery should be 0 on these but right now it always shows the calculated delivery. This is a manual order - there's another section elsewhere where a customer can select 'collect from store' I've been able to use code to say if collect_from_store == '1' then delivery = 0. I'm looking to do the same thing if this checkbox is not checked...

 

 

<?php
$action = 'manual_order';
$_SESSION['action'] = $action;

//echo "<pre>";
//print_r($_SESSION); 
//echo "</pre>";
//echo "<pre>";
//print_r($_POST); 
//echo "</pre>";
?>
<script type="text/javascript" src="js/jquery/orders.js"></script>
<div id="menuopt">
<a href="admin.php">Back to admin</a>
</div>
<div id="list">
<div id="inside2">
	<form action='admin.php' method="post">

          <input type="hidden" name="action" value="<?php echo $action;?>" />
          <?php
		if(isset($_GET['remove_manual_rug_id'])) {
			unset($_SESSION['manual_order_rugs'][$_GET['remove_manual_rug_id']]);
		}
		if(is_array($_POST['add_rug_to_manual_order'])) {

       $index = 0;
       foreach($_POST['add_rug_to_manual_order'] as $key => $val )
        $index = $key;
      //echo "SDA$key";
       
			$_SESSION['manual_order_rugs'][$_POST['manual_stock_id'][$index]]['rug_id'] = $_POST['manual_rug_id'][$index];
			$_SESSION['manual_order_rugs'][$_POST['manual_stock_id'][$index]]['delivery_cost'] = $_POST['delivery_cost'][$index];
			$_SESSION['manual_order_rugs'][$_POST['manual_stock_id'][$index]]['price']  = $_POST['rug_price'][$index];
        $_SESSION['manual_order_rugs'][$_POST['manual_stock_id'][$index]]['qty']    = $_POST['rug_qty'][$index];
			$_SESSION['manual_order_rugs'][$_POST['manual_stock_id'][$index]]['desc']   = $_POST['manual_rug_desc'][$index];
		}
		if(sizeof($_SESSION['manual_order_rugs'])>0) {
			echo "<table cellspacing='5'><tr><th>Reference</th><th>Description</th><th>Qty</th><th>Price</th><th>Delivery</th><th>Action</th></tr>";
			$total     = 0;
			$total_del = 0;
			foreach($_SESSION['manual_order_rugs'] as $key=>$value) {
				echo "<tr><td><strong>#".$value['rug_id']."</strong></td>";
          echo "<td>".$value['desc']."</td>";
          echo "<td>".$value['qty']."</td>";
          echo "<td align='right'>£".$value['price']."</td>";
          echo "<td align='right'>£".$value['delivery_cost']."</td>";
          echo "<td><a href='admin.php?action=$action&id=".$_GET['id']."&remove_manual_rug_id=".$key."' style='text-decoration:underline'>delete from order</a></td></tr>";
          
				$total     = $total +     (floatval($value['price']) * floatval($value['qty']));
				$total_del = $total_del + (floatval($value['delivery_cost']) * floatval($value['qty']));

			}
			echo "<tr><td colspan='2' align='right'><strong>Totals: </strong></td><td> </td>";
        echo "<td align='right'><strong>£".number_format($total, 2)."</strong></td>";
        echo "<td align='right'><strong>£".number_format($total_del, 2)."</strong></td>";
        echo "</tr>";
			echo "</table><br />";
		}
		if($_POST['do_add_rug'] != 'Enter order') {

	?>
		Lookup rug by reference: <input type='text' size='5' name='lookup_id'> <input type='submit' name='perform_lookup' value='Lookup' />
		<?php
if($_POST['perform_lookup'] == 'Lookup' and $_POST['lookup_id'] != "" and !isset($_POST['add_rug_to_manual_order'])) {

    
    $rug = new Rug($_POST['lookup_id']);
    
    if ( $rug->error )
      echo "<br /><p>No valid matches</p><br />";
    else
	{
		echo "<br /><p>Found rug(s): <br />";
      
      foreach ( $rug->data['stock']->data as $line ) {

        // calculate the delivery cost of this item
        $stock = new Stock($rug->data['id'], 0);
        $delivery_cost = $stock->calculateDelivery($line['id']);      

  			$desc = $line['width']."m x ".$line['length']."m, ".$rug->data['type_name'];
  			if(strlen($rug->data['subtype_name'])>0)
  				$desc .= ", ".$rug->data['subtype_name'];
  			if(strlen($rug->data['designname'])>0)
  				$desc .= ", ".$rug->data['designname'];
  			if(strlen($rug->data['designer_name'])>0)
  				$desc .= " (".$rug->data['designer_name'].")";
  			if(strlen($rug->data['pattern_name'])>0)
  				$desc .= ", ".$rug->data['pattern_name'];
  			if(strlen($rug->data['fabric_name'])>0)
  				$desc .= ", ".$rug->data['fabric_name'];
        //if ( $line['stock'] > 0 && $line['stock_override'] == 0  )  
        if ( $line['stock_override'] == 0  )  
          $stock = " (".$line['stock']." in stock)";
        elseif ( $rug->data['stock_override'] == 1 )
          $stock = " (stock override)";
  			echo $desc;
        echo "  <small>qty:</small><input type='text' name='rug_qty[".$line['id']."]' size='3' value='1' /> ".$stock;     
  			echo "  <small>price:</small><input type='text' name='rug_price[".$line['id']."]' size='7' value=".$line['actual_price']." /> ";
  			echo "  <small>delivery:</small><input type='text' name='delivery_cost[".$line['id']."]' size='7' value=".$delivery_cost." /> ";
  			echo "<input type='hidden' name='manual_rug_id[".$line['id']."]' value=".$rug->data['id']." />";
  			echo "<input type='hidden' name='manual_stock_id[".$line['id']."]' value=".$line['id']." />";
  			//echo "<input type='hidden' name='manual_delivery_cost[".$line['id']."]' value=".$delivery_cost." />";
  			echo "<input type='hidden' name='manual_rug_desc[".$line['id']."]' value=\"".$desc."\" />";
  			echo "<input type='submit' name='add_rug_to_manual_order[".$line['id']."]' value='Add to order' />";
  			echo "</p>";
      }
	}

}


		$fields = array("firstname"=>"Forename",
				"lastname"=>"Surname",
				"email"=>"Email",
				"street_adress"=>"Address Line 1",
				"suburb"=>"Address Line 2",
				"postcode"=>"Postcode",
				"city"=>"City",
				"country"=>"Country",
				"phone"=>"Phone");
		$shipping_fields = array("shipping_street_adress"=>"Shipping Address 1",
				"shipping_suburb"=>"Shipping Address 2",
				"shipping_postcode"=>"Shipping Postcode",
				"shipping_city"=>"Shipping City",
          "shipping_country"=>"Shipping Country",
				"shipping_phone"=>"Shipping Phone");
		echo "<br /><div class=\"left\">";
		foreach($fields as $fieldname => $displayname) {
			echo "<span class='form'><label>".$displayname."</label><input type='text' name='".$fieldname."'";
			echo " value=\"".$_POST[$fieldname]."\" /></span>";
		}
		?>
      <span class="form"><label>State:</label><select name="order_state">
        <option value='5' <?php if ($_POST['order_state'] == '5' ) echo 'selected'; ?>>Completed</option>
        <option value='4' <?php if ($_POST['order_state'] == '4' ) echo 'selected'; ?>>Returned</option>
        <option value='3' <?php if ($_POST['order_state'] == '3' ) echo 'selected'; ?>>Handling</option>
        <option value='2' <?php if ($_POST['order_state'] == '2' ) echo 'selected'; ?>>Received</option>
   		  <option value='1' <?php if ($_POST['order_state'] == '1' ) echo 'selected'; ?>>Abandoned</option>
      </select></span>            


      <span class="form"> </span>
      <span class="form"> </span>
      <span class="form"><h2>Totals</h2></span>

<?php
      // add a checkbox for delivery charges
		echo '<span class="form"><label>Delivery Charges</label>';
      echo '<input type="checkbox" name="del_charges" id="del_charges" value="1" class="boxes" ';
      if ( isset($_POST['del_charges']) && !empty($_POST['del_charges']) )
        echo 'checked="checked"';
      echo '/>';                 
?>            
		<span class='form'>
        <label>Delivery Total:</label>
        <input type='text' name='total_del' id='total_del' readonly="readonly" class="readonly off" value="<?php echo $total_del; ?>" />
      </span>
		<span class='form'>
        <label>Sub Total:</label>
        <input type='text' name='total' id='total' readonly="readonly" class="readonly" value="<?php echo $total; ?>" />
      </span>
      <span class="form"> </span>

		<span class='form'>
        <label>Total:</label>
        <input type='text' name='total_order' id="total_order" readonly="readonly" class="readonly" value="<?php echo $total; ?>" />
      </span>

		<input type="submit" class="save" name="do_add_rug" value="Enter order" />
		</div>
		<div class='right'>

      <span class='form'>
        <label>Payment Method</label>
        <input type='text' name='payment_method' value="<?php echo $_POST['payment_method']; ?>" />
      </span>

      <span class='form'>
        <label>Customer Source</label>
        <input type='text' name='customer_source' value="<?php echo $_POST['customer_source']; ?>" />
      </span>

      <span class="form"> </span>

		<?php
		foreach($shipping_fields as $fieldname => $displayname) {
			echo "<span class='form'><label>".$displayname."</label><input type='text' name='".$fieldname."'";
			echo " value=\"".$_POST[$fieldname]."\" /></span>";
		}
            // add a textarea for notes
		echo '<span class="form"><label>Notes</label><textarea name="notes" rows="8" cols="32" >'.$_POST['notes'].'</textarea>';            
		?>
		</div>

	</form>

<?php
	} else {
		echo "COMPLETED ORDER:".$completed_order_id."<br />";
		print_r($_POST);
		print_r($_SESSION);
	}
/*
<!--
                  <h3>Order: <?php echo $order['id']; ?></h3>

                  <div id="products">

                    <div class="headers">
                      <p class="products">Product(s)</p>
                      <p class="price">Price</p>
                    </div>
                    <!-- //product -->
                    <?php $total = 0; ?>
                    <?php foreach ($rugs as $rug): ?>
                    <div class="product">
                    <div class="product_photo">
                      <img src="img/<?php if ($rug['main_photo_id']) echo $rug['id']."_".$rug['main_photo_id']; else echo "nophoto"; ?>_small.jpg" width="120" height="160" alt="" />
                        <span>
                          <p class="txt">Reference: R<?php echo $rug['id']; ?></p>
                          <p class="txt">Length: <?php echo round($rug['length']/0.3, 2); ?> ft (<?php echo $rug['length'];?> m)</p>
                          <p class="txt">Width: <?php echo round($rug['width']/0.3, 2); ?> ft (<?php echo $rug['width'];?> m)</p>
                        </span>
                        <a href="?action=view_rug&id=<?php echo $rug['id']; ?>"><?php echo $rug['short_description']; ?></a>
                    </div>

                    <div class="how_much">
                      <p class="pound">£<?php echo $rug['sold_price']; $total += $rug['sold_price']*$rug['order_qty']; ?></p>
                    </div>

                  </div>
                  <?php endforeach; ?>
                  <!-- // finish product -->
                </div>

                <p class="total">Total: £<?php echo $total; ?></p>

			<div id="contact_details">
			<h5>Sold date: <?php echo $order['created']; ?></h5>
			<h5>Billing details</h5>
			<p>First Name: <?php echo $order['firstname']; ?></p>
			<p>Last Name: <?php echo $order['lastname']; ?></p>
			<p>Email: <?php echo $order['email']; ?></p>
			<p>Street Address: <?php echo $order['street_adress']; ?></p>
			<?php if ($order['suburb'] != ""): ?><p>Address line 2: <?php echo $order['suburb']; ?></p><?php endif; ?>
			<p>Post Code: <?php echo $order['postcode']; ?></p>
			<p>City: <?php echo $order['city']; ?></p>
			<p>County: <?php echo $order['county']; ?></p>
			<p>Country: <?php echo $order['country']; ?></p>
			<p>Telephone number: <?php echo $order['phone']; ?></p>

			<?php if (!isset($_POST['same_shipping_adress']) || !$_POST['same_shipping_adress']): ?>
			<h5>Shipping details</h5>
			<p>Street Address: <?php echo $order['shipping_street_adress']; ?></p>
			<?php if ($order['shipping_suburb'] != ""): ?><p>Address line 2: <?php echo $order['shipping_suburb']; ?></p><?php endif; ?>
			<p>Post Code: <?php echo $order['shipping_postcode']; ?></p>
			<p>City: <?php echo $order['shipping_city']; ?></p>
			<p>County: <?php echo $order['shipping_county']; ?></p>
			<p>Country: <?php echo $order['shipping_country']; ?></p>
			<p>Telephone number: <?php echo $order['shipping_phone']; ?></p>
			<?php endif; ?>
			</div>
-->
*/
?>
            </div>
</div>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.