使用POST和PHP返回纯文本的AJAX-apache配置?
我在用Ajax使用POST,然后用PHP处理时遇到了问题 我目前正在研究此示例,并对其进行调整以满足我的需要: 我准确地复制了2个示例文件,当我提交表单时,浏览器只显示PHP文件的文本内容 页面底部的演示运行正常,所以我想我的服务器配置一定有问题吧?(带有PHP5.3.16的Apache 2.2.22) 标准Ajax请求正常工作,这是一个正常的PHP Post表单,问题只出现在使用Ajax进行Post时 任何帮助都会很棒 如果不想单击该链接: test_form.php使用POST和PHP返回纯文本的AJAX-apache配置?,php,ajax,forms,apache,post,Php,Ajax,Forms,Apache,Post,我在用Ajax使用POST,然后用PHP处理时遇到了问题 我目前正在研究此示例,并对其进行调整以满足我的需要: 我准确地复制了2个示例文件,当我提交表单时,浏览器只显示PHP文件的文本内容 页面底部的演示运行正常,所以我想我的服务器配置一定有问题吧?(带有PHP5.3.16的Apache 2.2.22) 标准Ajax请求正常工作,这是一个正常的PHP Post表单,问题只出现在使用Ajax进行Post时 任何帮助都会很棒 如果不想单击该链接: test_form.php <?php //
<?php
// if data are received via POST
if (isset($_POST['nume']) && isset($_POST['mesaj'])) {
// get data into variables, deleting the html tags
$nume = strip_tags($_POST['nume']);
$mesaj = strip_tags($_POST['mesaj']);
// if the form fields are completed
if (strlen($nume)>0 && strlen($mesaj)>0) {
echo 'Welcome <b>'. $nume. '</b><br />The message you`ve sent: <pre>'. $mesaj. '</pre>';
}
else {
echo 'Fill in all form fields';
}
}
?>
ajax_form.html
示例Ajax和表单
此处将显示服务器响应。
您的姓名:
您的留言:
如果浏览器向您显示PHP源代码,那么它根本不会通过PHP处理请求。这可能是Apache配置错误(很可能是),也可能是PHP文件命名错误(Apache使用文件名确定文件何时应通过PHP模块)。如果文件命名正确,请检查Apache配置;您说有些PHP文件正在工作,所以可能是非工作PHP文件所在的目录或vhost没有配置为使用mod_PHP。我觉得很愚蠢。。。但我明白了。
只有从localhost打开html页面,post请求才会起作用。我正在使用文件路径打开它。您上面的代码对我来说很好,一定是您的服务器上的某个东西。这些是你实际使用的代码吗?
<!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" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-2" />
<title>Example Ajax and Form</title>
<script type="text/javascript"><!--
// create the XMLHttpRequest object, according browser
function get_XmlHttp() {
// create the variable that will contain the instance of the XMLHttpRequest object (initially with null value)
var xmlHttp = null;
if(window.XMLHttpRequest) { // for Forefox, IE7+, Opera, Safari, ...
xmlHttp = new XMLHttpRequest();
}
else if(window.ActiveXObject) { // for Internet Explorer 5 or 6
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
return xmlHttp;
}
// sends data to a php file, via POST, and displays the received answer
function ajaxrequest(php_file, tagID) {
var request = get_XmlHttp(); // calls the function for the XMLHttpRequest instance
// gets data from form fields, using their ID
var nume = document.getElementById('nume').value;
var mesaj = document.getElementById('mesaj').value;
// create pairs index=value with data that must be sent to server
var the_data = 'nume='+nume+'&mesaj='+mesaj;
request.open("POST", php_file, true); // sets the request
// adds a header to tell the PHP script to recognize the data as is sent via POST
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
request.send(the_data); // sends the request
// Check request status
// If the response is received completely, will be transferred to the HTML tag with tagID
request.onreadystatechange = function() {
if (request.readyState == 4) {
document.getElementById(tagID).innerHTML = request.responseText;
}
}
}
--></script>
</head>
<body>
<div id="resp">Here will be displayed the server response.</div><br />
<form action="test_form.php" method="post" name="form1" onsubmit="ajaxrequest('test_form.php', 'resp'); return false;">
Your name: <input type="text" name="nume" id="nume" size="20" maxlength="33" /><br />
Your message:<br />
<textarea name="mesaj" id="mesaj" cols="25" rows="4"></textarea><br />
<input type="submit" value="Send" />
</form>
</body>
</html>