Wednesday, 4 September 2013

Insert into the database only if the data exists in a database table? PHP/AJAX

Insert into the database only if the data exists in a database table?
PHP/AJAX

I currently have a page where a user can add their skills. This comprises
of a textbox and slider to select their skill level, that is all working.
I have a database table 'skill_list' which has a list of skills and when
the user starts typing into the textbox appropriate skills are shown with
jquery autocomplete. Though a user can still pretty much type whatever
they want and then save this skill which is inserted into a different
database table called 'skills'.
I want to be able to insert the data within the textbox to the database
table 'skills' only if the text is exactly equal to a skill name within
'skill_list' database table? Therefore a user shouldn't be able to save
the skills they have added and possibly prompted with an error using AJAX.
VIEW
<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>
CONTROLLER
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');
}
Thanks

No comments:

Post a Comment