[eluser]Zerg[/eluser]
Yes, I'm trying to save data in the database, but apparently problem is in getting values from form. Everything (saving in the database too) when I don't type html tags in form. Maybe as you suggest some code would help
The view:
Code:
<?php
$this->load->helper('form');
echo form_open('add');
?>
<div class="error">
<?=$this->validation->error_string;?>
</div>
Title:
<input type="text" name="title" value="<?=$this->input->post('title')?>"/>
//Description: <textarea name="description"><?=$this->input->post('description')?></textarea>
Tags (seperate by comma): <input type="text" name="tags" value="<?=$this->input->post('tags')?>"/>
<?php
echo form_submit('submit', 'Add!');
echo form_close(); ?>
The controller:
Code:
// [...] part of class Scripts
function addnew() {
if($this->input->server('REQUEST_METHOD') == 'POST') {
$rules['title']='trim|required|min_length[8]';
$rules['description']='trim|required|min_length[15]';
$rules['tags']='trim|required|min_length[2]';
$this->validation->set_rules($rules);
if($this->validation->run() == true) {
// model name is scr
$result = $this->scr->add(array(
'title' => $this->input->post('title'),
'description' => $this->input->post('description'),
'tags' => $this->input->post('tags')
));
if($result) {
redirect('scripts/view/'.$result, 'location');
} else {
// todo: redirect to error page
echo 'Not logged in!';
}
} else {
$this->response['content'] = $this->load->view('scr/new_script.php', '', True);
$this->load->view('index.php', $this->response);
}
} else {
$this->response['content'] = $this->load->view('scr/new_script.php', '', True);
$this->load->view('index.php', $this->response);
}
}
[...]
And the model:
Code:
[...] part of class Scr
function add($array) {
// tag
$tags = $this->tags->getTagsFromInput($array['tags']);
unset($array['tags']);
if($this->session->userdata('logged')) {
$array['user_id'] = $this->session->userdata('id');
$this->db->insert('scripts', $array);
$id = $this->db->insert_id();
foreach($tags as $value) {
$this->tags->addTag($id, $value);
}
return $id;
}
return false;
}
// [...]
It can't be simpler, I suppose.