-2

I have a function connected with onclick button. It sends some data through ajax to another php file. Actually everything was working fine, but I tried to add now to 'success' a simple IF statement, that would get true or false from external file. I guess some of the syntax may be wrong here because the error message says that my main fuction is not defined. Could someone find some mistakes here please?

main file (here in success: i have added if else statement only):

              $.ajax({
              type: 'get',
              url: '/editcomment',
              data: {newComment: newComment,
                             id: id,
                         userId: userId},

              success:function(data){
                  if(data.success){
                       $(".newCommentForm" +id).replaceWith("<span id='" + id + "' class='limitedText'>" + newComment + "</span>");
                       $(thisId).data("PinComment", newComment);
                   } 
                   else { alert('error');},

              error:function(){
                       alert('error');
                     }
          });                     
      }

external file (i have added the return arrays here):

public function editComment(){

   $userId = Input::get('userId');   

   if ( $userId == Auth::id() or Auth::user()->hasRole("admin")){

      $id = Input::get('id');
      $newComment = Input::get('newComment');
      DB::update('UPDATE `comments` SET f_text = ? WHERE h_id = ?', array($newComment, $id));
      return array('success' => true);

     } else {
         return array('success' => false);
     }    
}
UberProCoder
  • 33
  • 1
  • 7
  • your given code and description is not clear and complete – Muhammad Usman Aug 26 '15 at 10:56
  • It's difficult to explain the whole project :D I just have some syntax problem in the ajax – UberProCoder Aug 26 '15 at 10:58
  • Even with this simple returned data I would suggest you used `echo json_encode(array('success' => false));` and return your data as a json string. It makes everything easier to use in javascript. Also add `dataType : 'json'` to your `$.ajax` parameter list. – RiggsFolly Aug 26 '15 at 11:01

3 Answers3

1

You have a typo here else { alert('error');},, you need another closing brace to close the success function.

Styphon
  • 10,304
  • 9
  • 52
  • 86
1

You are trying to access PHP array in JS that not possible. What you need to do is, either you need to return response as an string or JSON from PHP file

see: Get data from php array - AJAX - jQuery

Community
  • 1
  • 1
Yogendra
  • 2,139
  • 3
  • 14
  • 27
0

Get rid of commas. Try this in else block

jAlert('No Success', 'alert box');
CaPs LoCk
  • 150
  • 4