vostro Posted November 19, 2011 Report Share Posted November 19, 2011 Sveiki, gribēju uzzināt kā pareizi izvadīt kļūdu AJAX formā. JS $('#formAddComents').submit(function(){ var send = $(this).serialize(); $.ajax({ type: 'POST', url: '/news/add', data: send, dataType: "json", success: function(msg){ $('.commentsList').append("<div class='commentsImage'><div><img src='/users_images/"+msg.user_image+"' border='1' width='100' height='90' /></div></div><div class='commentsText'>"+msg.text+"</div><div class='commentsUsersData'><span style='margin-right: 5px; margin-top: 5px; margin-left: 5px; font-size: 12px;'><a href='/users/"+msg.user_id+"'>"+msg.author+"</a></span> <span style='float: right; margin-right: 5px; margin-top: 5px; font-size: 12px; color: gray; '>"+msg.date+"</span></div><br />"); $(":input").val(''); } }); return false; }); PHP $page_id = $this->input->post('page_id'); $user_id = $this->input->post('user_id'); $user_images = $this->input->post('user_image'); $author = $this->input->post('name'); $text = $this->input->post('textComments'); $date = date("d.m.y H:i:s"); $data = array( 'page_id' => $page_id , 'user_id' => $user_id , 'user_image' => $user_images , 'author' => $author , 'text' => $text , 'date' => $date ); $this->db->insert('comments', $data); $c = json_encode($data); echo $c; Man vajag tikai pārbaudīt vai lauks nebija tukšs!!! Interesē, kā izvadīt no servera puses ? Un nodot json Quote Link to comment Share on other sites More sharing options...
briedis Posted November 19, 2011 Report Share Posted November 19, 2011 php pusē: exit(json_encode(array('error' => 'Kāds lauks nebija aizpildīts')); js pusē: success: function(msg){ if(msg.error){ alert(msg.error); return; } //Tālāk daram vajadzīgo ar msg, ja nav kļūdu Quote Link to comment Share on other sites More sharing options...
vostro Posted November 19, 2011 Author Report Share Posted November 19, 2011 (edited) Paldies briedis Pietrūkst vienas iekavas :) izlabots exit(json_encode(array('error' => 'Kāds lauks nebija aizpildīts'))); Edited November 19, 2011 by vostro Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.