How to fill ajax POST value in a form field?

$(document).ready(function() {
  $('#content-area').mouseup(function() {
   var selection = getSelected();
   if(selection && (selection = new String(selection).replace(/^\s+|\s+$/g,''))){
         $.ajax({
         type: 'POST',
         url : 'check.php',
         data: 'selection=' + encodeURI(selection),
   });
   }
  });

Now how do I access the POSTed 'data' and fill a text field in HTML forms. I'm not clicking a button during the ajax post and I want to do it without reloading the entire page. Any help is much much appreciated..

Answers


you need to define the a success handler in your ajax call. This handler will take three parameters, the first one is the data you are receiving back from the server...

$(document).ready(function() {
  $('#content-area').mouseup(function() {
   var selection = getSelected();
   if(selection && (selection = new String(selection).replace(/^\s+|\s+$/g,''))){
         $.ajax({
         type: 'POST',
         url : 'check.php',
         data: 'selection=' + encodeURI(selection),
         success: function(data){
            alert(data.d);
            $('text_input_selector').val(data.d);
        }
   });
   }
  });

Notice that text_input_selector must be a valid selector of your text input


Try below code sample :

$(document).ready(function () {
    $('#content-area').mouseup(function () {
        var selection = getSelected();

        if (selection && (selection = new String(selection).replace(/^\s+|\s+$/g, ''))) {
            $.ajax({
                type: 'POST',
                url: 'check.php',
                data: 'selection=' + encodeURI(selection),
                beforeSend: function () {
                    //do something
                }
                success: function (response) {
                    $("TEXTFIELD_ID").val(response);
                }
            });
        }
    });

Write Success callback handler. like

 $.ajax({
         type: 'POST',
         url : 'check.php',
         data: 'selection=' + encodeURI(selection),
        contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (result) {
                var returnedvalue = result.d;
                debugger;
            },

Need Your Help

Google Adwords Conversion Code Integration To A CMS Page in Prestashop?

javascript php prestashop google-adwords prestashop-1.6

I've read countless advices how to insert GA Conversion code to order-confirmation.tpl. (like this), but what if i need it inserted in my "Contact Us" CMS page?