views:

18

answers:

2
$.get("/student/Studentnumber.php", $("myform").serialize(),
    function(data){
            alert(data);
            if(data.substring(0,7) == 'STDERR'){
                //$("#MYSPAN").text(data.substring(8));
        } else {
                //$("#MYSPAN").text(data);
        }
    }
);

In the Above Script I have The PHP response coming from the "Studentnumber.php" .The "Data" field contains the Contains the "Student NAME" if the correct number is entered .if a wrong number is entered it displays the "STDERR:SNO NOT VALID ".Now Here I want the respone only "SNO not VALID " when a wrong number is entered .I want to over come the "STDERR:"

A: 

You're close.

The index of the .substring() calls is off by 1.

Try it out: http://jsfiddle.net/2GcS3/

if(data.substring(0,6) == 'STDERR'){
    $("#MYSPAN").text(data.substring(7));
} else {
    $("#MYSPAN").text(data);
}​

Update: This answer will still work, if you use $.trim() to get rid of the extra white space.

data = $.trim(data);

Otherwise, use the answer below.


EDIT: From your comment, there is a space at the beginning of the STDERR. In that case, you need to go back to the numbers you had, and add that space to the STDERR string for comparison.

Try it out: http://jsfiddle.net/2GcS3/1

if(data.substring(0,7) == ' STDERR'){
    $("#MYSPAN").text(data.substring(8));
} else {
    $("#MYSPAN").text(data);
}​
patrick dw
I have White Space coming in front of the as follows " STDERR:NUMBER NOT VALID" .How do we overcome the white space
Someone
@Someone - You would go back to your original indexes, and add the space to the beginning of the comparison string. `' STDERR'`
patrick dw
@patrick:I have used the $.trim which is trimming white space in front of teh string.but it is still displaying the "STD_ERROR:"
Someone
@Someone - `$.trim()` should be used with my first example, not the second. Using `$.trim()`, here's a complete example: http://jsfiddle.net/2GcS3/5/
patrick dw
@patrick: Thanks it worked out .I dont have Enough reputation to rate your answer any way thanks
Someone
@Someone - When you get an answer that solves the issue, you can click the big checkmark to the left of the answer. This confirms that it was the correct answer for your question. It gives you a couple reputation points, and helps the reputation of the one who gave the answer.
patrick dw
A: 

You can manipulate your response text in many ways using the string functions.

For example:

  1. Use data.substring(7) : This will return the required text.

  2. Use the 'split' function also based on you delimiter ':' .

I think this will help you solve your problem.

Siva