0

Hi! This is my first post in this forum.

Thanks in advance to anyone who can help with this! I hope this makes sense. I'm also relatively new to PHP, so I hope I have used terminology correctly in describing the problem.

I am not sure how to accomplish what needs to be done. This is a complicated problem for me and I have tried to include the necessary code examples below without putting hundreds of lines in here to wade through.

The scenario is that I'm creating a page within an e-commerce site that will allow the user to upload a file that will then be processed and added to the cart.

Once that upload has been done, detailed results will be displayed showing the status of each item that was processed from the uploaded file. In addition, the cart totals at the TOP of the page (which have already been displayed) need to be refreshed.

The framework in place for this site uses PHP classes and multiple layers of overloading in order to "build" the html for a page.

The "stack" in place to create the html for the upload page can be simplified for this discussion as "main.php" and "upload.php", although the actual code stack is somewhat more complicated.

As you can see in the class "account_upload" below that, once the form is submitted, two methods are called; ent_upload::upload_file and ent_upload::import_file.

Those are working just fine and the subsequent display is showing the status of the processed lines just fine. In fact everything is working fine, except...

What I can't figure out how to do is to get the cart totals at the top of the screen to re-display.

From my research thus far, it seems that what is needed is to use the Javascript/AJAX functionality to re-execute the method snap_cart->display(), snag those results and then modify the innerHtml property of the appropriate <div> tag. This will likely require creating an additional <div> tag within the class "main" that encapsulates or "wraps around" the <div> tag "snap_display" as created within the class "snap_cart".

But the basic problem is that all the code that executes after the "submit" button is pressed on the upload screen is PHP code and executes on the server, so neither Javascript nor AJAX are available. I can't find any documentation that indicates that PHP can modify the innerHtml properties of a <div> tag and so I am totally stumped as to how to refresh only this portion of the page.

Ideally, I would envision this occurring just prior to the <div id="upload_file_info"> tag in the class "account_upload".

I've been trying to figure this out for weeks.


*** This code segment is in the file main.php ***

class site_main extends ui_site
{
	.
	. (code removed here...)
	.
	public function load()
	{
		$this->m_components->add('snap_cart', new snap_cart());
	}
	.
	. (code removed here...)
	.
	public function display_header()
	{
		?><table width="100%" border="0" cellspacing="0" cellpadding="0">
		<tr>
		<td class="login" align="right" id="login">
		<?php
        
		if(sys::$show_login)
		{
		
			$this->m_components->snap_cart->display();

		}

		?></td>
		</tr></table>
	.
	. (code removed here...the remainder of the html for the page is created)
	.
}

*** This code segment is in the file cart.php ***

class snap_cart extends ui_component
{
	.
	. (code removed here...)
	.
	public function display()
	{
		if(sys::$user->is_logged_in)
		{
			sys::$user->cart->load_item_totals();
		}

		?>
		<div id="snap_display">
		<table border="0" cellspacing="0" cellpadding="0" class="snap_cart">
			<tr>
				<th class="icon" width="1"><img src="/shared/graphics/cart.png" /></th>
				<th class="link"><a href="http://<?php echo $_SERVER['HTTP_HOST']; ?>/cart/home.php">Shopping Cart</a></th>
				<td class="links" align="right"><a href="http://<?php echo $_SERVER['HTTP_HOST']; ?>/cart/home.php">View Cart</a> | <a href="http://<?php echo $_SERVER['HTTP_HOST']; ?>/cart/checkout.php">Checkout</a></td>
			</tr>
			<tr>
				<td colspan="2"><label>Total Units (<?php echo  number_format(sys::$user->cart->total_unit_qty, 0); ?>):</label></td>
				<td class="total">$<?php echo number_format(sys::$user->cart->total_unit, 2, '.', ','); ?></td>
			</tr>
			<tr>
				<td colspan="2"><label>Total Accessories (<?php echo  number_format(sys::$user->cart->total_accessory_qty, 0); ?>):</label></td>
				<td class="total">$<?php echo number_format(sys::$user->cart->total_accessory, 2, '.', ','); ?></td>
			</tr>
			<tr>
				<td colspan="2"><label>Total Other (<?php echo number_format(sys::$user->cart->total_other_qty,0); ?>):</label></td>
				<td class="total">$<?php echo number_format(sys::$user->cart->total_other, 2, '.', ','); ?></td>
			</tr>
			<tr>
				<td colspan="2"><label>Total Item Amount:</label></td>
				<td class="total" style="text-decoration:overline;">$<?php echo number_format(sys::$user->cart->total, 2, '.', ','); ?></td>
			</tr>
		</table>
		</div>
		<?php
	}
	.
	. (code removed here...)
	.
}

*** This code segment is in the file upload.php ***

class account_upload extends ui_page
{
	.
	. (code removed here...)
	.
	public function display()
	{
			
		// rwk - grab parameters from entered fields
		if($_POST['action'] == 'Upload')
		{
			
			// echo $_FILES['uploaded']['name'];
			if ($_FILES['uploaded']['name'] != "")
			{
				$upload_array = ent_upload::upload_file();
				
				if ($upload_array['ok'] == 1)
				{
					$upload_results = ent_upload::import_file($upload_array);
				}
			}
			
		}
		
		?>
		
		<table width="100%" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <th class="title">Upload File:</th>
          </tr>
        </table><br>
        
		<table class="orderlist" width="80%" border="0" cellspacing="0" cellpadding="0">
		  <tr>
			<th>You can use this page to upload a file to create an order.  You can use the "Browse" button to find the file on your local machine and then use the "Upload" button to import the file into the cart.</th>
		  </tr>
        </table><br/>
        
        <table width="80%" border="0" cellspacing="0" cellpadding="0">
        	<tr>
          	  <td>
				<form enctype="multipart/form-data" method="POST">
				<table border="0" cellspacing="0" cellpadding="2">
				<tr>
					<td width=45% align="right" > Please choose a file:</td>
					<td><input name="uploaded" type="file" /></td>
					<td><input type="submit" name="action" value="Upload" /></td>
				</tr>
				</table>
				</form>
          	  </td>
        </table><br>
        <table width="100%" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <th class="title">Upload Results</th>
          </tr>
        </table><br>
        
        <?php
        if (is_array($upload_results) && (count($upload_results) !== 0))
        {
        ?>
	        <div id="upload_file_info">
	        	<table width="100%" border="0" cellspacing="0" cellpadding="0">
	        		<tr>
	        			<th>Status: </th>
	        			<td><?php echo $upload_results['status']; ?></td>
	        		</tr>
	        	</table><br>
	        </div>
	        
		<?php
		
			$items_array = $upload_results['results'];
        	if (is_array($items_array) && (count($items_array) !== 0))
        	{
        		$first = true;
        		
				foreach($items_array as $item_no => $values)
				{
        		
					$item_array = $values;
					if ($first)
					{
		?>
				        <div id="upload_items_info">
							<table width="100%" border="0" cellspacing="0" cellpadding="0">
								<tr>
									<th>Part Number</th>
									<th>Qty Ord</th>
									<th>UOM</th>
									<th>Result</th>
								</tr>
		<?php
						$first = false;
					}
		?>
								<tr>
									<td><?php echo $item_no; ?></td>
									<td><?php echo $item_array['qty']; ?></td>
									<td><?php echo $item_array['uom']; ?></td>
									<td><?php echo $item_array['result']; ?></td>
								</tr>
		<?php
				}
		?>
							</table>
				        </div>
		<?php
        	}
		}
		?>
        
	<?php        
	.
	. (code removed here...)
	.
}
2
Contributors
3
Replies
4
Views
6 Years
Discussion Span
Last Post by Russ_Kauk
0

so upon submission of this upload file, I assume that this is a standard form submission and the page refreshes. the server processes the file and then populates the values in this <div> tag.

If that's the case then you would want to somehow tap into the already written functionality of the cart to update the values on the server side so that the cart will display the correct values naturally as the developer intended.

Does this make sense, you don't want to reinvent the wheel if you don't have to and you don't want to prevent 10%(maybe a little exagerated) of users from using your cart because they don't have javascript enabled.

0

Good morning RObbOb. Thanks for your reply.

so upon submission of this upload file, I assume that this is a standard form submission and the page refreshes. the server processes the file and then populates the values in this <div> tag.

If that's the case then you would want to somehow tap into the already written functionality of the cart to update the values on the server side so that the cart will display the correct values naturally as the developer intended.

Does this make sense, you don't want to reinvent the wheel if you don't have to and you don't want to prevent 10%(maybe a little exagerated) of users from using your cart because they don't have javascript enabled.

Yes I believe that is exactly what happens. I'm sorry, I was going to try and intersperse my comments between parts of your original post but don't seem to be able to figure out the tagging properly in order to do that.

You are correct and this has been my thinking all along. However, if I correctly understand what is happening, when the page refreshes is when the code in the method class_upload->display() occurs to do the actual processing and by then the cart display has already occurred as well via the method site_main->display_header().

You have given me another new thought, however, of perhaps moving the processing code in the class_upload to a position in the script that occurs BEFORE the method site_main->display_header().

I'll let you know if that works. Otherwise I'm back to trying to figure out how to modify html code that has already been sent to and displayed by the browser.

0

Hey RObbOb! Thanks for triggering some additional thinking on this. The cart display problem is solved! The status part of the display is now broke, but isn't that just about how it goes in software development!!??!

This thread is now closed!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.