tags:

views:

20

answers:

2

I am trying out a basic AJAX/php test. I have a form with two input text fields where I type in two values and an output text field where, when I push a button, the input fields are concatenated together and output to the third text field. I am doing this via AJAX/PHP. I get the concatenated value output to the third field, but there seems to be some additional text appended on to the responseText that I get back from the PHP. The text appended is an HTML comment (<--) with the hostname of the web server and a timestamp. How do I get rid of this? Is this normal for responseText to come back with this additional information in the responseText string?

The PHP/HTML page is as follows:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>AJAX/PHP Test</title>

</head>
<body>

<script language="javascript" type="text/javascript">
// Get the HTTP Object
function getHTTPObject(){
   if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");
   else if (window.XMLHttpRequest) return new XMLHttpRequest();
   else {
      alert("Your browser does not support AJAX.");
      return null;
   }
}

// Change the value of the output field
function setOutput() {
  var val;
  val="";
  if (httpObject.readyState == 4) {
    val=httpObject.responseText;
     if ( val != undefined ) {
       document.getElementById('outputFld').value = val;
     }
  }
}

// Implement business logic
function doWork(){
   var url;
   httpObject = getHTTPObject();
   if (httpObject != null) {
      httpObject.onreadystatechange = setOutput;
      url="concat.php?inputText="+document.getElementById('inputFld1').value+"&inputText2="+document.getElementById('inputFld2').value;
      httpObject.open("GET", url, true);
      httpObject.send(null);
   }
}

var httpObject = null;

</script>
This is a test page to see how to get ajax and php to work together when submitting a form with data.

<P>

First we have a simple form.  The php will be called when the button is pressed and will concatenate
"Input 1" and "Input 2" and write the output to the "Output" field. <P><P>


</body>

<form>
   Input 1: <input type="text" id="inputFld1"  size="50" /><br>
   Input 2: <input type="text" id="inputFld2"  size="50" /><br>
<HR>
   Output: <input type="text" id="outputFld"  size="100" /><br>
<P>
   <input type="button" name="submitButton" value="Concatenate" onClick="doWork()" />


</html>

and the PHP being called in the URL with the OPEN call (concat.php) is as follows:

<?php
  $in1 = $_GET['inputText'];
  $in2 = $_GET['inputText2'];
  $returnvar = $in1 . ' - ' . $in2;
  echo $returnvar;
?>

What gets passed back in the responseText (if my two input fields contained say "ONE" and TWO" is:

ONE - TWO<!-- webserver1.thedomain.com compressed/chunked Thu Jul  1 15:42:08 PDT 2010 -->

What's with the "" comment appended on to the responseText?

+2  A: 

Is it possible that in some configuration (PHP, apache...) a setting in the range of "auto append file" is active? Or maybe the webserver or some extension you are using is causing the string to be appended?

I did some quick googling but cannot find any real reference to the string. The most references I got were pointing to some responses people got from different Yahoo services.

Killer_X
I think you may be right. We probably have special settings for PHP or apache here.
GregH
As to your question "How do I get rid of it?":If you are just adding the thing to the page as html, its commented anyway, else some JS "comment removing" regex magic might help:http://www.w3schools.com/jsref/jsref_obj_regexp.asp
Killer_X
A: 

I have just ran this and it worked as I expected, without the date and domain.

Why no try using AJAX support from something like jQuery, Moo Tools or Prototype?

jakenoble