Jump to content

fethcing a data response


jbodary

Recommended Posts

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>&nbsp</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;
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.