【发布时间】:2010-07-02 07:51:40
【问题描述】:
我正在尝试一个基本的 AJAX/php 测试。我有一个带有两个输入文本字段的表单,我在其中输入两个值和一个输出文本字段,当我按下按钮时,输入字段连接在一起并输出到第三个文本字段。我正在通过 AJAX/PHP 执行此操作。我将连接值输出到第三个字段,但似乎有一些附加文本附加到我从 PHP 返回的 responseText 上。附加的文本是带有 Web 服务器主机名和时间戳的 HTML 注释 (
PHP/HTML页面如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<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>
在URL中被OPEN调用(concat.php)调用的PHP如下:
<?php
$in1 = $_GET['inputText'];
$in2 = $_GET['inputText2'];
$returnvar = $in1 . ' - ' . $in2;
echo $returnvar;
?>
在 responseText 中传回的内容(如果我的两个输入字段包含“一”和“二”是:
ONE - TWO<!-- webserver1.thedomain.com compressed/chunked Thu Jul 1 15:42:08 PDT 2010 -->
responseText 后面的“”注释是什么意思?
【问题讨论】: