biz14
biz14

Reputation: 187

Ajax results from php have extra new line

I have an ajax code as below. The issue now in my php I just write one echo as code incase my sql failed to insert and I tried to compare here but I notice it have new lines extra which it cause to fail this statement if(responseData=="SMGFE\n"). I have even put the extra "\n" together to check but it fails too. Any solution to this issue?

function ajaxLoad(url,postData,plain) {
            //alert("in url : "+url);
            var http_request = false;

            if (window.XMLHttpRequest) { // Mozilla, Safari, ...
                http_request = new XMLHttpRequest();
                if (http_request.overrideMimeType && plain) {
                    http_request.overrideMimeType('text/plain');
                }
            } else if (window.ActiveXObject) { // IE
                try {
                    http_request = new ActiveXObject("Msxml2.XMLHTTP");
                } catch (e) {
                    try {
                        http_request = new ActiveXObject("Microsoft.XMLHTTP");
                    } catch (e) {}
                }
            }
            if (!http_request) {
                alert('Giving up :( Cannot create an XMLHTTP instance');
                return false;
            }
            http_request.onreadystatechange =  function() {
                if (http_request.readyState == 4) {
                    if (http_request.status == 200) {
                          var responseData = http_request.responseText;
                        alert("http response :"+responseData+"TEST");
                        if(responseData=="SMGFE\n")
                            {
                                alert("Gname "+document.getElementById("gname").value+" Already Exist");
                            }
                            else{

                            alert("Successfully Inserted");  
                            clearSelection();                   
                              window.opener.location.reload();
                          window.close();

                      }
                    }
                    else {
                        alert('Request Failed: ' + http_request.status);
                    }
                }
            };
        //alert("before post data"+postData.length);
            if (postData) { // POST
                  //alert("post data"+postData.length);
                http_request.open('POST', url, true);
                http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');  
                http_request.setRequestHeader("Content-length", postData.length);
                http_request.send(postData);
            }
            else {
                http_request.open('GET', url, true);
                http_request.send(null);
            }
        }

Upvotes: 0

Views: 766

Answers (2)

John Smith
John Smith

Reputation: 1814

you would be better off trying to remove the line breaks and any white space from the responseData, either in your php file try

trim($your_php_variable_responseData);

See the php manual

> This function returns a string with whitespace stripped from the beginning 
and end of str. Without the second parameter, trim() will strip these characters:

" " (ASCII 32 (0x20)), an ordinary space.
"\t" (ASCII 9 (0x09)), a tab.
"\n" (ASCII 10 (0x0A)), a new line (line feed).
"\r" (ASCII 13 (0x0D)), a carriage return.
"\0" (ASCII 0 (0x00)), the NUL-byte.
"\x0B" (ASCII 11 (0x0B)), a vertical tab.

Or you can try in the javscript

responseData= responseData.replace(/^\s+|\s+$/g,"");

Upvotes: 2

TFennis
TFennis

Reputation: 1423

I don't exactly understand what you are trying to do but isn't this a solution to your problem?

if(responseData.substr(0,5) =="SMGFE")

Upvotes: 1

Related Questions