Skip to content Skip to sidebar Skip to footer

Ajax Not Fully Posting Data To Php File

Getting a really strange error with Ajax not sending the entire string data across to the php script. The string is '

Solution 1:

The problems with your code is you are not encoding the values. So that will mess up the values. Plus you have a leading & which makes no sense. You can manually encode all the values with encodeURIComponent or you can let jQuery handle that for you.

jQuery does the encoding for you when you use an object.

functionSend_upload_news() {

    var get_title = document.getElementById('Germ_title');
    var get_date = document.getElementById('Germ_date');

    varData = {
        input_title : get_title.value,
        input_date : get_date.value,
        input_content : $("div.nicEdit-main").html()
    };

    $.ajax({
        url : "uploadNews.php",
        type: "POST",
        dataType: 'text',
        data : Data,
        success: function(result){alert(result);},
       error: function(XMLHttpRequest, textStatus, errorThrown) {
       alert('There is an error, screenshot this error and send to Admin : ' +textStatus+" - "+errorThrown);
       }
    });

    nicEditors.findEditor( "Germ_content" ).setContent( '' );
    get_title.value = "";
    get_date.value = "";
    $('html, body').animate({ scrollTop: 0 }, 'slow');
}

And you really should use var so your application is not full of global variables.

Solution 2:

You have to escape(encode) sent parameters to the server, to avoid confusion of characters like ampersand(&)

Change this:

var Data = '&input_title='+input_title+'&input_date='+input_date+'&input_content='+input_content;

To this:

varData = '&input_title='+encodeURIComponent(input_title)+'&input_date='+encodeURIComponent(input_date)+'&input_content='+encodeURIComponent(input_content);

Solution 3:

Encode your string properly before firing off the Ajax.

var title = encodeURIComponent(input_title);
 var date = encodeURIComponent(input_date);
 var content = encodeURIComponent(input_content);
 varData = 'input_title='+title+'&input_date='+date+'&input_content='+content;
 $.ajax({
    ...
    data : Data,
    ...

To make your life easier, why not just use object notation and let jQuery handle the encoding?

$.ajax({...data : {
        input_title:input_title, 
        input_date:input_date, 
        input_content:input_content
    },...

Post a Comment for "Ajax Not Fully Posting Data To Php File"