我是Web编程的新手,尤其是在Codeigniter上。现在,我正在寻找如何从视图向控制器传递/提交数组。
视图中我的HTML脚本的这一部分:
<tr class="rowdim"> <!-- ROW 1 -->
<td><input type="text" id="bookid1" name="book_id[]" /></td>
<td><input type="text" id="qty1" name="qty[]" /></td>
<td><input type="text" id="uom1" name="uom_id[]" /></td>
</tr>
<tr class="rowdim"> <!-- ROW 2 -->
<td><input type="text" id="bookid2" name="book_id[]" /></td>
<td><input type="text" id="qty2" name="qty[]" /></td>
<td><input type="text" id="uom2" name="uom_id[]" /></td>
</tr>
<tr class="rowdim"> <!-- ROW 3 -->
<td><input type="text" id="bookid3" name="book_id[]" /></td>
<td><input type="text" id="qty3" name="qty[]" /></td>
<td><input type="text" id="uom3" name="uom_id[]" /></td>
</tr>
我的ajax:
var det_book = document.getElementsByName("book_id[]");
var det_qty = document.getElementsByName("qty[]");
var det_uom = document.getElementsByName("uom_id[]");
var vdata = {det_book:det_book,det_qty:det_qty,det_uom:det_uom}
$.ajax({
type:"POST",
url:"<?php echo base_url(); ?>trans/StockIn/saveData",
data:vdata,
success:function(returnmsg){
if (returnmsg=='""'){
window.alert(msg);
} else {
window.alert(returnmsg);
}
});
控制器:
$det_book=$_POST["det_book"];
$det_qty=$_POST["det_qty"];
$det_uom=$_POST["det_uom"];
$details = array();
$index=0;
foreach ($det_book as $baris){
array_push($details,array(
'book_id'=>$baris,
'quantity'=>$det_qty[$index],
'uom_id'=>$det_uom[$index]
));
$index++; }
$error="";
if (!$this->db->insert_batch('trx_inbound_detail',$details))
{
$error = $this->db->error();
}
任何遗漏或我的代码有问题吗? 已经在社区中搜索,但仍然没有运气。 如果您还建议其他方式,请赞赏。 谢谢
答案 0 :(得分:0)
是的,您错过了一些东西。
名称为namespace Test
{
public partial class Test : System.Web.UI.Page
{
SqlConnection con = new
SqlConnection(ConfigurationSettings.AppSettings["constr"].ToString());
DataTable dt = new DataTable();
DataSet ds = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetDegreeName();
}
}
public void GetDegreeName()
{
try
{
objlog.Action = "SelectDegree";
objlog.StrName = "Sp_records";
dt = SchoolMarksheet_DataLayer.GetData(objlog);
drpdegreelist.DataSource = dt;
drpdegreelist.Items.Clear();
drpdegreelist.DataTextField = "Degree_Name";
drpdegreelist.DataBind();
drpdegreelist.Items.Insert(0, new ListItem("---Select Degree---
", "0"));
}
catch (Exception ex)
{
}
}
public void bscitclasswise()
{
try
{
objlog.Action = "DisplayRecords";
objlog.StrName = "Sp_records";
objlog.Degree_Id = drpdegreelist.SelectedIndex;
dt = SchoolMarksheet_DataLayer.GetData(objlog);
if (dt.Rows.Count > 0)
{
Gridview.DataSource = dt;
Gridview.DataBind();
ds.Tables.Add(dt);
}
}
catch (Exception ex)
{
}
}
protected void btnprint_Click(object sender, EventArgs e)
{
// for viewing crystal report on button click
try
{
CrystalDecisions.CrystalReports.Engine.ReportDocument rd = new
CrystalDecisions.CrystalReports.Engine.ReportDocument();
rd.FileName = Server.MapPath("~/Report.rpt");
rd.SetDataSource(dt);
CrystalReportViewer1.ReportSource= rd;
CrystalReportViewer1.DataBind();
}
catch(Exception ex)
{
}
}
}
}
的元素不存在。另外,您有三个具有相同名称的输入。
检查this link,了解如何通过ajax传递数组。
答案 1 :(得分:0)
Your first mistake is get the textbox value in multiple fields:
var det_book = $('input[name^=book_id]').map(function(idx, elem) {
return $(elem).val();
}).get();
var det_qty = $('input[name^=qty]').map(function(idx, elem) {
return $(elem).val();
}).get();
var det_uom = $('input[name^=uom_id]').map(function(idx, elem) {
return $(elem).val();
}).get();
在php中,您没有在foreach中提及索引:
foreach ($det_book as $index => $baris) {
array_push($details,array(
'book_id'=>$baris,
'quantity'=>$det_qty[$index],
'uom_id'=>$det_uom[$index]
));
}
print_r($details);
exit();