xjermx Posted April 12, 2011 Share Posted April 12, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/233527-weird-extra-line-in-responsetext-sometimes/ Share on other sites More sharing options...
sunfighter Posted April 13, 2011 Share Posted April 13, 2011 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.(???) 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. Quote Link to comment https://forums.phpfreaks.com/topic/233527-weird-extra-line-in-responsetext-sometimes/#findComment-1201237 Share on other sites More sharing options...
xjermx Posted April 13, 2011 Author Share Posted April 13, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/233527-weird-extra-line-in-responsetext-sometimes/#findComment-1201264 Share on other sites More sharing options...
markjoe Posted April 13, 2011 Share Posted April 13, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/233527-weird-extra-line-in-responsetext-sometimes/#findComment-1201286 Share on other sites More sharing options...
xjermx Posted April 14, 2011 Author Share Posted April 14, 2011 Yes, I'm aware the buttons seem out of whack. Their sole purpose is to display the weird behavior for this thread, so I was not focused on better organization. Any thoughts about why I have those line breaks? Quote Link to comment https://forums.phpfreaks.com/topic/233527-weird-extra-line-in-responsetext-sometimes/#findComment-1201371 Share on other sites More sharing options...
markjoe Posted April 14, 2011 Share Posted April 14, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/233527-weird-extra-line-in-responsetext-sometimes/#findComment-1201678 Share on other sites More sharing options...
xjermx Posted April 14, 2011 Author Share Posted April 14, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/233527-weird-extra-line-in-responsetext-sometimes/#findComment-1201683 Share on other sites More sharing options...
markjoe Posted April 14, 2011 Share Posted April 14, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/233527-weird-extra-line-in-responsetext-sometimes/#findComment-1201746 Share on other sites More sharing options...
xjermx Posted April 14, 2011 Author Share Posted April 14, 2011 That was it! Thanks very much for the help. I had not idea that it would behave in that fashion. Quote Link to comment https://forums.phpfreaks.com/topic/233527-weird-extra-line-in-responsetext-sometimes/#findComment-1201756 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.