我目前有一个页面,用户可以在其中添加他们的技能。这包括一个文本框和滑块来选择他们的技能水平,这一切都有效。
我有一个'skill_list'
包含技能列表的数据库表,当用户开始在文本框中输入相应的技能时,会显示 jquery 自动完成。尽管用户仍然可以键入任何他们想要的内容,然后保存这个技能,该技能被插入到另一个名为'skills'
.
'skills'
只有当文本完全等于数据库表中的技能名称时,我才希望能够将文本框中的数据插入到'skill_list'
数据库表中?因此,用户不应该能够保存他们添加的技能,并可能使用 AJAX 提示错误。
看法
<form method="post" action="skills/add" id="container">
<script>
$.fn.addSlide = function () {
return this.each(function () {
var $this = $(this),
$num = $('.slide').length++,
$name = $('<input type="text" class="inputField" id="autoskill-' + $num + '" name="skill-' + $num + '" placeholder="What\'s your skill?"></div>'),
$slide = $('<br><div class="slide" id="slider-' + $num + '"></div><br>'),
$amt = $('<input name="amount-' + $num + '" id="amount-' + $num + '" class="inputField" readonly placeholder="Slide to select this skill level..."/><br>');
$this.append($name).append($amt).append($slide);
$('.inputField').autocomplete({
source:'skills/auto',
minLength:2
});
console.log($('#autoskill'));
$slide.slider({
value: 0,
min: 0,
max: 5,
step: 1,
slide: function (event, ui) {
$amt.val(ui.value);
}
});
});
}
$('body').on('click', '.addNew', function(event) {
event.preventDefault();
$('#newFields').addSlide();
});
var count = 0;
$(document).ready(function(){
$('.addNew').click( function(event){
event.preventDefault();
$('#length').html(count);
count++;
});
$('.submitButton').click( function(event){
$('#container').append('<input type="hidden" name="count" value="' + count + '">');
});
});
</script>
<button class="addNew submitButton"> Add a Skill </button><br>
<div id="newFields"></div>
<input type="submit" class="submitButton" value="Save Skills">
</form>
控制器
public function auto(){
$skill = $_GET['term'];
$query = $this->db->query('SELECT * from skill_list WHERE name LIKE "%'.$skill.'%" ORDER by name ASC LIMIT 10');
$data = array();
if ( $query && $query->num_rows() ){
foreach ($query->result_array() as $row){
$data[$row['id']]=$row['name'];
}
}
echo json_encode($data);
flush();
}
public function add(){
$this->load->view('header');
$id = $this->session->userdata('id');
$skill_points = 0;
for ($i = 0; $i < $_POST['count']; $i++) {
$skill = $_POST['skill-'.$i.''];
$level = $_POST['amount-'.$i.''];
$points = $_POST['amount-'.$i.''] + 1;
$query = $this->db->get_where('jobseeker_profiles', array('jobseeker_id' => $id));
$user = $query->row_array();
$this->db->set('jobseeker_profile_id', $user['id']);
$this->db->set('name', $skill);
$this->db->set('level', $level);
$this->db->set('points_value', $points);
$this->db->insert('skills');
$skill_points += $points;
}
$this->db->set('skills_value', $skill_points);
$this->db->where('jobseeker_id', $id);
$this->db->update('jobseeker_profiles');
$query = $this->db->get_where('jobseeker_profiles', array('jobseeker_id' => $id));
$user = $query->row_array();
$skills = $user['skills_value'];
$qualifications = $user['qualifications_value'];
$work_history = $user['work_history_value'];
$total = $skills + $qualifications + $work_history;
$this->db->set('total_points_value', $total);
$this->db->where('jobseeker_id', $id);
$this->db->update('jobseeker_profiles');
$this->db->set('points_value', $total);
$this->db->where('jobseeker_profile_id', $user['id']);
$this->db->update('applications');
redirect('skills');
}
谢谢