ajax调用php无法正常工作

时间:2021-02-28 01:25:49

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?

我正在对php函数进行ajax调用...我正在尝试使用php脚本在时间输入字段上显示服务器时间...我正在按照教程我发现每个单词的在线单词但由于某种原因我一直在时间输入字段中接收来自php文件的实际文本...有人可以告诉我为什么会发生这种情况吗?

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:

<?php
  echo date("H:i:s");

?>

3 个解决方案

#1


2  

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:

我将假设你使用Apache作为你的web服务器。您应确保在配置中具有类似于以下内容的内容:

AddType application/x-httpd-php .php

It looks like your files are being served as plain text instead of being handed off to PHP.

看起来您的文件作为纯文本提供,而不是传递给PHP。

#2


1  

Check the following:

检查以下内容:

  1. The php code (echo date("H:i:s");) should be in a separate file from the ajax/html code (technically there are ways to get around this, but they make everything much more complicated)
  2. php代码(echo date(“H:i:s”);)应该与ajax / html代码位于一个单独的文件中(技术上有办法解决这个问题,但它们会使一切变得复杂得多)

  3. The php file should have the .php extension
  4. php文件应该有.php扩展名

  5. Your server should be configured to treat the .php file extension as php code (see Ek0nomik's answer)
  6. 您的服务器应配置为将.php文件扩展名视为php代码(请参阅Ek0nomik的回答)

  7. The php code should have <?php and ?> tags around it
  8. php代码应该有 标签

  9. Your server should have php installed
  10. 你的服务器应该安装php

If all this fails, go to the php file directly in your browser and see what comes up.

如果这一切都失败了,直接在浏览器中转到php文件,看看会出现什么。

#3


0  

I'm going to play Carnac and say it's because you don't have <?PHP at the beginning of your file and ?> at the end of your file.

我要玩Carnac并说这是因为你的文件开头没有 。

#1


2  

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:

我将假设你使用Apache作为你的web服务器。您应确保在配置中具有类似于以下内容的内容:

AddType application/x-httpd-php .php

It looks like your files are being served as plain text instead of being handed off to PHP.

看起来您的文件作为纯文本提供,而不是传递给PHP。

#2


1  

Check the following:

检查以下内容:

  1. The php code (echo date("H:i:s");) should be in a separate file from the ajax/html code (technically there are ways to get around this, but they make everything much more complicated)
  2. php代码(echo date(“H:i:s”);)应该与ajax / html代码位于一个单独的文件中(技术上有办法解决这个问题,但它们会使一切变得复杂得多)

  3. The php file should have the .php extension
  4. php文件应该有.php扩展名

  5. Your server should be configured to treat the .php file extension as php code (see Ek0nomik's answer)
  6. 您的服务器应配置为将.php文件扩展名视为php代码(请参阅Ek0nomik的回答)

  7. The php code should have <?php and ?> tags around it
  8. php代码应该有 标签

  9. Your server should have php installed
  10. 你的服务器应该安装php

If all this fails, go to the php file directly in your browser and see what comes up.

如果这一切都失败了,直接在浏览器中转到php文件,看看会出现什么。

#3


0  

I'm going to play Carnac and say it's because you don't have <?PHP at the beginning of your file and ?> at the end of your file.

我要玩Carnac并说这是因为你的文件开头没有 。