我试图一次生成多个条形码,并将生成的条形码号码保存在我的数据库中。现在我已经生成了多个条形码,但是我在将它保存到数据库时遇到了问题。
-我的代码到现在为止:-
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%"});
}
}现在在我的可变赞助人中,它包含多个生成的条形码号码。现在,我需要将赞助人中的条形码号推送到数组中,并通过ajax传递yii2控制器中的数组?我该怎么做呢?我一点也不知道。
发布于 2017-08-23 07:25:09
首先,创建一个数组,即array_barcode,并使用JavaScript阵列推送()方法向该数组填充值。然后,如果不为空,则在for循环之后,通过AJAX将这个数组值传递给控制器。
<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>控制器
因为没有提供多少资料。因此,我假设控制器名为MyController,操作名称为MyAction。现在,在实际的MyAction中,检查请求是否属于AJAX类型。否则,抛出错误。通过Yii::$app->request->post()检索数组值。
<?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");
}
}
?>https://stackoverflow.com/questions/45832794
复制相似问题