Search Webmaster Help and Solution

[RESOLVED] Problem processing checkboxes

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. Ive hit a brickwall, can anyone see any errors in my code? Code is below.

split_order.php
PHP Code: <?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 Code: <?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]}, Your order has been split and will be sent in 2 separate shipments. Your first shipment will be: ;
while(($row = @mysql_fetch_assoc($get_old_products)) !== false) {
  $message .= $row[qty]. : .$row[name]. ;
}
$message .= Your second shipment will be: ;
$message .= <b>Quantity Ordered - Product Ordered</b><br>;
while(($row = @mysql_fetch_assoc($get_new_products)) !== false) {
  $message .= $row[qty]. : .$row[name]. ;
}
$message .= 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. Thank You, Customer Service www.researchchemical.org;

$headers = From: .$admin_email .Reply-To: .$admin_email .X-Mailer: PHP/ . phpversion();

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

// Redirect
header(Location: tracking.php);
die(); 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. Ive hit a brickwall, can anyone see any errors in my code? Code is below.

split_order.php
PHP Code: <?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 Code: <?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]}, Your order has been split and will be sent in 2 separate shipments. Your first shipment will be: ;
while(($row = @mysql_fetch_assoc($get_old_products)) !== false) {
  $message .= $row[qty]. : .$row[name]. ;
}
$message .= Your second shipment will be: ;
$message .= <b>Quantity Ordered - Product Ordered</b><br>;
while(($row = @mysql_fetch_assoc($get_new_products)) !== false) {
  $message .= $row[qty]. : .$row[name]. ;
}
$message .= 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. Thank You, Customer Service www.researchchemical.org;

$headers = From: .$admin_email .Reply-To: .$admin_email .X-Mailer: PHP/ . phpversion();

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

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

View Complete Thread with Replies

Related Items

Query failed: connection to localhost:3354 failed (errno=111, msg=Connection refused).