我有一个codeigniter
中的视图表格。当我输入计算机序列号时,与此计算机有关的所有值都将使用json方法显示在表单上。现在,如果我想添加10台计算机,该如何在视图中进行操作,并将其插入数据库中。有人可以建议我采取动态方法吗?
Controller function:
public function getComputerDetails() {
$comsno=$this->input->post('comsno');
$this->load->model('calendarmodel');
$result=$this->calendarmodel->getComputerDetails($comsno);
if (isset($result)) {
echo json_encode($result);
}
}
Model:
public function getComputerDetails($comsno) {
$this->db->where('com_sno', $comsno);
$query = $this->db->get('computers');
return $query->row();
}
}
?>
完整视图:
<?php include('calendar_header.php');?>
<head>
<!--<link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>assets/css/style.css">-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
</head>
<body>
<!--<img src="<?php echo base_url();?>C:\xampp\htdocs\CI_project\images\computers.png" style="width: 600px">-->
<!--<img src="<?php echo base_url(); ?>images/computers.png">-->
<div class="container">
<?php if($error=$this->session->flashdata('number_exists')):?>
<div class="row">
<div class="col-lg-6">
<div class="alert alert-dismissible alert-success">
<strong>Customer number</strong> <a href="#" class="alert-link">is</a> registered.
</div>
</div>
</div>
<?php endif;?>
<?php if($error=$this->session->flashdata('no_number')):?>
<div class="row">
<div class="col-lg-6">
<div class="alert alert-dismissible alert-danger">
<strong>The number does not exist</strong> <a href="#" class="alert-link">To add a New Customer</a> click Add Customer
</div>
</div>
</div>
<?php endif;?>
</div>
<?= form_open('autocomplete/store_calendar',['class'=>'form-horizontal'])?>
<img src="<?php echo base_url();?>assets/image/computers.jpg" alt="computers">
<div class="container">
<div class="form group row">
<div class="col-lg-12">
<div align="left">
<div class=" form group row">
<div class="col-lg-2">
<a class="nav-link" href="#">Customer Phone Number<span class="sr-only"></span></a>
<?=form_input(['name'=>'cus_phone','id'=>'cus_phone','class'=>'form-control','placeholder'=>'customer phone number','value'=>set_value('cus_phn')])?>
</div>
<div class="col-lg-5">
<?php echo form_error('cus_phone',"<p class='text-danger'>","</p>");?>
</div>
<div class="form group row">
<div class="col-lg-12">
<!--<a class="navbar-brand" href="#">Customer Name</a>-->
<a class="nav-link" href="#">Customer Name<span class="sr-only"></span></a>
<?=form_input(['name'=>'cus_name','id'=>'cus_name','class'=>'form-control','placeholder'=>'Enter your name','value'=>set_value('cus_name')])?>
</div>
<div class="col-lg-2">
<?php echo form_error('cus_name',"<p class='text-danger'>","</p>");?>
</div>
<!--<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarColor01" aria-controls="navbarColor01" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>-->
</div>
</div>
</div>
</body>
<body>
<div class="float">
<div class="form group row">
<div class="col-lg-12">
<div align="left">
<div class=" form group row">
<div class="col-lg-2" >
<!--<a class="navbar-brand" href="#">Customer Name</a>-->
<a class="nav-link" href="#">Billing Address<span class="sr-only"></span></a>
<?=form_input(['name'=>'cus_badr','id'=>'cus_badr','class'=>'form-control','placeholder'=>'Enter billing address','value'=>set_value('cus_badr')])?>
</div>
<div class="col-lg-5">
<?php echo form_error('cus_badr',"<p class='text-danger'>","</p>");?>
</div>
<!--<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarColor01" aria-controls="navbarColor01" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>-->
<div class=" form group row">
<div class="col-lg-12">
<a class="nav-link" href="#">Customer Ship Address<span class="sr-only"></span></a>
<?=form_input(['name'=>'cus_sadr','id'=>'cus_sadr','class'=>'form-control','placeholder'=>'Enter shipping address','value'=>set_value('cus_sadr')])?>
</div>
<div class="col-lg-2">
<?php echo form_error('cus_sadr',"<p class='text-danger'>","</p>");?>
</div>
</div>
</div>
</div>
</body>
<body>
<div class="float">
<div class="form group row">
<div class="col-lg-12">
<div align="left">
<div class=" form group row">
<div class="col-lg-2" >
<!--<a class="navbar-brand" href="#">Customer Name</a>-->
<a class="nav-link" href="#">Computer Serial No<span class="sr-only"></span></a>
<?=form_input(['name'=>'com_sno','id'=>'com_sno','class'=>'form-control','placeholder'=>'Enter computer seial no','value'=>set_value('com_sno')])?>
</div>
<div class="col-lg-5">
<?php echo form_error('com_sno',"<p class='text-danger'>","</p>");?>
</div>
<!--<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarColor01" aria-controls="navbarColor01" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>-->
<div class=" form group row">
<div class="col-lg-12">
<a class="nav-link" href="#"> Computer Manufacturer <span class="sr-only"></span></a>
<?=form_input(['name'=>'com_make','id'=>'com_make','class'=>'form-control','placeholder'=>'Enter computer make details','value'=>set_value('com_make')])?>
</div>
<div class="col-lg-2">
<?php echo form_error('com_make',"<p class='text-danger'>","</p>");?>
</div>
</div>
</div>
</div>
</body>
<body>
<div class="float">
<div class="form group row">
<div class="col-lg-12">
<div align="left">
<div class=" form group row">
<div class="col-lg-2" >
<!--<a class="navbar-brand" href="#">Customer Name</a>-->
<a class="nav-link" href="#">Computer Model<span class="sr-only"></span></a>
<?=form_input(['name'=>'com_model','id'=>'com_model','class'=>'form-control','placeholder'=>'Enter computer model','value'=>set_value('com_model')])?>
</div>
<div class="col-lg-5">
<?php echo form_error('com_model',"<p class='text-danger'>","</p>");?>
</div>
<!--<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarColor01" aria-controls="navbarColor01" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>-->
<div class=" form group row">
<div class="col-lg-12">
<a class="nav-link" href="#"> Computer Configuration <span class="sr-only"></span></a>
<?=form_input(['name'=>'com_config','id'=>'com_config','class'=>'form-control','placeholder'=>'Enter computer configuration','value'=>set_value('com_config')])?>
</div>
<div class="col-lg-2">
<?php echo form_error('com_config',"<p class='text-danger'>","</p>");?>
</div>
</div>
</div>
</div>
</body>
<body>
<div class="form-group">
<label class="control-label col-sm-2" for="date">Computer Hire Start Date</label>
<div class="col-sm-5">
<input id="date1" type="text" class="form-control" name="training_start_date" value="">
<label class="control-label col-sm-2" for="date">Computer Hire End Date</label>
<div class="col-sm-5">
<input id="date2" type="text" class="form-control" name="training_end_date" value="">
</div>
</div>
<br>
<span> Diff:</span>
<span id='diff'> - </span>
<span> Days</span>
</body>
<body>
<div class="float">
<div class="form group row">
<div class="col-lg-12">
<div align="left">
<div class=" form group row">
<div class="col-lg-2" >
<!--<a class="navbar-brand" href="#">Customer Name</a>-->
<fieldset class="form-group">
<a class="nav-link" href="#"> Hire Mode<span class="sr-only"></span></a>
<div class="form-check">
<label class="form-check-label">
<?=form_input(['name'=>'hire_mode','type'=>'radio','class'=>'form-check-input','placeholder'=>'Enter computer hire mode', 'id'=>"hire", 'value'=>"0", 'checked'=>""])?>
<!--<input type="radio" class="form-check-input" name="optionsRadios" id="optionsRadios1" value="option1" checked="">-->
Monthly
</label>
</div>
<div class="form-check">
<label class="form-check-label">
<?=form_input(['name'=>'hire_mode','type'=>'radio','class'=>'form-check-input','placeholder'=>'Enter computer hire mode', 'id'=>"hire", 'value'=>"1", 'checked'=>""])?>
Weekly
</label>
</div>
<div class="form-check disabled">
<label class="form-check-label">
<?=form_input(['name'=>'hire_mode','type'=>'radio','class'=>'form-check-input','placeholder'=>'Enter computer hire mode', 'id'=>"hire", 'value'=>"2", 'checked'=>""])?>
Daily
</label>
</div>
<div class="col-lg-5">
<?php echo form_error('hire_mode',"<p class='text-danger'>","</p>");?>
</div>
</fieldset>
</div>
</div>
</div>
<!--<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarColor01" aria-controls="navbarColor01" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>-->
</body>
<body>
<div class="float">
<div class="form group row">
<div class="col-lg-12">
<div align="left">
<div class=" form group row">
<div class="col-lg-2" >
<!--<a class="navbar-brand" href="#">Customer Name</a>-->
<a class="nav-link" href="#">Computer Hire Rate<span class="sr-only"></span></a>
<?=form_input(['name'=>'mohr','class'=>'form-control','placeholder'=>'Enter monthly hire rate','value'=>set_value('mohr')])?>
</div>
<div class="col-lg-5">
<?php echo form_error('mohr',"<p class='text-danger'>","</p>");?>
</div>
<!--<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarColor01" aria-controls="navbarColor01" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>-->
</div>
</div>
</body>
<body>
<div class="float">
<div class="form group row">
<div class="col-lg-12">
<div align="left">
<!--<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarColor01" aria-controls="navbarColor01" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>-->
<div class=" form group row">
<div class="col-lg-12">
<a class="nav-link" href="#"> Computer Total Hire Rate <span class="sr-only"></span></a>
<?= form_input(['name'=>'tohr','class'=>'form-control','placeholder'=>'Enter total hire rate','value'=>set_value('dahr')])?>
</div>
<div class="col-lg-2">
<?php echo form_error('tohr',"<p class='text-danger'>","</p>");?>
</div>
</div>
</div>
</div>
</body>
<div class="row" align="center">
<div class="col-lg-10 col-lg-offset-2">
<?= form_reset(['name'=>'reset', 'value'=>'reset','class'=>'btn btn-default'])?>
<?= form_submit(['name'=>'submit', 'value'=>'Submit','class'=>'btn btn-primary'])?>
</div>
</div>
</html>
<script>
$('#date1').datepicker();
$('#date2').datepicker();
$('#date2').change(function () {
var diff = $('#date1').datepicker("getDate") - $('#date2').datepicker("getDate");
$('#diff').text(diff / (1000 * 60 * 60 * 24) * -1);
$('#diff').change();
});
</script>
<script>
$('#diff').change(function () {
var hireMode = $('input[name=hire_mode]:checked').val();
var startDate = $('#date1').val();
var endDate = $('#date2').val();
var mRate = $('input[name=mohr]').val();
$.ajax({
url: "<?php echo base_url();?>autocomplete/pricing",
method: "POST",
data: {
hire_mode:hireMode, com_sdate:startDate, com_edate:endDate,
daily_rate:dRate, weekly_rate:wRate, monthly_rate:mRate
},
success: function(res) {
console.log(res);
}
});
});
$('#cus_phone').on('input', function(){
var phone = this.value;
if (phone.length == 10) {
$.ajax({
url: "<?php echo base_url();?>autocomplete/getUserDetails",
method: "POST",
data: {cusphone:phone},
dataType:"JSON",
success: function(data) {
$('#cus_name').val(data.cus_name);
$('#cus_badr').val(data.cus_badr);
$('#cus_sadr').val(data.cus_sadr);
}
});
}
});
$('#com_sno').on('input', function() {
var com_sno = this.value;
$.ajax({
url: "<?php echo base_url();?>autocomplete/getComputerDetails",
method: "POST",
data: {comsno:com_sno},
dataType:"JSON",
success: function(data) {
console.log(data);
$('#com_make').val(data.com_make);
$('#com_model').val(data.com_model);
$('#com_config').val(data.com_config);
}
});
});
</script>
<script type="text/javascript">
$(document).ready(function(){
var dataString = $("#FormId").serialize();
var url="autocomplete/store_calendar"
$.ajax({
type:"POST",
url:"<?php echo base_url() ?>autocomplete/store_calendar",
data:dataString,
success:function (data) {
alert(data);
}
});
})
</script>