0

i have page that do add new record by old way ajax, this code was add new record and return the error or done result message , how can i print the message on div and print result on other div. i try but some one tell me to use JOSN, how can i do that

<script language="JavaScript">
$(document).ready(function() {
});
$("#closeerr").live('click', function() {
    $("#gadget").hide();
});

       var HttPRequest = false;

       function doCallAjax(Mode,Page,ID) {
          HttPRequest = false;
          if (window.XMLHttpRequest) { // Mozilla, Safari,...
             HttPRequest = new XMLHttpRequest();
             if (HttPRequest.overrideMimeType) {
                HttPRequest.overrideMimeType('text/html');
             }
          } else if (window.ActiveXObject) { // IE
             try {
                HttPRequest = new ActiveXObject("Msxml2.XMLHTTP");
             } catch (e) {
                try {
                   HttPRequest = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
             }
          } 

          if (!HttPRequest) {
             alert('Cannot create XMLHTTP instance');
             return false;
          }

          var url = 'AjaxItemsGroupsRecord.php';
          var pmeters = "titems_groups_GroupName=" + encodeURI( document.getElementById("items_groups_GroupName").value) +
                        "&titems_groups_sys_type_ID=" + encodeURI( document.getElementById("items_groups_sys_type_ID").value ) +
                        '&myPage='+Page +
                        "&tID=" + ID +
                        "&tMode=" + Mode;

            HttPRequest.open('POST',url,true);

            HttPRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            HttPRequest.setRequestHeader("Content-length", pmeters.length);
            HttPRequest.setRequestHeader("Connection", "close");
            HttPRequest.send(pmeters);


            HttPRequest.onreadystatechange = function()
            {

                 if(HttPRequest.readyState == 3)  // Loading Request
                  {
                   document.getElementById("mySpan").innerHTML = "looding";
                  }

                 if(HttPRequest.readyState == 4) // Return Request
                  {
                   document.getElementById("mySpan").innerHTML = HttPRequest.responseText;
                  }

            }

       }
    </script>  
  • You should look into jQuery and the `ajax()` method. It would be much tidier than you have here: http://api.jquery.com/jQuery.ajax/ Basically handles all the HttpRequest bits for you so you can focus on the more important aspects. – diggersworld Nov 21 '12 at 16:22

1 Answers1

0

If jQuery is an option... As mentioned in my comment I'd recommend you try out jQuery http://jquery.com/ as you look to be fairly new to JavaScript. It makes AJAX requests a lot simpler and you don't have to worry about making XMLHttpRequest work cross browser.

For making an actual AJAX request see: http://api.jquery.com/jQuery.ajax/

Now if you want to use JSON you need to convert the data to return in your PHP script. This is really easy, you just pass the data in json_encode() and it will convert the data to a JSON string. You then just echo it out so that it's returned to the AJAX request.

echo json_encode($data);

Now if you've setup your AJAX request to expect a JSON response then you can use the data that comes back. So something like this:

$.ajax({
    url: 'request.php',    // the php you want to call
    dataType: 'json'       // the type of data being returned
}).done(function(json) {
    // you now have a json object
});

If you can only use native JavaScript... If you can't use jQuery then it roughly works the same way. You'd have the code in your example for the AJAX request. You'd still use json_encode() in the PHP. The only difference is when the data comes back you'd need to parse it like so:

JSON.parse(json);

For more info on this last bit checkout: Parse JSON in JavaScript?

Community
  • 1
  • 1
diggersworld
  • 12,770
  • 24
  • 84
  • 119