[eluser]yorvik[/eluser]
Hello I have this litte code where I update value of the database with jquery AJAX.
Code:
class Art_controller extends Controller {
function input() {
$this->load->helper('form');
$this->load->helper('html');
$this->load->model('art_model');
$query = $this->art_model->get_records($this->uri->segment(3));
if ($query) {
$data['records'] = $query;
$data['main_content'] = 'editwork_form';
$this->load->view('includes/template', $data);
}
}
function submit()
{
//load form validation
$this->load->library('form_validation');
$this->load->model('art_model');
//set form rules
$this->form_validation->set_rules('kunstwerk', 'Kunstwerk', 'trim|required|min_length[5]');
//run form validation
$success = $this->form_validation->run();
//if the validation was a success
if ((IS_AJAX && $success) || (!IS_AJAX && $success)) {
echo "Congradulations, your form was accepted!";
$data = array(
'schilderijNaam' => $this->input->post('kunstwerk'),
'kunstenaarId' => $this->input->post('kunstenaarID')
);
$this->art_model->edit_record($this->uri->segment(3), $data);
}
//if validation failed
else {
echo strip_tags(validation_errors());
}
}
}
Now i load this view: (but when I typ $artwork less than 5 caracters I get in an continous loop) I want my errors to be showed but that is not the case. Can someone help?
Code:
<h1>Edit artwork!</h1>
<fieldset>
<legend>Art information</legend>
<?php
$attributes = array('id' => 'myForm');
echo form_open('art_controller/submit/' . $this->uri->segment(3), $attributes);
if (isset($records)) : foreach ($records as $row) :
?>
<?php
echo '<label for="kunstwerk">Kunstwerk: </label>' . form_input('kunstwerk', set_value('kunstwerk', $row->schilderijNaam));
echo '<label for="kunstenaarId">Kunstenaar: </label>' . form_input('kunstenaarId', set_value('kunstenaarId', $row->kunstenaarId));
echo '<label for="categorieId">Categorie: </label>' . form_input('categorieId', set_value('categorieId', $row->categorieId));
echo '<label for="oplage">Oplage: </label>' . form_input('oplage', set_value('oplage', $row->oplage));
echo '<label for="afmetingen">Afmetingen: </label>' . form_input('afmetingen', set_value('afmetingen', $row->afmetingen));
echo '<label for="prijs">Prijs: </label>' . form_input('prijs', set_value('prijs', $row->prijs));
$dataTextarea = array(
'name' => 'infoNL',
'id' => 'infoNL',
'value' => set_value('infoNL', $row->infoNL),
'rows' => '5',
'cols' => '32'
);
echo '<label for="infoNL">Info NL: </label>' . form_textarea($dataTextarea);
$dataTextarea = array(
'name' => 'infoNL',
'id' => 'infoNL',
'value' => set_value('infoEN', $row->infoEN),
'rows' => '5',
'cols' => '32'
);
echo '<label for="infoEN">Info EN: </label>' . form_textarea($dataTextarea);
echo '<label for="datum">Date: </label>' . form_input('datum', $row->datum);
echo form_submit('submit', 'Edit Work', 'id="submit"');
echo form_close();
?>
<?php endforeach; ?>
<?php else : ?>
<h2>No records were returned</h2>
<?php endif; ?>
<div id="error"></div>
</fieldset>
[removed]
$(document).ready(function() {
$("form").submit(function(e) {
e.preventDefault();
v_kunstwerk = $('input[name="kunstwerk"]').val();
v_kunstenaarID = $('input[name="kunstenaarId"]').val();
v_categorieID = $('input[name="categorieId"]').val();
v_oplage = $('input[name="oplage"]').val();
v_prijs = $('input[name="prijs"]').val();
v_date = $('input[name="datum"]').val();
if (v_kunstwerk != null) {
$.ajax({
url: 'http://localhost/codeIgniter/index.php/art_controller/submit/<?php echo$this->uri->segment(3); ?>',
type: 'POST',
data: {
kunstwerk: v_kunstwerk,
kunstenaarID: v_kunstenaarID,
categorieID: v_categorieID,
oplage: v_oplage,
prijs: v_prijs
},
success: function(data) {
alert(data);
$('#error').text(data);
}
});
}
});
});
[removed]
Edit: I fixed the problem with the following code.