PHP-Scripte PHP-Tutorials PHP-Jobs und vieles mehr

PHP-Scripte PHP-Tutorials PHP-Jobs und vieles mehr (https://www.php-resource.de/forum/)
-   HTML, JavaScript, AJAX, jQuery, CSS, Bootstrap, LESS (https://www.php-resource.de/forum/html-javascript-ajax-jquery-css-bootstrap-less/)
-   -   input[] Feld über Ajax laden (https://www.php-resource.de/forum/html-javascript-ajax-jquery-css-bootstrap-less/105651-input-feld-ueber-ajax-laden.html)

sanktusm 21-07-2016 13:21

input[] Feld über Ajax laden
 
Hallo,
ich habe hier eine Warenkorb für den ich eine Aktualisierung der Anzahl möglich machen will:

PHP-Code:

$("#shopping-cart-results").on('submit''.form-to-update', function(e) {
        
                          
     var 
productCode = $('input[name="productCode[]"]').serialize();
                
                
                
    var 
quantity = $('input[name="quantity[]"]').serialize();

     
                  
            $.
ajax({ 
                
url"cart_process.php",
                
type"post"
                
dataJSON.stringify({  
        
                           
productCode,
                        
                            
quantity 
                             
                             
})
                       
                      
                    
            }).
done();
            
      $(
"#shopping-cart-results").html('<img src="images/ajax-loader.gif">'); //show loading image
        
$("#shopping-cart-results" ).load"cart_process.php", {"load_cart":"1"});
      
            
e.preventDefault();
        }); 


das FormularArray wird auch über ajax geladen:

PHP-Code:

if(isset($_POST["load_cart"]) && $_POST["load_cart"]==1)
{

      if(isset(
$_SESSION["products"]) && count($_SESSION["products"])>0){ //if we have session variable
        
$cart_box '<form class="form-to-update"><ul class="cart-products-loaded">';
        
$total 0;
        foreach(
$_SESSION["products"] as $product){ //loop though items and prepare html content
            
            //set variables to use them in HTML content below
            
$product_name $product["product_name"]; 
            
$product_price $product["product_price"];
            
$product_code $product["product_code"];
            
$product_qty $product["qty"];
            
            
$cart_box .=  "<li><span>Ihr Produkt: $product_name </span><span>PS: $product_code <input type=\"hidden\" name=\"productCode[]\" value=\"".$product_code."\" /></span> <span>Anzahl : <input type=\"text\" name=\"quantity[]\" value=\"".$product_qty."\" style=\"width: 30px;\" /></span><span>Einzelpreis :$currency ".sprintf("%01.2f"$product_price)." </span> <span>Zwischensumme: $currency ".sprintf("%01.2f", ($product_price $product_qty)). "</span> <span><a href=\"#\" class=\"remove-item\" data-code=\"".trim($product_code)."\"><img src=\"./themes/shop/images/trash.png\" border=\"0\" /></a></span></li>";
            
$subtotal = ($product_price $product_qty);
            
$total = ($total $subtotal);
        }
    
        
$cart_box .= "</ul>";
        
$cart_box .= '<div class="clearfix"></div><div class="cart-products-total">Total : '.$currency.sprintf("%01.2f",$total).' <button type="submit" class="warenkorb-aktualisieren">aktualisieren</button></form> <a href="https://www.vah.de/shop/zur-kasse" title="zur Kasse">zur Kasse</a></div>';
        die(
$cart_box); //exit and output content
    
}else{
        die(
"<div class='empty'>Ihr Warenkorb ist leer</div>"); //we have empty cart
    
}


Nun wird leider immer nur FormularArray mit dem letzten Wert von productCode weitergegeben:

PHP-Code:

// cart_process.php

if(isset($_POST) AND is_array($_POST['productCode'])) {
                               
       foreach(
$_POST['productCode'] as $key => $value) {

                                          
        if(isset(
$_SESSION["products"])){ 
                       
                
$_SESSION["products"][$value]['qty'] = $_POST['quantity'][$key]; //unset old item
                        
        
}
        
   } 

Ich komme da nicht weiter. Vielleicht weiß jemand eine Lösung.

wahsaga 21-07-2016 16:06

Und die productCode-Variable im ersten Abschnitt enthält was?

sanktusm 21-07-2016 16:31

habs gelöst
 
PHP-Code:

$("#shopping-cart-results").on('submit''.form-to-update', function(e) {
        
    
           
 var 
form_data = $(this).serialize();    
                  
alert(form_data); 
            $.
ajax({ 
                
url"cart_process.php",
                
type"post",
        
                
dataType:"json",        
 
                
dataform_data
                             
                       
                        
// other data
                    
            
});
            
      $(
"#shopping-cart-results").html('<img src="images/ajax-loader.gif">'); //show loading image
        
$("#shopping-cart-results" ).load"cart_process.php", {"load_cart":"1"});
      
            
e.preventDefault();
        }); 



Alle Zeitangaben in WEZ +2. Es ist jetzt 20:55 Uhr.

Powered by vBulletin® Version 3.8.2 (Deutsch)
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0
[c] ebiz-consult GmbH & Co. KG