Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    send variable from php to html

    Hi, i'm quite new with PHP, i'm trying to modify some code, what I want to do is to get the value that is calculated via php and use it on an html input text field, hr_extra, wich I wrote here in yellow color, so the user could be able to modify it, I want it to get the $totalAmount variable, but I haven't been able to parse it.

    here's the code from ordercheckout.php

    Code:
    <?php 
    	session_start();
    
    	require_once("shared.php");
    	
    	//XAJAX HANDLERS
    	require_once("xajax_core/xajax.inc.php");
    	$xajax = new xajax();
    
    	$xajax->register(XAJAX_FUNCTION, "xsetImage");
    	
    	function xsetImage($value, $id)
    	{
    		$objResponse = new xajaxResponse();
    
    		if(!isset($_SESSION['images']))
    		{
    			$_SESSION['images'] = array();
    		}
    		
    		if($value == 1)
    		{
    			$_SESSION['images'][] = $id;
    
    		}
    		else
    		{
    			for($i=0;$i<count($_SESSION['images']);$i++)
    			{
    				if($_SESSION['images'][$i] == $id)
    				{
    					$_SESSION['images'][$i] = 0;
    					break;
    				}
    			}
    		}
    			
    		
    		return $objResponse;
    	}
    	
    	$xajax->processRequest();
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <script>
    	function viewPdf()
    	{
    		document.getElementById('order-checkout').action = 'viewpdf.php';
    		document.getElementById('order-checkout').target = '_new';
    		document.getElementById('order-checkout').submit();
    		
    	}
    	
    	function sendPdf()
    	{
    		document.getElementById('order-checkout').action = 'ordersend.php';
    		document.getElementById('order-checkout').target = '_self';
    		document.getElementById('order-checkout').submit();
    	}
    	
    </script>
    <?php $xajax->printJavascript(); ?>
    <link rel="stylesheet" href="stylesheets/style.css" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Nox - Checkout de Orden</title>
    </head>
    <body>
    <div id="debugger"></div>
    	<div id="wrapper">
        <form id="order-checkout" action="ordersend.php" method="post">
    		<fieldset>
        		<legend>Resumen de Cotizacion</legend>
                <div class="inputEntry">
                	<label class="labelL">Nombre del Cliente</label>
    				<label><?php echo $_SESSION['c-name']; ?></label>
                </div>
                <div class="inputEntry">
                	<label class="labelL">Direccion</label>
    				<label><?php echo $_SESSION['c-address']; ?></label>
                </div>
                <div class="inputEntry">
        	        <label class="labelS">Telefono</label>
                    <label><?php echo $_SESSION['c-phone']; ?></label>
                </div>
                <div class="inputEntry">
        	        <label class="labelS">Email</label>
    				<label><?php echo $_SESSION['c-email']; ?></label>
                </div>
                <div class="inputEntry">
        	        <label class="labelS">Fecha de Nacimiento</label>
    				<label><?php echo $_SESSION['c-dob']; ?></label>
                </div>
                <div class="inputEntry">
                	<label class="labelS">Horas de Servicio</label>
                    <input type="text" id="hours" name="hours" style="width:60px;"class="inputTextL required" value="5"/>
                </div>
                <div class="inputEntry">
                	<label class="labelS">Cotizacion desglosada</label>
    				<input type="checkbox" name="des" id="des"/>
                </div>            
                <div class="inputEntry">
                	<label class="labelS">Destinatarios</label>
                    <input type="text" name="dest" style="width:600px;"
                     class="inputTextL required" value="<?php echo $_SESSION['c-email']; ?>"/>
                    <div class="inputEntry">
                        <label class="labelS">Hr. Extra</label>
                        <input type="text" id="hr_extra" name="hr_extra" style="width:100px;" value=""/>
    				</div>
                </div>
    			
                <div class="order-actions">            
                    <ul>
                        <li><input type="button" value="Modificar Cliente" class="submit" 
                        onclick="document.location='clientregistry.php'"/></li>
                        <li><input type="button" value="Modificar Orden" class="submit"
                        onclick="document.location='productcatalog.php'"/></li>
                        <li><input type="button" value="Visualizar  PDF" class="submit" 
                        onclick="viewPdf()"/></li>
                        <li><input type="button" value="Finalizar y Enviar" class="submit"
                        onclick="sendPdf()"/></li>
                    </ul>
                </div>
    
    	    
            
            <div id="product-summary">
                	<div id="p-details">
                    	<div>Concepto</div>
                        <div>Cantidad</div>
                        <div>Precio Unitario</div>
                        <div>Subtotal</div>
                    </div>
                    <div id="order-content">
                    <?php
    					
    	$totalAmount = 0;
    				//Print current Items
    				for($i=0;$i<count($_SESSION['productIDS']);$i++)
    				{
    					if($_SESSION['productQuantities'][$i] < 1)
    						continue;
    					$product = getProductById($_SESSION['productIDS'][$i]);
    					$subTotal = $product->price * $_SESSION['productQuantities'][$i];
    					$totalAmount += $subTotal;
    					
    					$html .= '
    					<div id="p-content">
                            <div>'.$product->name.'</div>
                            <div>'.$_SESSION['productQuantities'][$i].'</div>
                            <div>$'.number_format($product->price, 2).'</div>
                            <div>$'.number_format($subTotal, 2).'</div>
                        </div>
    					
    					';
    				}
    				echo $html;
    				
    				//MANDAR TOTAL 
    				
    				if($_SESSION['dsc'] > 0)
    				{
    					$html = '
    					<div id="p-content">
                            <div>&nbsp;</div>
                            <div>&nbsp;</div>
                            <div style="font-weight:bold;">Descuento</div>
                            <div style="font-weight:bold;">'.number_format($_SESSION['dsc'], 2).'%</div>
                        </div>
    					
    					';
    					
    					$totalAmount = $totalAmount - ($totalAmount * ($_SESSION['dsc']/100));
    					echo $html;
    				}
    				
    				
    				
    				$html= '
    					<div id="p-content">
                            <div>&nbsp;</div>
                            <div>&nbsp;</div>
                            <div style="font-weight:bold;">Total:</div>
                            <div style="font-weight:bold;">$'.number_format($totalAmount, 2).'</div>
                        </div>
    					
    					';
    				//echo $totalAmount;
    				echo $html;	
    				?>
                    </div>            	
                </div>
                
                <div>
    	            <label>Imagenes para adjuntar</label>            
                    <ul id="image-list">
                    	<?php
    					
    						for($i=0;$i<count($_SESSION['productIDS']);$i++)
    						{
    							$html = "";
    							if($_SESSION['productQuantities'][$i] > 0)	
    							{
    								$image = getImageByProductId($_SESSION['productIDS'][$i]);
    								if(!empty($image))
    								{
    									$html .= '<li><input type="checkbox" name="images[]" value="'.$image->id.'"  /><img src="images/'.$image->url.'" width="150px"/></li>';
    								}
    								
    							}
    							
    							echo $html;
    						}
    		
    					?>
    
                    </ul>                
                </div>
            </fieldset>
            
        </form>
        
        
        
        </div>
    </body>
    </html>

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,225
    Thanks
    23
    Thanked 606 Times in 605 Posts
    Find the following code segment and INSERT the javascript that is colored red: It starts at line 165:
    Code:
    $totalAmount = $totalAmount - ($totalAmount * ($_SESSION['dsc']/100));
    echo $html;
    }
    $html= '
    <div id="p-content">
        <div>&nbsp;</div>
        <div>&nbsp;</div>
        <div style="font-weight:bold;">Total:</div>
        <div style="font-weight:bold;">$'.number_format($totalAmount, 2).'</div>
    </div>
    ';
    //echo $totalAmount;
    echo $html;
    ?>
    <script type="text/javascript">
      var amount_total = <?php echo $totalAmount; ?>;
      document.getElementById('hr_extra').value = '$'+amount_total;
    </script>
    </div>
    </div>

  • #3
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,332
    Thanks
    60
    Thanked 526 Times in 513 Posts
    Blog Entries
    4
    Well that only deals with a value that php printed into the page as it was being generated.

    If you want a value to be transmitted from the server to the page the op will need to use ajax.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #4
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,225
    Thanks
    23
    Thanked 606 Times in 605 Posts
    @tangoforce
    From everything I see the $totalAmount is generated by values in the session variables and the only open field in the html form [Hr. Extra] does not affect it. So the preprocessed value should be the final value jofrabt is looking for.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •