2017-08-23 1 views
0

Ich versuche, mehrere Barcodes gleichzeitig zu generieren und die generierten Barcodes in meiner Datenbank zu speichern. Noch habe ich multiple Barcode generiert, aber ich hatte ein Problem beim Speichern in der Datenbank.Wie den Wert in Array in Javascript erfassen und den Wert in Yii2-Controller zurückgeben?

Mein Code bis jetzt: -

function getbarcode(num,barcode) 
    { 

    for (var i = 1; i <= num; i++) { 
    var barcodenum = parseInt(barcode)+parseInt(i); 
    var patron= barcodenum.toString(); 
    var type = "code11"; 
    var settings = { 
     barWidth: 2, 
     barHeight: 50, 
     moduleSize: 5, 
     addQuietZone: true, 
     marginHRI: 5, 
     bgColor: "#FFFFFF", 
     color: "#000000", 
     fontSize: 10, 
     output: "css", 
     posX: 0, 
     posY: 0, 
     fontOptions: "bold", 
    }; 
    $('#barcoderesult').append('<div id="showBarcode'+ i +'" 
    style="float:left" />'); 
    $("#showBarcode"+i).barcode(patron, type, settings); 
    $("#showbarcode"+i).animate({height: "100%", width: "100%"}); 

    } 
} 

Jetzt in meinem variablen Patron, es enthält mehr generierte Barcode-Nummer. Jetzt muss ich die Barcode-Nummer in Patron zu einem Array und übergeben Sie das Array in yii2 Controller durch Ajax? Wie kann ich es tun ? Ich hatte keine Ahnung.

+0

In diesem ** getbarcode() ** wollten Sie nur AJAX verwenden? –

Antwort

0

Zuerst erzeugen nämlich eine Anordnung, array_barcode und verwenden JavaScript Array push() Method Werte in dieser Matrix zu füllen. Dann übergeben Sie diese Array-Werte nach der for-Schleife über AJAX an den Controller, wenn es nicht leer ist.

<script> 
function getbarcode(num, barcode){ 

    var array_barcode = []; 

    for (var i = 1; i <= num; i++) { 
    var barcodenum = parseInt(barcode) + parseInt(i); 
    var patron = barcodenum.toString(); 
    array_barcode.push(patron); //Push generated barcode into array 'array_barcode' 

    var type = "code11"; 
    var settings = { 
     barWidth: 2, 
     barHeight: 50, 
     moduleSize: 5, 
     addQuietZone: true, 
     marginHRI: 5, 
     bgColor: "#FFFFFF", 
     color: "#000000", 
     fontSize: 10, 
     output: "css", 
     posX: 0, 
     posY: 0, 
     fontOptions: "bold", 
    }; 
    $('#barcoderesult').append('<div id="showBarcode' + i + '"style = "float:left"/> '); 
    $("#showBarcode" + i).barcode(patron, type, settings); 
    $("#showbarcode" + i).animate({height: "100%", width: "100%"}); 
    } 

    if(array_barcode.length > 0){//If array is not empty 
    $.ajax({ 
     //url: "/controller-name/controller-action-name", 
     url: "/MyController/my-action", 
     type: "POST", 
     data: {array_barcode : array_barcode}, 
     success: function (data) { 
     alert("Success"); 
     }, 
     error: function() { 
     alert("Error Ocurred"); 
     }, 
    }); 
    } 
} 
</script> 

-Controller

Da nicht viele Informationen zur Verfügung gestellt wurde. Also nahm ich Controller Name als MyController und Aktion Name als MyAction. Überprüfen Sie jetzt in der Aktion MyAction, ob die kommende Anfrage vom AJAX-Typ ist oder nicht. Andernfalls wird ein Fehler ausgegeben. Matrixwerte durch Yii::$app->request->post() abrufen.

<?php 

class MyController 
{ 

    . 
    . 
    . 
    public function actionMyAction(){ 

    if (Yii::$app->request->isAjax) { 

     $array_barcode = Yii::$app->request->post('array_barcode'); 
     foreach($array_barcode as $barcode){ 

     /* 
     * Your Logic 
     * Save '$barcode' to database 
     */ 

     } 


    } 
    throw new ForbiddenHttpException("Problem Ocurred"); 

    } 
} 
?> 
+1

Danke! Es klappt !! –

Verwandte Themen