AJAX调用到PHP工作不正常 [英] ajax call to php not working properly
问题描述
我正在做一个AJAX调用PHP函数...我想显示在使用PHP脚本时输入域服务器的时间...我下面的教程我在网上找到字每个字,但由于某种原因,我不断收到及时输入字段从PHP文件实际的文字...有人可以告诉我为什么会这样?
这是我的code
<!DOCTYPE HTML PUBLIC - // W3C // DTD XHTML 1.0过渡// ENhttp://www.w3.org/TR/xhtml1/DTD/ XHTML1-transitional.dtd>
< HTML>
< HEAD>
<冠军>服务器时间例子< /标题>
< /头>
<身体GT;
< SCRIPT LANGUAGE =JavaScript的类型=文/ JavaScript的>
<! -
//浏览器支持code
功能ajaxFunction(){
VAR ajaxRequest; //让Ajax成为可能的变量!
尝试 {
//歌剧8.0+,火狐,Safari浏览器
ajaxRequest =新XMLHtt prequest();
}赶上(五){
// Internet Explorer浏览器
尝试 {
ajaxRequest =新的ActiveXObject(MSXML2.XMLHTTP);
}赶上(五){
尝试 {
ajaxRequest =新的ActiveXObject(Microsoft.XMLHTTP);
}赶上(五){
// 出了些问题
警报(您的浏览器打破了!);
返回false;
}
}
}
//创建将接收从服务器发送数据的功能
ajaxRequest.onreadystatechange =功能(){
如果(ajaxRequest.readyState == 4){
document.myForm.time.value = ajaxRequest.responseText;
}
}
ajaxRequest.open(GET,serverTime.php,真正的);
ajaxRequest.send(空);
}
// - >
< / SCRIPT>
<表格名称=myForm的'>
名称:<输入类型='文本'的onChange =ajaxFunction(); NAME ='用户名'/> < BR />
时间:<输入类型=文本名称=时间/>
< /形式GT;
< /身体GT;
< / HTML>
下面是PHP的:
< PHP
回声日期(H:我:秒);
?>
我将让你在使用Apache作为Web服务器的假设。您应该确保您有类似下面的东西在你的配置:
将AddType应用/ X的httpd - PHP的.php
它看起来像你的文件正在送达被移交给PHP作为纯文本代替。
i'm making an ajax call to a php function... i'm trying to display the server time on the time input field using a php script... i was following a tutorial i found online word per word but for some reason i keep receiving the actual text from the php file in time input field... can someone please tell me why this is happening?
here's my code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Server Time Example</title>
</head>
<body>
<script language="javascript" type="text/javascript">
<!--
//Browser Support Code
function ajaxFunction() {
var ajaxRequest; // The variable that makes Ajax possible!
try {
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e) {
// Internet Explorer Browsers
try {
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function () {
if (ajaxRequest.readyState == 4) {
document.myForm.time.value = ajaxRequest.responseText;
}
}
ajaxRequest.open("GET", "serverTime.php", true);
ajaxRequest.send(null);
}
//-->
</script>
<form name='myForm'>
Name: <input type='text' onChange="ajaxFunction();" name='username' /> <br />
Time: <input type='text' name='time' />
</form>
</body>
</html>
Here's the PHP:
<?php
echo date("H:i:s");
?>
I'm going to make the assumption you are using Apache as your web server. You should ensure you have something similar to the following in your configuration:
AddType application/x-httpd-php .php
It looks like your files are being served as plain text instead of being handed off to PHP.
这篇关于AJAX调用到PHP工作不正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!