ajax和php不能一起工作

use*_*615 0 php ajax

这是整个HTML代码:

<title></title>
<script type="text/javascript">
function send()
    {
        var xmlhttp;
         if (window.XMLHttpRequest)
           {// code for IE7+, Firefox, Chrome, Opera, Safari
           xmlhttp=new XMLHttpRequest();
           }
         else
           {// code for IE6, IE5
           xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
           }

        xmlhttp.onreadystatechange=function()
           {
           if (xmlhttp.readyState==4 && xmlhttp.status==200)
             {
                alert(xmlhttp.responseText);
             }
           }

        xmlhttp.open("POST","add.php",true);
        xmlhttp.send('subject=' + document.getElementById("subject").value);
    }
</script>
</head>
<body>
<form name="main" method="post" action="add.php">
<div align="center">    
<table border="1" frame="hsides" rules="rows" cellpadding="5" cellspacing="0" width="50%" align="center">
    <tr>
        <td width="20%">
            subject: 
        </td>
        <td width="80%" align="right">
            <input type="text" size="80" maxlength="80" name="subject" id="subject" />
        </td>
    </tr>
</table>
<table border="0" width="50%" align="center">
    <tr>
        <td width="80%">
            <input type="button" value="send" onclick="send();" />
        </td>
    </tr>
</table>
</div>
</form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

这是add.php代码:

<?php
$subject = $_POST['subject'];
echo $subject;
?>
Run Code Online (Sandbox Code Playgroud)

我收到错误:在add.php中注意第2行的未定义索引.在警报中,我得到了什么..谁能告诉我这是什么问题?我对这个错误感到疯狂.

Dav*_*dom 6

尝试在xmlhttp.open()xmlhttp.send()调用之间添加此行:

xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
Run Code Online (Sandbox Code Playgroud)

PHP将不知道如何解码您发送的内容,除非您告诉它它是什么类型的内容.