我正在动态地使用JavaScript函数添加多个文本框,我想从这些框插入到codeigniter中的表中,但是即使没有显示任何错误也没有插入数据。任何身体都可以让我摆脱这个问题。提前谢谢
我的型号代码
public function insert_Job_question($ques_data) {
$this->db->set($ques_data);
$this->db->insert('job_question',$ques_data);
}
我的控制器代码是
$questions = $this->input->post('question');
if (is_array($questions)) {
foreach ($questions as $id => $ques) {
$ques_data = array(
'job_post_id' => $new_job_Id,
'question' => $ques
);
$this->db->insert('job_question', $ques_data);
}
}
my view code is
<div class="row">
<div class="span8">
<div class="about-heading">
<div class="head-menu"> </div>
</div>
<div class="about-section">
<div class="about-content rigs-content">
<form name="jobpost" action=" <?php echo base_url('employer/job_post/post_job'); ?>" method="post">
<h2>Your Job Detail :</h2>
<div class="invest-form">
<ul>
<li class="pull-right">
<label>Job Category:</label>
<input type="text" name="jobcat" value="" placeholder="Enter Job Category*" />
</li>
<li class="pull-right">
<label>Job Title:</label>
<input type="text" name="jobtitle" value="" placeholder="Enter Job Title*" />
</li>
<li>
<label>Job Type:</label>
<input type="text" name="jobtype" value="" placeholder="Enter Job Type*" />
</li>
<li class="pull-right">
<label>Preffered Age:</label>
<input type="text" name="age" value="" placeholder="Enter Job Title*" />
</li>
<li>
<label>Preffered Gender:</label>
<input type="text" name="gender" value="" placeholder="Enter Job Type*" />
</li>
<li>
<label>Job Description:</label>
<input type="text" name="desc" value="" placeholder="Enter Job Type*" />
</li>
<li class="pull-right">
<label>Location:</label>
<input type="text" name="location" value="" placeholder="Enter the Country*" />
</li>
<li>
<label>Post Code:</label>
<input type="text" id="post" rel="popover" data-trigger="hover" name="postcode" placeholder="Enter the City*" />
</li>
<li class="pull-right">
<label>Salary:</label>
<input type="text" id="salaries" name="salary" placeholder="Enter the Salary*" />
</li>
<li>
<li>
<label>Qualification:</label>
<input type="text" id="qualification" rel="popover" data-trigger="hover" name="qualification" placeholder="Enter Benifits*" />
</li>
<li class="pull-right">
<label>Category:</label>
<input type="text" id="category" class="span12" required name="category" placeholder="Enter Job Tags" />
</li>
<label>Benifits:</label>
<input type="text" id="benefits" rel="popover" data-trigger="hover" name="benefits" placeholder="Enter Benifits*" />
</li>
<li class="pull-right">
<label>Job Tags:</label>
<input type="text" id="jobtags" class="span12" required name="jobtag" placeholder="Enter Job Tags" />
</li>
<li class="pull-right">
<label>Career Level:</label>
<input type="text" id="career" class="span12" required name="career" placeholder="Enter Job Tags" />
</li>
<li>
<label>Country:</label>
<?php $countries = getCountriesList(); ?>
<select name="company_country">
<option>Select your country*</option>
<?php foreach ($countries as $country) { ?>
<option value="<?php echo $country->name ?>" <?php echo(@$userComp->country == $country->name ? 'selected' : '') ?> > <?php echo $country->name; ?></option>
<?php }
?>
</select>
</li>
<li class="pull-right">
<table id="question">
<th>Add Killer Questions</th>
<tr>
<td><input type="text" id="txtquestion" name="question"/></td>
<td><input type="button" id="btnAdd" class="button-add" onClick="insertTextBox()" value="Add More"></input></td>
</tr>
</table>
</li>
<br>
<input type="submit" value="Save As Draft" class="button-next" />
</div>
</form>
<div class="wid-social">
</div>
</div>
</div>
</div>
<script>
var index = 1;
function insertTextBox(){
var table=document.getElementById("question");
var row=table.insertRow(table.rows.length);
var cell1=row.insertCell(0);
var t1=document.createElement("input");
t1.id = "question"+index;
t1.name = "question";
cell1.appendChild(t1);
var cell2=row.insertCell(1);
index++;
}
var total = 0;
$('input[type="text"]').each(function(){
if(this.val().length > 0)
total = total + 1;
});
alert("Number of non-empty textboxes on page are: " + total);
</script>
答案 0 :(得分:1)
您需要更改此行
<td><input type="text" id="txtquestion" name="question"/></td>
要
<td><input type="text" id="txtquestion" name="question[]"/></td>
注意,我们添加了两个括号,表示这是一个数组。
然后在JS代码中更改此行
t1.name = "question";
要
t1.name = "question[]"; // Optional use an incremented index t1.name = "question[i]";
然后在您的控制器中,您可以循环播放它或任何您喜欢的内容。
foreach ( $this->input->post('question') as $question)
{
// some stuff here
}