1 Replies - 571 Views - Last Post: 26 November 2012 - 05:50 AM

#1 bmckinn8  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 57
  • Joined: 11-September 11

Trouble using Ajax to get PHP info

Posted 25 November 2012 - 04:50 PM

I am having some trouble retrieving information from a PHP file. I have an HTML document that using AJAX to get information from a PHP file. The HTML file produces an input box and button that says "Type CSS or XHTML". Depending on which one you type it should return the related values from the PHP file. However, it is only returning info when XHTML is typed, and giving a parsing error if CSS is typed. Any help would be greatly appreciated.

HTML code
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head> 
 <meta http-equiv="content-type" content="text/html;charset=utf-8">
 <title>jQuery For Ajax</title>
 <style type="text/css">
      body {text-align:center;}
      table {border:2px solid black;padding:2px; margin:2px;width:600px;}
      td{text-align:center;border:2px solid black;padding:2px; margin:2px}
      .odd{background-color:aqua;}
      #tests{width:600px;margin:auto;}
  </style>

 
 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
 <script type="text/javascript">
		function getTests(testparam){
				$.ajax({
          //uncomment the following line and add your own comment here explaining what this is doing
					url: "request.php",
					type: "POST",
					dataType: "json",
          data: ({
            //uncomment the following line and add your own comment that explains briefly to me what this is doing
					testname: testparam
					}),
					error: function(XMLHttpRequest, textStatus, errorThrown){
						    alert("Request failed: " + textStatus);
					},
					success: function(response){
        				var isIE = /msie/gi.test(navigator.userAgent);
        	      if (isIE == false){
        				  console.log(response);
        				}
        				parseAndOutputResponse(response);
					}
				});
		}
		
		function parseAndOutputResponse(tests){
        	if (tests == null){
        		document.getElementById('tests').innerHTML="No test results returned";
        	}
        	else{
  				var testsString = "";
  		    	for(x=0;x<tests.testname.length;x++){
  					
            		if (x%2 == 0){
            	  		testsString = testsString + "<tr class=\"even\"><td class=col1>" + tests.testname[x] + "</td><td class=col2> " + tests.datetaken[x] + "</td></tr>";
            		}else
            		{
            	  		testsString = testsString + "<tr class=\"odd\"><td class=col1>" + tests.testname[x] + "</td><td class=col2> " + tests.datetaken[x] + "</td></tr>";
            		}
            	}              	
        		document.getElementById('tests').innerHTML="<table><tr><td>Name</td><td>When Taken</td></tr>" + testsString + "</table>";
			}	
		}
 </script>
</head>
<body bgcolor="white" >
<input type="text" id='testname'>
<input type="button" value="Type in 'CSS' or 'XHTML' to get test results" onclick="getTests(document.getElementById('testname').value)">
<div id="tests"></div>
</body>
</html>



PHP Code
<?php
$id_testname = $_POST['testname'];

if ($id_testname=="xhtml")
{
echo "{\"proctoringenabled\":[0,0],
\"reviewfromthisip\":[1,1],
\"englishdictionary\":[\"\",\"\"],
\"resumable\":[0,0],
\"authorizedip\":[0,0],
\"timedtest\":[\"\",\"\"],
\"staffinstructions\":[\"\",\"\"],
\"studentenrolled\":[\"\",\"\"],
\"gradebookid\":[\"\",\"\"],\"id\":[56467,56467],
\"availablefrom\":[\"2012\/8\/2 0:0:0\",\"2012\/8\/2 0:0:0\"],
\"type\":[1,1],
\"typeid\":[1,1],\"instructions\":[\"\",\"\"],
\"testsites\":{\"testrownum\":[0,0,0,0,0,0,0,1,1,1,1,1,1,1],
\"siterownum\":[0,1,2,3,4,5,6,0,1,2,3,4,5,6]},
\"securityattemptsperid\":[20,20],
\"deliveryid\":[56347,56347],
\"calculatorscientific\":[\"\",\"\"],
\"reviewtimelimit\":[\"\",\"\"],
\"withinavailabledates\":[1,1],
\"reviewenabled\":[1,1],
\"calculatorcenterprovided\":[\"\",\"\"],
\"appointmentset\":[1,1],\"rownum\":[0,1],
\"calculatorgraphing\":[\"\",\"\"],
\"canviewscore\":[1,1],
\"calculatorfinancial\":[\"\",\"\"],
\"cuesheets\":[\"\",\"\"],
\"instructorfirstname\":[\"\",\"\"],
\"everywhereelseenabled\":[0,0],
\"writeonexam\":[\"\",\"\"],
\"datetaken\":[\"2012\/9\/13 11:29:21\",\"2012\/9\/14 10:17:20\"],
\"availableuntil\":[\"2012\/12\/8 23:59:0\",\"2012\/12\/8 23:59:0\"],
\"openbook\":[\"\",\"\"],
\"notes\":[\"\",\"\"],
\"resultid\":[5767638,5772799],
\"instructorlastname\":[\"\",\"\"],
\"instructorid\":[\"\",\"\"],
\"passwordset\":[0,0],
\"scratchpaper\":[\"\",\"\"],
\"testname\":[\"CS 2350 Fernandez XHTML\",\"CS 2350 Fernandez XHTML\"],
\"reviewonlyaftertest\":[0,0],
\"criteria\":\"cs 2350 fernandez xhtml\",
\"restroombreak\":[1,1],
\"calculator4function\":[\"\",\"\"],
\"paperforeigndict\":[\"\",\"\"],
\"electronicforeigndict\":[\"\",\"\"],
\"reviewwaitaftertest\":[0,0],
\"studentisinstructor\":[0,0],
\"sites\":{\"name\":[\"Nowhere\",\"WSU Site-Marriott Testing Ctr\",
\"WSU Testing - Davis\",\"WSU Testing - Morgan\",\"WSU Testing - Student Services\",
\"WSU Testing - Union\",\"WSU Testing - West\"],
\"siteid\":[117,108,127,124,126,125,123],
\"rownum\":[0,1,2,3,4,5,6],
\"abbrevname\":[\"Nowhere\",\"Marriott Health\",\"Davis Campus\",\"Morgan Campus\",
\"Student Services\",\"Union\",\"West\"]}}";
}
else if ($id_testname=="css")
{
echo 
"{\"proctoringenabled\":[0,0,0,0,0,0],
\"reviewfromthisip\":[1,1,1,1,1,1],
\"englishdictionary\":[\"\",\"\",\"\",\"\",\"\",\"\"],
\"resumable\":[0,0,0,0,0,0],
\"authorizedip\":[0,0,0,0,0,0],
\"timedtest\":[\"\",\"\",\"\",\"\",\"\",\"\"],
\"staffinstructions\":[\"\",\"\",\"\",\"\",\"\",\"\"],
\"studentenrolled\":[\"\",\"\",\"\",\"\",\"\",\"\"],
\"gradebookid\":[\"\",\"\",\"\",\"\",\"\",\"\"],
\"id\":[56627,56627,56627,56627,56627,56627],
\"availablefrom\":[\"2012\/8\/13 0:0:0\",\"2012\/8\/13 0:0:0\",\"2012\/8\/13 0:0:0\",\"2012\/8\/13 0:0:0\",\"2012\/8\/13 0:0:0\",\"2012\/8\/13 0:0:0\"],
\"type\":[1,1,1,1,1,1],\"typeid\":[1,1,1,1,1,1],
\"instructions\":[\"This is a formative assessment test (as opposed to a summative assessment test). 
It's designed to help you memorize basic things about CSS. To that end of helping you memorize my bet 
is that you are best off not consulting texts or the internet while taking the test. Instead, consult 
them afterward while you are reviewing your wrong answers. (Hence the original idea of having you take 
the tests in a testing center). However if you prefer to take this test in a different manner you 
are free \",\"This is a formative assessment test (as opposed to a summative assessment test). It's 
designed to help you memorize basic things about CSS. To that end of helping you memorize my bet is 
that you are best off not consulting texts or the internet while taking the test. Instead, consult 
them afterward while you are reviewing your wrong answers. (Hence the original idea of having you take 
the tests in a testing center). However if you prefer to take this test in a different manner you are 
free \",\"This is a formative assessment test (as opposed to a summative assessment test). It's 
designed to help you memorize basic things about CSS. To that end of helping you memorize my bet is 
that you are best off not consulting texts or the internet while taking the test. Instead, consult 
them afterward while you are reviewing your wrong answers. (Hence the original idea of having you 
take the tests in a testing center). However if you prefer to take this test in a different manner 
you are free \",\"This is a formative assessment test (as opposed to a summative assessment test). 
It's designed to help you memorize basic things about CSS. To that end of helping you memorize my 
bet is that you are best off not consulting texts or the internet while taking the test. Instead, 
consult them afterward while you are reviewing your wrong answers. (Hence the original idea of having 
you take the tests in a testing center). However if you prefer to take this test in a different 
manner you are free \",\"This is a formative assessment test (as opposed to a summative assessment 
test). It's designed to help you memorize basic things about CSS. To that end of helping you memorize 
my bet is that you are best off not consulting texts or the internet while taking the test. Instead, 
consult them afterward while you are reviewing your wrong answers. (Hence the original idea of having 
you take the tests in a testing center). However if you prefer to take this test in a different manner 
you are free \",\"This is a formative assessment test (as opposed to a summative assessment test). 
It's designed to help you memorize basic things about CSS. To that end of helping you memorize my 
bet is that you are best off not consulting texts or the internet while taking the test. Instead, 
consult them afterward while you are reviewing your wrong answers. (Hence the original idea of having 
you take the tests in a testing center). However if you prefer to take this test in a different manner 
you are free \"],
\"testsites\":{\"testrownum\":[0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5],
\"siterownum\":[0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9]},
\"securityattemptsperid\":[99,99,99,99,99,99],
\"deliveryid\":[56507,56507,56507,56507,56507,56507],
\"calculatorscientific\":[\"\",\"\",\"\",\"\",\"\",\"\"],
\"reviewtimelimit\":[\"\",\"\",\"\",\"\",\"\",
\"\"],\"withinavailabledates\":[1,1,1,1,1,1],
\"reviewenabled\":[1,1,1,1,1,1],
\"calculatorcenterprovided\":[\"\",\"\",\"\",\"\",\"\",\"\"],
\"appointmentset\":[1,1,1,1,1,1],
\"rownum\":[0,1,2,3,4,5],
\"calculatorgraphing\":[\"\",\"\",\"\",\"\",\"\",\"\"],
\"canviewscore\":[1,1,1,1,1,1],
\"calculatorfinancial\":[\"\",\"\",\"\",\"\",\"\",\"\"],
\"cuesheets\":[\"\",\"\",\"\",\"\",\"\",\"\"],
\"instructorfirstname\":[\"\",\"\",\"\",\"\",\"\",\"\"],
\"everywhereelseenabled\":[1,1,1,1,1,1],
\"writeonexam\":[\"\",\"\",\"\",\"\",\"\",\"\"],
\"datetaken\":[\"2012\/10\/3 10:25:52\",\"2012\/10\/3 14:48:1\",\"2012\/10\/7 14:0:59\",
\"2012\/10\/7 14:2:58\",\"2012\/10\/7 14:3:57\",\"2012\/10\/7 20:4:56\"],
\"availableuntil\":[\"2012\/12\/18 23:59:0\",\"2012\/12\/18 23:59:0\",\"2012\/12\/18 23:59:0\",
\"2012\/12\/18 23:59:0\",\"2012\/12\/18 23:59:0\",\"2012\/12\/18 23:59:0\"],
\"openbook\":[\"\",\"\",\"\",\"\",\"\",\"\"],
\"notes\":[\"\",\"\",\"\",\"\",\"\",\"\"],
\"resultid\":[5893218,5896034,5915392,5915398,5915404,5916065],
\"instructorlastname\":[\"\",\"\",\"\",\"\",\"\",\"\"],
\"instructorid\":[\"\",\"\",\"\",\"\",\"\",\"\"],
\"passwordset\":[0,0,0,0,0,0],
\"scratchpaper\":[\"\",\"\",\"\",\"\",\"\",\"\"],
\"testname\":[\"CS 2350 Fernandez CSS\",\"CS 2350 Fernandez CSS\",\"CS 2350 Fernandez CSS\",
\"CS 2350 Fernandez CSS\",\"CS 2350 Fernandez CSS\",\"CS 2350 Fernandez CSS\"],
\"reviewonlyaftertest\":[0,0,0,0,0,0],\"criteria\":\"css\",\"restroombreak\":[1,1,1,1,1,1],
\"calculator4function\":[\"\",\"\",\"\",\"\",\"\",\"\"],
\"paperforeigndict\":[\"\",\"\",\"\",\"\",\"\",\"\"],
\"electronicforeigndict\":[\"\",\"\",\"\",\"\",\"\",\"\"],
\"reviewwaitaftertest\":[0,0,0,0,0,0],
\"studentisinstructor\":[0,0,0,0,0,0],
\"sites\":{\"name\":[\"Everywhere Else\",\"Nowhere\",\"WSU Site-Marriott Testing Ctr\",
\"WSU Testing - Davis\",\"WSU Testing - Morgan\",\"WSU Testing - Natural Science\",
\"WSU Testing - Social Science\",\"WSU Testing - Student Services\",\"WSU Testing - Union\",
\"WSU Testing - West\"],\"siteid\":[112,117,108,127,124,131,128,126,125,123],
\"rownum\":[0,1,2,3,4,5,6,7,8,9],
\"abbrevname\":[\"Public\",\"Nowhere\",\"Marriott Health\",\"Davis Campus\",\"Morgan Campus\",
\"Natural Science\",\"Social Science\",\"Student Services\",\"Union\",\"West Campus\"]}}";


}


?>



Is This A Good Question/Topic? 0
  • +

Replies To: Trouble using Ajax to get PHP info

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,437
  • Joined: 23-August 08

Re: Trouble using Ajax to get PHP info

Posted 26 November 2012 - 05:50 AM

I believe it's the newline characters in your instructions field.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1