ajax和php赢得了';我不能一起工作


ajax and php won't work together

以下是整个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>

这是add.php代码:

<?php
$subject = $_POST['subject'];
echo $subject;
?>

我得到错误:注意到add.php中第2行的未定义索引。在警报中,这就是我得到的。。有人能告诉我出了什么问题吗?这个错误让我发疯了。

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

xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

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