gpt4 book ai didi

php - 如何将 jquery 数组传递给 laravel Controller

转载 作者:行者123 更新时间:2023-12-01 05:45:30 24 4
gpt4 key购买 nike

这是我的代码,以便您可以看到我正在尝试做什么

var cont = 1;
var form_data = {};
$('.preview-add-button').click(function(){ //Introduce los nuevos campo
form_data["isexo"] = $('.payment-form #sexo option:selected').text();
form_data["icolor"] = $('.payment-form input[name="color"]').val();
form_data["iraza"] = $('.payment-form #raza option:selected').text();
form_data["itipo"] = $('.payment-form #tipo option:selected').text();
form_data["iprecio"] = $('.payment-form input[name="precio"]').val();
form_data["ipeso"] = $('.payment-form input[name="peso"]').val();
form_data["imonto"] = parseFloat($('.payment-form input[name="precio"]').val()*$('.payment-form input[name="peso"]').val()).toFixed(2);
form_data["remove-row"] = '<span class="glyphicon glyphicon-remove"></span>';
var row = $('<tr></tr>');
$.each(form_data, function( type, value ) {
$('<td class="input-'+type+'"><input type="hidden" class="form-control" name="data-'+type+'" value="'+value+'"></td>').html(value).appendTo(row);
cont++;
});
$('.preview-table > tbody:last').append(row);
calc_total();
$('#sexo').val('');
$('#color').val('');
$('#raza').val('');
$('#tipo').val('');
$('#precio').val('');
$('#peso').val('');
});
console.log( form_data );
$.ajax({
type: "POST",
url: "/compras/create/store",
data: form_data
});

如何将此数组:var form_data = {} 传递到我的 Controller ?除了 form_data["删除行"]

例如:pic

更新:尝试使用ajax,我不知道我的代码是否正常,

$.ajax({
type: "POST",
url: "compras/create/store",
data: form_data
});

$.ajax({
data: form_data
});

我的 Controller ,其中 $data 用于返回数组,但是:

public function store()
{
$compra = new Compra;
$compra->fecha = Input::get('fecha');
$compra->num_factura = Input::get('num_factura');
$compra->id_proveedor = Input::get('proveedor');
$compra->nombre_vendedor = Input::get('nombre_vendedor');
$compra->total = Input::get('total');
$compra->descuento = Input::get('desc');
$compra->itbms = Input::get('itbms');
$compra->total_bruto = Input::get('total_bruto');
$id_compra = $compra->id;
$data = Input::except('remove-row');
if($compra->save()){
Session::flash('message','Guardado Correctamente');
Session::flash('class','success');
}else{
Session::flash('message','Ha ocurrido un error');
Session::flash('class','danger');
}
return $data;
}

返回: {"_token":"tLPlnBix0vQxkjZkHaF9cdIFPvgq7O1U7pTXye8v","fecha":"2014-11-25","num_factura":"2131AJ","provedor":"6 ","nombre_vendor":"DelPotro","id_proveedor":"","ruc":"","telef":"","sexo":"","color":"","raza":"","tipo":"","precio":"","peso":"","total_bruto":"387.00","total":"387.00","desc":"","itbms": ""} 没有 form_data 数组

最佳答案

我在这里给出了执行类似操作的代码,在我的例子中,我将用户兴趣(游泳、唱歌、音乐等)保存到名为 Interests(id,interest_name) 的数据库表中。

首先让我们看一下 javascript:

public function saveInterests()
{
//I declare the array here
var allVals = [];


//I get my values from checkboxes checked in a div with id interests

$('#interests :checked').each(function(){

allVals.push($(this).val());

});


//Now I am making an post ajax call

$.post("http://b2.com/saveInterests", {interests: allVals},function(data)
{
//this is ajax callback function

if(data=='Saved')
{

alert('Saved With Success');

}
else
{
alert('Sorry couldnt save data in Database');
}
});

}

//处理本次Ajax请求的路由路线::post('saveInterests',array('as'=>'interests.Profile','uses'=>'ProfileController@saveInterests'));

//ProfileController 中用于处理路由的 Laravel 代码

 public function saveInterests()
{
$iarray=Input::get('interests'); //getting my array from ajax call to Laravel
foreach($iarray as $userInterest)
{
$inter = new Interest(); //creating a new row in Interest table using model
$inter->interest_name=$userInterest;
$inter->save();
}
return "Success";
}

关于php - 如何将 jquery 数组传递给 laravel Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27127600/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com