0

I have a simple form, where user can set some numeric value and add a comment. Then it is being send to database via AJAX call.

So I have two inputs: a numeric slider (text field) and a textarea for comment (and of course submit button).

My JavaScript that calls Ajax after clicking the button looks like that:

jQuery("button.ocen").click(function(event){


    var dok = jQuery(this).parent().find("input#dok").val(); //  this is always integer
    var comment = jQuery(this).parent().find("#komentarz").val(); // this is string

    var data = {
            action: 'ocen',
            dok: dok,
            comment: comment
    };
     var jqxhr = jQuery.post("http://mydomain/wp-admin/admin-ajax.php", data, function(response){
        }).done(function(response){
            jQuery(".odpowiedz").html(response);
            setTimeout(function(){location.reload();}, 1500);
        });        
    event.preventDefault(); 
});

I used this method succesfully for about 7 months. But lately after one of the wordpress' actualizations it stopped to work correctly: If I set only the numeric value, it is being saved to database correctly, but if I add a comment, the page is being reloaded but nothing is being saved to DB.

What can be wrong with it?

EDIT: the php code:

function ajax_ocen()
{
extract($_POST);
global $wpdb;
$wpdb->show_errors;
    if(!isset($comment))
        $comment = "";

    $q = "INSERT INTO my_table(date, dok, comment) VALUES(NOW(), $dok, '$comment')";

    $res = $wpdb->query($q);
    if($res)
        echo "<div class='success'>Added</div>";

    $wpdb->show_errors;

exit();
}

EDIT:

OK, I found out that comment is not being saved if it contains special language-specific characters. How can I escape/convert those charracters to be properly saved to DB?

  • Can you post the PHP code? – cybmeta May 13 '15 at 21:06
  • Yes - see my edit – Gacek May 13 '15 at 22:08
  • You use extract($_POST); and then $comment and $dot; that expect that the name attribute of the textarea is comment ($comment extracted from $_POST['comment']) and the name attribute of the input is dok ($dok extracted from $_POST['dok']), are those values correctly set in the name attributes? Can you post the HTML also? Aside note: extract() was abandoned by WordPress core and, in general, it is not recommended because it makes debugging more difficult, as you can see in this situation. – cybmeta May 14 '15 at 5:32
  • Yes, they are correctly set. – Gacek May 17 '15 at 11:23
  • Try to echo the resulting query $q. (Also, please use $wpdb->escape() to avoid possible SQL injections.) – giraff May 17 '15 at 12:37
2

OK, I found out that my DB encoding was LATIN2 and wordpress forced UTF-8 in all calls after the update.

When I changed my database's encoding to UTF-8 it all started to work. I also used:

$comment = mb_convert_encoding($comment, "UTF-8");

just to be shure.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.