0

Trying to get data from php server. Data shows file itself. no errors are shown in console.

function process() {
 if (xmlHttp.readyState == 0 || xmlHttp.readyState == 4) { // 0 and 4 if object is ready to go and not busy
    food = encodeURIComponent(document.getElementById("userInput").value); //document is web page
    //        to communicate with server
    xmlHttp.open("GET", "foodstore.php?food=" + food, true); //create we wana sent to server, request type should be same, uri is second parameter, true is request to be handled asynchrously
    xmlHttp.onreadystatechange = handleServerResponse; //3.handle that request,like update a page or something
    xmlHttp.send(); //if GET then null
 } else {
    setTimeout(process, 1000);
    //if busy timeout then communicate again
 }
}

foodstore.php

using array for foodItems and checking if alphabets are typed in input box.

<?php
header('Content-Type: text/xml');
echo '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>'; 
//response stored in food 
echo '<response>'; 
    $food = $_GET['food'];
    $foodArray = array('tuna','bacon','beef','loaf','ham');
if(in_array($food, $foodArray)) 
        echo 'We do have' .$food.'!' 
    elseif($food=='') 
        echo 'Enter a food ass';
else 
        echo 'Sorry we dont sell no ' .$food.'!'; 
echo '</response>'; 
?>
7urkm3n
  • 6,054
  • 4
  • 29
  • 46

1 Answers1

0

If the text is being displayed and it's the same as the code you have listed then it means that server that's running it is not seeing it as a php script ie it thinks it's plain text or something else. Have a look here for possible reasons...

Apache shows php code instead of executing

Community
  • 1
  • 1
Harry
  • 11,298
  • 1
  • 29
  • 43