Jump to content

Recommended Posts

I'm still very new to javascript and ajax, and have read some, and then done some hands on learning.

 

I'm running into a problem where I'm sending data via GET to PHP, and then returning a value.  In the process of hacking it apart, I ran into a problem that *sometimes* the request.responseText seems to have an extra carriage feed (or line return) in it.

 

You can see it in action here:

 

http://www.dixieandtheninjas.net/hunter/dev/hunter_test2.php

 

if you click on button 1, it behaves just fine.  You'll get an alert window that clearly shows that the return text has no extra CR/LF.  If you click button 2, it runs the SAME CODE, from a different PHP page, and instead returns a result that has a CR/LF in it.  The same is true for button 3.

 

My PHP code is NOT inserting a line, as far as I can tell.  You can mimic the GET by trying:

http://www.dixieandtheninjas.net/hunter/dev/hunter_test3.php?myshield=2

 

 

Here is an example of the PHP code involved:

 if (isset($_GET['myshield'])) {
    $playershield = $_GET['myshield'];
    
     switch ($playershield) {
    case 1:
        //no shield
        $armorval = '16';
        $text = "no shield";
        break;
    case 2:
        //Buckler shield
        $armorval = '12';
        $text = "buckler shield";
        //$myarmorIDarray[] = $armorval;
        // armorID 12
        break;
    case 3:
        $armorval = '13';
        $text = "medium round shield";
        break;
    case 4:
        $armorval = '15';
        $text = "large \"kite\" shield";
                break;
     }
     //$_SESSION['myarmorIDarray'] = $myarmorIDarray;
    
     echo $text;
    
    }

 

Here is some of the ajax code involved

function commitshieldJS4() {
    
    var varShieldValue = document.getElementById("myshield").value;
    
    
        var url = "hunter_test3.php?myshield=" +
           escape(varShieldValue);
           request.open("GET", url, true);
           request.onreadystatechange = updateshieldJS;
           request.send(null);
}
    
    function updateshieldJS() {
    if (request.readyState == 4) {
           if (request.status == 200) {
    
            var varReturned = request.responseText;
            
             alert ("the response recieved is:  >>" + request.responseText + "<<");
            
           }
           else
           alert ("Error! Request status is " + request.status);
    }
}

 

I'm totally scratching my head here.  I haven't the slightest idea why those extra CR/LFs are there.  Any ideas?  :shrug:

I went to your page : http://www.dixieandtheninjas.net/hunter/dev/hunter_test2.php and tried it.

 

I don't get alert messages, I get replies that tell me there problems.

 

Loaded things into my editor and ran html. Yes, I get a line feed. Changed things so I write to a div in the html page and I get NO LINE FEED.(???) :o

 

maybe from the alert????

 

Don't know where it's coming from. The php is good. What php works with out getting the CRs? Please post that.

Here is the exact code from the page that *does not* produce an extra CR/LF

 if (isset($_GET['myshield'])) {
    $playershield = $_GET['myshield'];
    
    if (isset($_SESSION['myarmorIDarray'])) {
        $myarmorIDarray = $_SESSION['myarmorIDarray'];
    }
    
     switch ($playershield) {
    case 1:
        //no shield
        $armorval = '16';
        $text = "no shield";
        $myarmorIDarray[] = $armorval;
        break;
    case 2:
        //Buckler shield
        $armorval = '12';
        $text = "buckler shield";
        $myarmorIDarray[] = $armorval;
        // armorID 12
        break;
    case 3:
        $armorval = '13';
        $text = "medium round shield";
        $myarmorIDarray[] = $armorval;
        //Medium round shield
        // armorID 13
        break;
    case 4:
        //Large "kite" shield
        $armorval = '15';
        $text = "large \"kite\" shield";
        $myarmorIDarray[] = $armorval;
        // armorID 15
        break;
     }
     $_SESSION['myarmorIDarray'] = $myarmorIDarray;
    
     echo $text;
    //echo "My shield choice is $playershield! and here's an array." . print_r($myarmorIDarray);
    }

 

 

So... you didn't get alert popups?  How strange.

 

Subfighter, the line breaks don't show in a div because they are ignored by the html parser. Dialog boxed show plain text , not HTML.

 

xjermx, it appears you need to get your code and files cleaned up a bit.

button 2 calls "hunter_test3.php?myshield=" but button 3 calls "hunter_test.php?myshield=".

 

"hunter_test3.php?myshield=2" returns

 


buckler shield

 

"hunter_test.php?myshield=2" returns

buckler shield

 

My point is you have inconsistent test cases and incomplete info here. You listed php source, but the 2 buttons point to different php files.

So my first guess is that the other php file is sending different data, actually I know this and have proven it.

My recommendation is to look at the differences between hunter_test.php and hunter_test3.php.

code from hunter_test.php

if (isset($_GET['myshield'])) {
    $playershield = $_GET['myshield'];
    
   
    
     switch ($playershield) {
    case 1:
        //no shield
        $armorval = '16';
        $text = "no shield";
        //$myarmorIDarray[] = $armorval;
        break;
    case 2:
        //Buckler shield
        $armorval = '12';
        $text = "buckler shield";
        //$myarmorIDarray[] = $armorval;
        // armorID 12
        break;
    case 3:
        $armorval = '13';
        $text = "medium round shield";
        //$myarmorIDarray[] = $armorval;
        //Medium round shield
        // armorID 13
        break;
    case 4:
        //Large "kite" shield
        $armorval = '15';
        $text = "large \"kite\" shield";
        //$myarmorIDarray[] = $armorval;
        // armorID 15
        break;
     }
     //$_SESSION['myarmorIDarray'] = $myarmorIDarray;
    
     echo $text;
    //echo "My shield choice is $playershield! and here's an array." . print_r($myarmorIDarray);
    }

 

Code from hunter_test3.php

if (isset($_GET['myshield'])) {
    $playershield = $_GET['myshield'];
    
   
    
     switch ($playershield) {
    case 1:
        //no shield
        $armorval = '16';
        $text = "no shield";
        //$myarmorIDarray[] = $armorval;
        break;
    case 2:
        //Buckler shield
        $armorval = '12';
        $text = "buckler shield";
        //$myarmorIDarray[] = $armorval;
        // armorID 12
        break;
    case 3:
        $armorval = '13';
        $text = "medium round shield";
        //$myarmorIDarray[] = $armorval;
        //Medium round shield
        // armorID 13
        break;
    case 4:
        //Large "kite" shield
        $armorval = '15';
        $text = "large \"kite\" shield";
        //$myarmorIDarray[] = $armorval;
        // armorID 15
        break;
     }
     //$_SESSION['myarmorIDarray'] = $myarmorIDarray;
    
     echo $text;
    //echo "My shield choice is $playershield! and here's an array." . print_r($myarmorIDarray);
    }

 

Code from new_fight1.php

    if (isset($_GET['myshield'])) {
    $playershield = $_GET['myshield'];
    
    if (isset($_SESSION['myarmorIDarray'])) {
        $myarmorIDarray = $_SESSION['myarmorIDarray'];
    }
    
     switch ($playershield) {
    case 1:
        //no shield
        $armorval = '16';
        $text = "no shield";
        $myarmorIDarray[] = $armorval;
        break;
    case 2:
        //Buckler shield
        $armorval = '12';
        $text = "buckler shield";
        $myarmorIDarray[] = $armorval;
        // armorID 12
        break;
    case 3:
        $armorval = '13';
        $text = "medium round shield";
        $myarmorIDarray[] = $armorval;
        //Medium round shield
        // armorID 13
        break;
    case 4:
        //Large "kite" shield
        $armorval = '15';
        $text = "large \"kite\" shield";
        $myarmorIDarray[] = $armorval;
        // armorID 15
        break;
     }
     $_SESSION['myarmorIDarray'] = $myarmorIDarray;
    
     echo $text;
    //echo "My shield choice is $playershield! and here's an array." . print_r($myarmorIDarray);
    }

 

If you see something I'm missing, please share.  Otherwise, that's why I'm confused - the php isn't doing anything different in the three different test cases.

check your open and close php tags. Anything outside the tags will be returned to the browser.

 

<?php
echo "one";
?>

will return one line containing 'one'

 



<?php
echo "one";

?>

will return 3 blank lined before 'one' and one after

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.