Jump to content

Dynamically checking whether $_POST checkboxes are empty or not won't work


Recommended Posts

Hello all!

 

I have a page that dynamically generates checkboxes in a for() loop if $i<$product_qty, while it is generating the checkboxes the name for them is set like this $product_id_$i. In part of the next page that processes the checkboxes, I have a part that regenerates the names for those checkboxes using a for() loop again and the product_qty and then checks to see whether the checkbox is empty or not.

 

Everything looks right in the for() loop, yet the if() that checks whether it isset or not just ignores the ones that are set. If I make a static if with the actual name of the checkbox the if() comes back as true. I've hit a brickwall, can anyone see any errors in my code? Code is below.

 

// Get Old Order
$get_order = @mysql_query("SELECT * FROM orders WHERE order_id = {$_POST['order_id']}");
$order = @mysql_fetch_assoc($get_order);

// Get Old Order Items
$products = $order['products'];
//breaking products text down for display
$prod = array();

$_products = explode('|', $products);
foreach ($_products AS $p) 
$prod[] = explode(',', $p);

if(empty($prod)) {
  header("Location: tracking.php");
  die();
}

/*
// Create New Order
@mysql_query("INSERT INTO orders SET customer_id = {$order['customer_id']}, order_status = {$order['order_status']}, order_date = '{$order['order_date']}', order_date_paid = '{$order['order_date_paid']}', order_shipping = '{$order['order_shipping']}', order_shipping_fee = '{$order['order_shipping_fee']}', order_insurance = '{$order['order_insurance']}', order_insurance_fee = '{$order['order_insurance_fee']}', order_insurance_total = '{$order['order_insurance_total']}', order_grand_total = '{$order['order_grand_total']}', order_date = '{$order['order_date']}', order_filled = '{$order['order_filled']}', order_ship_date = '{$order['ship_date']}'");
$get_new_order = @mysql_query("SELECT MAX(order_id) AS order_id FROM orders");
$new_order_id = @mysql_result($get_new_order, 'order_id', 0);
*/

// Add Items to New Order & Remove Items from Old Order
$new_items = array();
foreach($prod as $p2) {
  for($i = 0; $i < $p2[0]; $i++) { 
    if(!empty($_POST[$p2[3].'_'.$i])) {
      $new_items[$p2[3]]++;
    }
  }
}

if(isset($_POST['50_4'])) {
echo "hi";
}

 

TIA!

 

Jonathan

I figured maybe if I post both files it would help more.

 

split_order.php

<?php
session_start();
if(!isset($_SESSION['admin']) || $_SESSION['admin'] != 1) {
	//die("HELLOWORLD");
	include('scripts/common_top.php');
	header('Location: index.php');
	die();
}
if(empty($_GET['order_id']) || !is_numeric($_GET['order_id'])) {
  header("Location: admin.php");
  die();
}

$order_id = $_GET['order_id'];
include('includes/header.inc.php');

$get_cust  = @mysql_query("SELECT u.first_name, u.last_name FROM orders AS o LEFT JOIN users AS u ON o.customer_id = u.user_id WHERE o.order_id = $order_id");
$get_items = @mysql_query("SELECT oi.qty, oi.product_id, p.name FROM order_items AS oi LEFT JOIN products AS p USING (product_id) WHERE oi.order_id = $order_id");

$get_data = mysql_query("SELECT u.first_name, u.last_name, o.products FROM orders AS o LEFT JOIN users AS u ON o.customer_id = u.user_id WHERE o.order_id = $order_id");

$data  = @mysql_fetch_assoc($get_data);

$products = $data['products'];
//breaking products text down for display
$prod = array();

$_products = explode('|', $products);
foreach ($_products AS $p) 
  $prod[] = explode(',', $p);

$items = array();
while(($row = @mysql_fetch_assoc($get_items)) !== false) {
  $items[] = $row;
}
/*
$items = array(
			  "qty"=>array(
							   "1","2","3"
							   ),
			  "product_id"=>array(
							   "1","2","3"
							   ),
			  "name"=>array(
							   "Cleveland","Loretta","Junior"
							 )
			  ); 
*/
?>

    <div id="banner_container"><img src="images/banner.jpg" /></div>

    <div id="wrap">
        <div id="left_col">
          <div class="title">Split Order</div>
          
          <div class="page_text">
            <form name="splitOrder" id="splitOrder" action="processSplit.php" method="post">
            <div style="width: 99%; height: 300px; overflow: auto; border: solid 1px #000; padding: 1%;">
              <p>
                Split order for <?php echo ucwords($data['first_name'].' '.$data['last_name']); ?>.<br />                
                Which items go in the new order?<br /><br />
                
                <?php
                if(!empty($prod)) {
                  foreach($prod as $p2) {
                    for($i = 0; $i < $p2[0]; $i++) {
                      echo '<label><input class="splitOrderCheckbox" type="checkbox" value="1" name="'.$p2[3].'_'.$i.'" /> '.$p2[1].'</label><br />';
                    }
                  }
                }
                ?>
              </p>
            </div>
            
            <p>
              <input type="submit" value="Continue" />
              <input type="button" id="cancel" value="Cancel" />
              <input type="hidden" name="order_id" value="<?php echo $order_id; ?>" />
            </p>
            </form>
          </div>
        </div>
        
        <div id="right_col">
          <?php include('includes/admin_right_menu.inc.php'); ?>          
        </div>
    </div>
    
    <script type="text/javascript"><!--
    document.getElementById("cancel").onclick = function() { window.location = 'tracking.php'; }
    --></script>


<?php

//include footer
include('includes/footer.inc.php');
?>

 

 

processSplit.php

<?php
require('./scripts/common_top.php');
include('./scripts/dbconfig.php');
include('./scripts/get_cus_info.php');

// Get Old Order
$get_order = @mysql_query("SELECT * FROM orders WHERE order_id = {$_POST['order_id']}");
$order = @mysql_fetch_assoc($get_order);

// Get Old Order Items
$products = $order['products'];
//breaking products text down for display
$prod = array();

$_products = explode('|', $products);
foreach ($_products AS $p) 
$prod[] = explode(',', $p);

/*OLD CODE
$get_items = @mysql_query("SELECT product_id, qty FROM order_items WHERE order_id = {$order['order_id']}");
$items = array();
while(($row = @mysql_fetch_assoc($get_items)) !== false) {
  $items[] = $row;
}
*/
if(empty($prod)) {
  header("Location: tracking.php");
  die();
}

/*
// Create New Order
@mysql_query("INSERT INTO orders SET customer_id = {$order['customer_id']}, order_status = {$order['order_status']}, order_date = '{$order['order_date']}', order_date_paid = '{$order['order_date_paid']}', order_shipping = '{$order['order_shipping']}', order_shipping_fee = '{$order['order_shipping_fee']}', order_insurance = '{$order['order_insurance']}', order_insurance_fee = '{$order['order_insurance_fee']}', order_insurance_total = '{$order['order_insurance_total']}', order_grand_total = '{$order['order_grand_total']}', order_date = '{$order['order_date']}', order_filled = '{$order['order_filled']}', order_ship_date = '{$order['ship_date']}'");
$get_new_order = @mysql_query("SELECT MAX(order_id) AS order_id FROM orders");
$new_order_id = @mysql_result($get_new_order, 'order_id', 0);
*/

// Add Items to New Order & Remove Items from Old Order
$new_items = array();
foreach($prod as $p2) {
  for($i = 0; $i < $p2[0]; $i++) { 
    //if(!empty($_POST[$p2[3].'_'.$i])) {
      $new_items[$p2[3]]++;
    //}
  }
}

if(isset($_POST['50_4'])) {
echo "hi";
}

foreach($prod as $p5) {
for($j = 0; $j < $p5[0]; $j++) {
	if(isset($_POST[$p5[3]."_".$j])) {
		$new_items[$p2[3]]++;
	}
}
}

echo "<pre>";
print_r($new_items);
echo "</pre>";
die();


foreach($new_items as $product_id=>$qty) {
  @mysql_query("INSERT INTO order_items SET order_id = $new_order_id, product_id = $product_id, qty = $qty");
  @mysql_query("UPDATE order_items SET qty = qty - $qty WHERE product_id = $product_id AND order_id = {$order['order_id']}");
}

@mysql_query("DELETE FROM order_items WHERE qty = 0");

// Email Customer
$get_customer = @mysql_query("SELECT u.email, u.first_name, u.last_name FROM orders AS o LEFT JOIN users AS u ON o.customer_id = u.user_id WHERE o.order_id = $new_order_id");
$customer     = @mysql_fetch_assoc($get_customer);

$get_old_products = @mysql_query("SELECT oi.qty, p.name FROM order_items AS oi LEFT JOIN products AS p USING (product_id) WHERE order_id = {$order['order_id']}");
$get_new_products = @mysql_query("SELECT oi.qty, p.name FROM order_items AS oi LEFT JOIN products AS p USING (product_id) WHERE order_id = $new_order_id");

//include the sc_send_email function 
include('includes/functions.inc.php');

//set the email vars
$a_from = $admin_from_email;
$a_to = $customer['email'];
$a_cus_fname = ucfirst($customer['first_name']);
$a_cus_lname = ucfirst($customer['last_name']);
$a_subject = "Research Chemical | Your Research Chemical Order";
$a_message = "
		 <br><br>Dear ".$a_cus_fname." ".$a_cus_lname.",<br><br>Your order has been split and will be sent in 2 separate shipments.<br><br>Your first shipment will be:<br><br>
		 <b>Qty - Product Ordered</b><br>
		 ";

while(($row = @mysql_fetch_assoc($get_old_products)) !== false) {
  $a_message .= $row['qty'].' - '.$row['name']."<br>";
}
$a_message .=  "<br>Your second shipment will be:<br><br>";
$a_message .= "<b>Qty - Product Ordered</b><br>";
while(($row = @mysql_fetch_assoc($get_new_products)) !== false) {
  $a_message .= $row['qty'].' - '.$row['name']."<br>";
}
$a_message .= "<br>We apologize for any inconvenience and are working to get your orders out as quickly as possible. You will not be charged for the added shipping cost. We will cover that expense as a gesture of good will.<br><br>";
$a_message .= "Thank You,<br>Customer Service<br>www.researchchemical.org";
$return_url = NULL;

//echo $a_cus_lname;
//echo $a_message;

//send the email and redirect the user with a message
sc_send_email($a_from, $a_to, $a_cus_fname, $a_cus_lname, $a_subject, $a_message, $source_url, $return_url);

/*
$message = "Dear {$customer['first_name']},\n\nYour order has been split and will be sent in 2 separate shipments.\n\nYour first shipment will be:\n\n";
while(($row = @mysql_fetch_assoc($get_old_products)) !== false) {
  $message .= $row['qty'].' : '.$row['name']."\n";
}
$message .= "\nYour second shipment will be:\n\n";
$message .= "<b>Quantity Ordered - Product Ordered</b><br>";
while(($row = @mysql_fetch_assoc($get_new_products)) !== false) {
  $message .= $row['qty'].' : '.$row['name']."\n";
}
$message .= "\nWe apologize for any inconvenience and are working to get your orders out as quickly as possible. You will not be charged for the added shipping cost. We will cover that expense as a gesture of good will.\n\nThank You,\nCustomer Service\nwww.researchchemical.org";

$headers = 'From: '."$admin_email\r\n".'Reply-To: '."$admin_email\r\n" .'X-Mailer: PHP/' . phpversion();

@mail($customer['email'], 'Your Research Chemical Order', $message, $headers);
*/

// Redirect
header("Location: tracking.php");
die();

 

 

Please let me know if I can provide anything else

This is the specific code I am talking about...any ideas?

 

// Add Items to New Order & Remove Items from Old Order
$new_items = array();
foreach($prod as $p2) {
  for($i = 0; $i < $p2[0]; $i++) { 
    if(!empty($_POST[$p2[3].'_'.$i])) {
      $new_items[$p2[3]]++;
    }
  }
}

 

The if will not recoginize that those checkboxes have been selected.

 

Help!

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.