0

I'm trying to update a record with JQuery/JSON but I'm seeing this error:

An error has occurred:

[object Object]

parsererror

SyntaxError: JSON.parse: unexpected character

My JS:

jQuery('#commentForm').live('submit', function (event) {
event.preventDefault()

    
jQuery.ajax(edit_url, {
    data: jQuery(this).serialize(),
    dataType: 'json',
    type: 'POST',
    success: function (data) {
        if (data.error === 'OK') {
            alert('ok c good')
        } else {
            alert('hi' + data.error)
        }
    },
    error: function(x,y,z){
        alert('An error has occurred:\n' + x + '\n' + y + '\n' + z);
    }
})
    
return false;
})

And my php:

$ret = array(
    'error'             =>  'OK',
);
$update =
    "UPDATE crm_set_users SET ".
        "crm_set_users_civilite = '".mysql_real_escape_string($crm_set_users_civilite)."',".
        "crm_set_users_nom = '".mysql_real_escape_string($crm_set_users_nom)."',".
        "crm_set_users_prenom = '".mysql_real_escape_string($crm_set_users_prenom)."',".
        "crm_set_users_email = '".mysql_real_escape_string($crm_set_users_email)."', ".
        "crm_set_users_telephone = '".mysql_real_escape_string($crm_set_users_telephone)."', ".
        "crm_set_users_portable = '".mysql_real_escape_string($crm_set_users_portable)."'";
        
if($crm_set_users_photo != ""){
    $update .=", crm_set_users_photo = '".mysql_real_escape_string($crm_set_users_photo)."'";
}   
    
$update .=
    "WHERE ".
        "crm_set_users_id = '".mysql_real_escape_string($user_id)."'";




echo json_encode($ret);
exit;

If my php is :

$ret = array(
    'error'             =>  'OK',
);
echo json_encode($ret);
exit;

then it works...

Tks for you help!

3
  • Check your output in a hex editor (NOT in the browser), some garbage before or after (particularly utf-8 BOM) could be the cause. Commented May 6, 2012 at 8:59
  • Welcome to Stack Overflow! Did you know that you only have to write jQuery in its long form once? By wrapping your code in (function($) { .... })(jQuery);, you can use $ no matter if noConflict has been used or not. Commented May 6, 2012 at 9:05
  • 1
    Also, since you have just 1 element you may remove that comma. Commented May 6, 2012 at 9:06

2 Answers 2

3

Probably your PHP throws some Errors or Warnings, which make the returned document not a valid JSON string. Use a debugger like FireBug to see, what actual result is returned by your PHP script.

Following the PHP docu for mysql_real_escape_string (link)it will throw an Error, if there is no active MySQL connection available. Maybe that's your problem.

Sign up to request clarification or add additional context in comments.

Comments

3

The problem must be that the php to update the record fails, and so the json does not get outputted.

I think you need to add a space before the WHERE

$update .=
    " WHERE ".

to give it some room after the rest of the string

1 Comment

This might be correct, but in the source given the SQL query is never sent to the MySQL server. Thus this should not be the reason for the error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.