I ve been trying to post something using ajax but I m getting a Failed to load resource: the server responded with a status of 500 (Internal Server Error). All other forms on the site using ajax work this is the only one that’s giving the error. Not sure what is going on … maybe someone will notice something I m not.
jQuery
(user_id is already defined in the view. alert returns everything I need to pass to the controller)
$('.one_add').submit(function(e) {
e.preventDefault();
var formID = $(this).attr('id');
var formIDSplit = formID.split('-');
var oneID = formIDSplit[1];
var url = $('.one-link-'+oneID).val();
var desc = $('.one-link-desc-'+oneID).val();
$.post(base_url + "index.php/home/one_link", { user_id : user_id, one_id : oneID, desc : desc, url : url }, function(data)
{
}, "json");
// alert(user_id+'-'+oneID+'-'+desc+'-'+url);
});
The form
<?php
$attributes = array('class' => 'one_add', 'id' => 'add-'.$job->id.'');
echo form_open('ajax/add_one_link', $attributes ); ?>
<input type="text" class="one-link-<? echo $job->id; ?>" value="Url" />
<input type="text" class="one-link-desc-<? echo $job->id; ?>" value="Description" />
<button type="submit">Go!</button>
<?php echo form_close(); ?>
The controller function
function one_link()
{
$this->load->model('one_add_model');
$this->form_validation->set_rules('user_id', 'trim|required|xss_clean');
$this->form_validation->set_rules('url', 'trim|required|xss_clean|strtolower');
$this->form_validation->set_rules('desc', 'trim|required|xss_clean|strtolower');
$this->form_validation->set_rules('one_id', 'trim|required|xss_clean');
$user_id = $this->input->post('user_id');
$url = $this->input->post('url');
$desc = $this->input->post('desc');
$one_id = $this->input->post('one_id');
$this->one_add_model->add_url($user_id, $url, $desc, $one_id);
}
The model
function add_url($user_id, $url, $desc, $one_id);
{
$query_str = "INSERT INTO one_add (`user_id`, `url`, `desc`, `one_id` ) VALUES (?, ?, ?, ?)";
$this->db->query( $query_str, array($user_id, $url, $desc, $one_id) );
}
This was finally showing
And I just loaded the form validaton which was not loaded. But it was in another controller and was causing the same error. Not sure what happened … but it works now.