Getting Ajax to update webpage

I have been working on this web server issue for a week and cant get ajax to work. the XML is ajax inputs. when i have the "if string contains" for ajax in the loop after "HTTP/1.1 200 OK" is freezes. if i put it at the end of the loop it works but never updates. Not sure if i have the set up proper.

and advice will help.

ARDUINO

#include "HX711.h"
#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
#define PIN_ETH_SPI 10
#define REQ_BUF_SZ 60
#define DOUT 3
#define CLK 2
float calibration_factor = 80; //-7050 worked for my 440lb max scale setup
float units;
float pounds;
float ounces;
// MAC address from Ethernet shield sticker under board
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 0, 50); // IP address, may need to change depending on network
EthernetServer server(80); // create a server at port 80
File webFile; // the web page file on the SD card
char HTTP_req[REQ_BUF_SZ] = {0}; // buffered HTTP request stored as null terminated string
char req_index = 0; // index into HTTP_req buffer
boolean LED_state[4] = {0}; // stores the states of the LEDs
HX711 scale(DOUT, CLK);// LEDs

#define FILE_NAME_LEN 20
// HTTP request type
#define HTTP_invalid 0
#define HTTP_GET 1
#define HTTP_POST 2
#define FT_HTML 0
#define FT_ICON 1
#define FT_CSS 2
#define FT_JAVASCRIPT 3
#define FT_XML 4
#define FT_PNG 5
#define FT_GIF 6
#define FT_TEXT 7
#define FT_INVALID 8

void setup()
{
pinMode(PIN_ETH_SPI, OUTPUT);
digitalWrite(PIN_ETH_SPI, HIGH);

digitalWrite(7, HIGH);
// disable Ethernet chip
pinMode(10, OUTPUT);
digitalWrite(10, HIGH);

Serial.begin(115200); // for debugging

// initialize SD card
Serial.println("Initializing SD card...");
if (!SD.begin(4)) {
Serial.println("ERROR - SD card initialization failed!");
return; // init failed
}
Serial.println("SUCCESS - SD card initialized.");
// check for index.htm file
if (!SD.exists("index.htm")) {
Serial.println("ERROR - Can't find index.htm file!");
return; // can't find index file
}
Serial.println("SUCCESS - Found index.htm file.");
// switches on pins 2, 3 and 5
pinMode(3, INPUT);
pinMode(2, OUTPUT);
scale.set_scale();
scale.tare(); //Reset the scale to 0
Ethernet.begin(mac, ip); // initialize Ethernet device
server.begin(); // start to listen for clients
}

void loop() {

EthernetClient client = server.available();
if (client) {
while (client.connected()) {
if (ServiceClient(&client)) {

break;
}
}
delay(1);
client.stop();
}
}

bool ServiceClient(EthernetClient *client)
{
static boolean currentLineIsBlank = true;
char cl_char;
File webFile;

char file_name[FILE_NAME_LEN + 1] = {0};
char http_req_type = 0;
char req_file_type = FT_INVALID;
const char *file_types[] = {"text/html", "image/x-icon", "text/css", "application/javascript", "text/xml", "image/png", "image/gif", "text/plain"};

static char req_line_1[40] = {0};
static unsigned char req_line_index = 0;
static bool got_line_1 = false;

if (client->available()) {
cl_char = client->read();

if ((req_line_index < 39) && (got_line_1 == false)) {
if ((cl_char != '\r') && (cl_char != '\n')) {
req_line_1[req_line_index] = cl_char;
req_line_index++;
}
else {
got_line_1 = true;
req_line_1[39] = 0;
}
}

if ((cl_char == '\n') && currentLineIsBlank) {

http_req_type = GetRequestedHttpResource(req_line_1, file_name, &req_file_type);
if (http_req_type == HTTP_GET) {
if (req_file_type < FT_INVALID) {
webFile = SD.open(file_name);
if (webFile) {

client->println(F("HTTP/1.1 200 OK"));

client->print(F("Content-Type: "));
client->println(file_types[req_file_type]);
client->println(F("Connection: close"));
client->println();

while(webFile.available()) {
int num_bytes_read;
char byte_buffer[64];

num_bytes_read = webFile.read(byte_buffer, 64);

client->write(byte_buffer, num_bytes_read);
}
webFile.close();
}
else {

}
}
else {

}
}
else if (http_req_type == HTTP_POST) {

}
else {

}
req_line_1[0] = 0;
req_line_index = 0;
got_line_1 = false;

return 1;
}
if (cl_char == '\n') {
currentLineIsBlank = true;
}
else if (cl_char != '\r') {
currentLineIsBlank = false;
}
}
return 0;
if (StrContains(HTTP_req, "ajax_inputs")) {
// send rest of HTTP header
client->println("Content-Type: text/xml");
client->println("Connection: keep-alive");
client->println();

XML_response(client);
}
}

char GetRequestedHttpResource(char *req_line, char *file_name, char *file_type)
{
char request_type = HTTP_invalid;
char *str_token;

*file_type = FT_INVALID;

str_token = strtok(req_line, " ");
if (strcmp(str_token, "GET") == 0) {
request_type = HTTP_GET;
str_token = strtok(NULL, " ");
if (strcmp(str_token, "/") == 0) {
strcpy(file_name, "index.htm");
*file_type = FT_HTML;
}
else if (strlen(str_token) <= FILE_NAME_LEN) {

strcpy(file_name, str_token);

str_token = strtok(str_token, ".");
str_token = strtok(NULL, ".");

if (strcmp(str_token, "htm") == 0) {*file_type = 0;}
else if (strcmp(str_token, "ico") == 0) {*file_type = 1;}
else if (strcmp(str_token, "css") == 0) {*file_type = 2;}
else if (strcmp(str_token, "js") == 0) {*file_type = 3;}
else if (strcmp(str_token, "xml") == 0) {*file_type = 4;}
else if (strcmp(str_token, "png") == 0) {*file_type = 5;}
else if (strcmp(str_token, "gif") == 0) {*file_type = 6;}
else if (strcmp(str_token, "txt") == 0) {*file_type = 7;}
else {*file_type = 8;}
}
else {

}
}
else if (strcmp(str_token, "POST") == 0) {
request_type = HTTP_POST;
}

return request_type;
}
void XML_response(EthernetClient cl)
{
int dogwater;

int pin_val2;
int pin_val3;
cl.print("<?xml version = \"1.0\" ?>");
cl.print("");
cl.print("");
if (digitalRead(7)) {
cl.print("ON");
}
else {
cl.print("OFF");
}
cl.print("");

pin_val2 = dogwater;
dogwater = random(2,99);
Serial.print(dogwater);
cl.print("");
cl.print(pin_val2);
cl.print("");
cl.print("");

pin_val3 = pounds;
cl.print("");
cl.print(pin_val3);
cl.print("");
cl.print("");

}

void StrClear(char *str, char length)
{
for (int i = 0; i < length; i++) {
str = 0;

  • }*
    }
    char StrContains(char *str, char *sfind)
    {

  • char found = 0;*

  • char index = 0;*

  • char len;*

  • len = strlen(str);*

  • if (strlen(sfind) > len) {*

  • return 0;*

  • }*

  • while (index < len) {*

  • if (str[index] == sfind[found]) {*

  • found++;*

  • if (strlen(sfind) == found) {*

  • return 1;*

  • }*

  • }*

  • else {*

  • found = 0;*

  • }*

  • index++;*

  • }*

  • return 0;*
    }

Arduino SD Card Web Page using Ajax with XML body { background-color: #939ec6; font-family: Arial; font-size: 18px; margin: 0; } a { text-decoration: none; } h2 { clear: left; color: #ffffff; font-size: 60px; font-weight: normal; margin: 0 0 -24px 20px; position: relative; z-index: 1; } ul { margin: 100; padding: 0; position: relative; z-index: 10; } li { display: block; float: left; height: 300px; list-style: none; margin: 10px; width: 400px; } li, li p a { background-color: #333333; background: -moz-linear-gradient(top, #4d4d4d 0%, #333333 100%); background: -webkit-linear-gradient(top, #4d4d4d 0%, #333333 100%); background: linear-gradient(top, #4d4d4d 0%, #333333 100%); border-radius: 5px; -webkit-box-shadow: inset 1px 1px 0px 0px rgba(255, 255, 255, 0.5), 1px 1px 0px 0px rgba(0, 0, 0, 0.5); box-shadow: inset 1px 1px 0px 0px rgba(255, 255, 255, 0.5), 1px 1px 0px 0px rgba(0, 0, 0, 0.5); } li span { border-radius: 5px 5px 5px 5px; -webkit-box-shadow: inset 1px 1px 0px 0px rgba(255, 255, 255, 0.5); box-shadow: inset 1px 1px 0px 0px rgba(255, 255, 255, 0.5); color: #ffffff; display: block; height: 23px; padding: 5px 10px; } li span, li p a.aan { background-color: #004080; background: -moz-linear-gradient(top, #0066cc 0%, #004080 100%); background: -webkit-linear-gradient(top, #0066cc 0%, #004080 100%); background: linear-gradient(top, #0066cc 0%, #004080 100%); } .automation li span, .automation li p a.aan { background-color: #1a1a1a; background: -moz-linear-gradient(top, #333333 0%, #1a1a1a 100%); background: -webkit-linear-gradient(top, #333333 0%, #1a1a1a 100%); background: linear-gradient(top, #333333 0%, #1a1a1a 100%); } li p a { color: #fffffF; display: inline-block; margin-bottom: 20px; padding: 45px; text-decoration: none; text-align: center; width: 100px; } li p a.small { width: 30px; } li p a.small:nth-child(2n) { margin-left: 10px; } li p a:hover, li p a.aan { -webkit-box-shadow: 1px 1px 0px 0px rgba(255, 255, 255, 0.5), inset 1px 1px 0px 0px rgba(0, 0, 0, 0.5); box-shadow: 1px 1px 0px 0px rgba(255, 255, 255, 0.5), inset 1px 1px 0px 0px rgba(0, 0, 0, 0.5); } li p { color: #ffffff; margin: 5px; text-align: right; } li p sup { font-size: 11px; line-height: 5px; } li p + p { margin-top: -5px; } li p strong { name: analogA0; id: analog0; value: 0; display: block; font-size: 100px; margin-top: 50px; } li p strong2 { name: analogA1; id: analog1; value: 50; display: block; font-size: 100px; margin-top: 50px; } li p strong sup { font-size: 10px; line-height: 15px; } li div { .bowl, .catfood, .dog, .dogfood, .petfood, .server, .sun display: inline-block; background: url('png.png') no-repeat; overflow: hidden; text-indent: -9999px; text-align: left; float: left; height: 128px; margin: 17px -20px 17px 10px; width: 128px; } .bowl { background-position: -0px -0px; width: 128px; height: 128px; } .catfood { background-position: -128px -0px; width: 128px; height: 128px; } .dog { background-position: -0px -128px; width: 128px; height: 128px; } .dogfood { background-position: -128px -128px; width: 128px; height: 128px; } .petfood { background-position: -0px -256px; width: 128px; height: 128px; } .plex { background-position: -128px -256px; width: 128px; height: 128px; } .sun { background-position: -0px -384px; width: 128px; height: 128px; }

    WATER AND FOOD

  • Dog Water

    . %

  • Dog Food

    . %

  • Plex

    ON OFF