• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
prototype ajax.updater response help

#1
[eluser]sszynrae[/eluser]
hey, I'm pretty new to doing ajax calls, in fact the only thing i really can do is modify Derek Allar's video tutorial. Thankfully, that has worked like a charm by just having an example function in my controller, and echoing out say, a new comment, to my ajax.updater in my javascript file.
However, I want to do apply some server side testing to this, like, say validation. I've managed to echo the validation error message out to my ajax.updater, but it remains blissfully unaware that it's not actually echoing a comment, but an error message, so repeated submits, will just pseudo insert error messages like they were comments.

Can anyone please give me a quick example on the method for making ajax.updater aware of the result from my controller function? I've searched around a bit, but haven't found anything with prototype, and the docs are a bit cryptic for me.

#2
[eluser]TheFuzzy0ne[/eluser]
If you can post the code for your view and controller, we might be able to give you some pointers.

#3
[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>
        &lt;?php if ($query->num_rows() > 0): ?&gt;
    &lt;?php foreach($query->result_array() as $comment): ?&gt;
        &lt;?=$this->load->view('entries/single-comment', array('comment'=>$comment), true) ?&gt;
    &lt;?php endforeach; ?&gt;
    &lt;?php endif; ?&gt;
    <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="&lt;?=$comment['cid']?&gt;"><table class="tcmnt"><tr>
    <td width="30" valign="center">&lt;?=$this->blog_model->gravatar($comment['email'],40,"ava")?&gt;</td>
    <td width="150" align="center" valign="top"><b>
    &lt;?php if ($comment['website']): ?&gt; <a href="&lt;?=$comment['website']?&gt;">&lt;?php endif;?&gt;&lt;?=$comment['name']?&gt;&lt;?php if ($comment['website']):?&gt;</a>&lt;?php endif;?&gt;
    &lt;?php if ($comment['notify'] == 1):?&gt; <a title="&lt;?=$comment['name']?&gt; has subscribed to this post"><img class="icon" src="/imgs/mailbutton.gif"></a>
    &lt;?php endif; ?&gt;</b><br>&lt;?=date("H:i j.M'y",$comment['date'])?&gt;</td>
    <td valign="top">&lt;?=nl2br($comment['commenttext'])?&gt;</td>
    <td align="center" width="40" valign="top">&lt;?=anchor('entries/edit_comment/'.$comment['cid'], 'Edit');?&gt;
    <a id="del" class="del" href="&lt;?=site_url('entries/delete_comment/'.$comment['cid'])?&gt;" title="delete comment">[-]</a>
    <a href="">Reply</a><br /></td>
</tr></table></div>


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.