[eluser]sszynrae[/eluser]
Code:
function ajaxaddcomment()
{
$_POST['date'] = mktime(); $_POST['ip'] = $this->input->ip_address();
$this->blog_model->get_validation_rules(true);
if ($this->input->post('notify') == 1) { $_POST['notify'] = 1; } else { $_POST['notify'] = 0; }
if ( $this->validation->run() == FALSE )
{
echo $this->validation->error_string;
}
else {
unset($_POST['securitycode']);
$this->db->insert('comments', $_POST);
$this->db->where('date',$_POST['date']);
$this->db->where('name',$_POST['name']);
$query = $this->db->get('comments', 1);
foreach($query->result_array() as $newcomment) {
echo $this->load->view('entries/single-comment', array('comment'=>$newcomment));
}
}
}
it's the case validation->run == false that i want to make ajax aware of.
here's my j.script:
Code:
document.observe("dom:loaded", function() {
$('add_comment_form').observe('submit', add_comment);
});
function add_comment(e) {
Event.stop(e);
k = Math.floor(Math.random()*10000000)
$('subbtn').disabled=true; //stops submit button from working
new Effect.Appear('new'); //make new comments container visible
$('new').insert('<div id="new'+k+'"></div>'); //create new div to contain new comment inside container
pars = $('add_comment_form').serialize();
new Ajax.Updater ('new'+k, base_url+'entries/ajaxaddcomment',
{method:'post', postBody: pars});
new Effect.Appear('new'+k);
new Effect.Highlight('new'+k, {delay: 0, startcolor: '#707000', endcolor: '#002a00'});
addobserve.delay(0.7);
}
function addobserve(){
$('new'+k).down('.del').observe('click', remove_comment);
}
basically i want my javascript to do something different if validation->run == false, as of now it's being treated as a success and echoes the validation error as if it were a comment.
and yeah, this probably is not the most effective code to to this stuff, but i'm new and it gets the half the job done. i'm happy to receive any pointers in effectiveness though!
edit: forgot the views: the important divs in comment.php
Code:
<div id="cmntz" class="tabz">
<div class="pagetitle">Comments</div>
<?php if ($query->num_rows() > 0): ?>
<?php foreach($query->result_array() as $comment): ?>
<?=$this->load->view('entries/single-comment', array('comment'=>$comment), true) ?>
<?php endforeach; ?>
<?php endif; ?>
<div id="new" style="display:none"></div>
</div>
<br />
single-comment.php is not really that relevant to my question, nor is it easy to read, but here you go. it is just a div with a table with my styled comment layout. there's a delete button there that gets a listener added by the script.
Code:
<div class="cmnt" id="<?=$comment['cid']?>"><table class="tcmnt"><tr>
<td width="30" valign="center"><?=$this->blog_model->gravatar($comment['email'],40,"ava")?></td>
<td width="150" align="center" valign="top"><b>
<?php if ($comment['website']): ?> <a href="<?=$comment['website']?>"><?php endif;?><?=$comment['name']?><?php if ($comment['website']):?></a><?php endif;?>
<?php if ($comment['notify'] == 1):?> <a title="<?=$comment['name']?> has subscribed to this post"><img class="icon" src="/imgs/mailbutton.gif"></a>
<?php endif; ?></b><br><?=date("H:i j.M'y",$comment['date'])?></td>
<td valign="top"><?=nl2br($comment['commenttext'])?></td>
<td align="center" width="40" valign="top"><?=anchor('entries/edit_comment/'.$comment['cid'], 'Edit');?>
<a id="del" class="del" href="<?=site_url('entries/delete_comment/'.$comment['cid'])?>" title="delete comment">[-]</a>
<a href="">Reply</a><br /></td>
</tr></table></div>