jbodary Posted May 7, 2008 Share Posted May 7, 2008 Trying to do this for a homework assignment and I cant get it to work, instead of brining back the proper message it brings back all of them including some of the code? What am I doing wrong?!? <!--Startwith index.html--> <html> <head> <script type="text/javascript"> function fetchData(url,dataToSend,objectID){ var pageRequest = false; if (window.XMLHttpRequest) pageRequest = new XMLHttpRequest(); else if (window.ActiveXObject) pageRequest = new ActiveXObject("Microsoft.XMLHTTP"); else return false; pageRequest.onreadystatechange = function() { var object = document.getElementById(objectID); object.innerHTML = pageRequest.responseText; } if (dataToSend) { var sendData = 'sendData=' + dataToSend; pageRequest.open('POST',url,true); pageRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); pageRequest.send(sendData); } else { pageRequest.open('GET',url,true); pageRequest.send(null); }} </script> <body> <body background="lilo12b.jpg"> <h1>Which Lilo & Stitch character are you?</h1> <div id="control" onclick="fetchData('dataPage2.php',1,'message');">Lilo</div> <div id="control" onclick="fetchData('dataPage2.php',2,'message');">Stitch</div> <div id="control" onclick="fetchData('dataPage2.php',3,'message');">Jumba</div> <div id="control" onclick="fetchData('dataPage2.php',4,'message');">Pleakley</div> <p> </p> <div id="message"> </div> <br><br> <a href="index.html">Home</a> </body> </html> <?php if(isset($_POST["sendData"])) { $searchString = $_POST["sendData"]; if ($searchString == 1) $dataResults = sprintf("<h1>Hello Lilo!</h1>"); else if ($searchString == 2) $dataResults = sprintf("<h1>Hello Stitch!</h1>"); else if ($searchString == 3) $dataResults = sprintf("<h1>Hello Jumba!</h1>"); else if ($searchString == 4) $dataResults = sprintf("<h1>Hello Pleakley!</h1>"); else $dataResults = sprintf("<h1>Aloha, cousin! Welcome to Hawaii!</h1>"); } echo $dataResults; ?> Quote Link to comment https://forums.phpfreaks.com/topic/104635-fethcing-a-data-response/ Share on other sites More sharing options...
DarkWater Posted May 7, 2008 Share Posted May 7, 2008 elseif is one word, not two. =P Quote Link to comment https://forums.phpfreaks.com/topic/104635-fethcing-a-data-response/#findComment-535531 Share on other sites More sharing options...
jbodary Posted May 7, 2008 Author Share Posted May 7, 2008 Unfortunately it does the same thing no matter if I have it as one word or two :-) Quote Link to comment https://forums.phpfreaks.com/topic/104635-fethcing-a-data-response/#findComment-535533 Share on other sites More sharing options...
DarkWater Posted May 7, 2008 Share Posted May 7, 2008 print_r($_POST) at the top of that page and show us the output. Quote Link to comment https://forums.phpfreaks.com/topic/104635-fethcing-a-data-response/#findComment-535537 Share on other sites More sharing options...
wildteen88 Posted May 7, 2008 Share Posted May 7, 2008 From my testing its to do with how you have constructed your if/elseif/else statement. Use: if(isset($_POST["sendData"])) { $searchString = $_POST["sendData"]; if ($searchString == 1) $dataResults = "<h1>Hello Lilo!</h1>"; elseif ($searchString == 2) $dataResults = "<h1>Hello Stitch!</h1>"; elseif ($searchString == 3) $dataResults = "<h1>Hello Jumba!</h1>"; elseif ($searchString == 4) $dataResults = "<h1>Hello Pleakley!</h1>"; else $dataResults = "<h1>Aloha, cousin! Welcome to Hawaii!</h1>"; echo $dataResults; } No need for sprintf when defining strings. Quote Link to comment https://forums.phpfreaks.com/topic/104635-fethcing-a-data-response/#findComment-535540 Share on other sites More sharing options...
jbodary Posted May 7, 2008 Author Share Posted May 7, 2008 Comes up nothing actually, attaching the original files if that helps [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/104635-fethcing-a-data-response/#findComment-535541 Share on other sites More sharing options...
DarkWater Posted May 7, 2008 Share Posted May 7, 2008 It probably came up with nothing because you didn't access it through AJAX. Did you? Quote Link to comment https://forums.phpfreaks.com/topic/104635-fethcing-a-data-response/#findComment-535542 Share on other sites More sharing options...
rarebit Posted May 7, 2008 Share Posted May 7, 2008 elseif is one word, not two. =P It can be either! Quote Link to comment https://forums.phpfreaks.com/topic/104635-fethcing-a-data-response/#findComment-535543 Share on other sites More sharing options...
wildteen88 Posted May 7, 2008 Share Posted May 7, 2008 jbodary please read my post Quote Link to comment https://forums.phpfreaks.com/topic/104635-fethcing-a-data-response/#findComment-535544 Share on other sites More sharing options...
jbodary Posted May 7, 2008 Author Share Posted May 7, 2008 It probably came up with nothing because you didn't access it through AJAX. Did you? ah, no I didnt Quote Link to comment https://forums.phpfreaks.com/topic/104635-fethcing-a-data-response/#findComment-535545 Share on other sites More sharing options...
jbodary Posted May 7, 2008 Author Share Posted May 7, 2008 jbodary please read my post Nope, pasted that in instead and it still does the same thing Quote Link to comment https://forums.phpfreaks.com/topic/104635-fethcing-a-data-response/#findComment-535547 Share on other sites More sharing options...
DarkWater Posted May 7, 2008 Share Posted May 7, 2008 Would you please show me the output after an ajax call? Quote Link to comment https://forums.phpfreaks.com/topic/104635-fethcing-a-data-response/#findComment-535552 Share on other sites More sharing options...
jbodary Posted May 7, 2008 Author Share Posted May 7, 2008 I hate to admit this, but I have no clue how to :-) Newbie doesn't begin to cover it Quote Link to comment https://forums.phpfreaks.com/topic/104635-fethcing-a-data-response/#findComment-535562 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.