2013-03-13 75 views
0

我在使用POST和Ajax時遇到問題,然後使用PHP進行處理。用POST和PHP返回純文本-apache配置的AJAX?

我目前通過這個例子的工作和適應它適合我的需要: http://coursesweb.net/ajax/ajax-post-php

我複製了2例文件完全一致,當我提交表單,瀏覽器只顯示文本內容PHP文件。

在該頁面底部的演示工作正常,所以我假設我的服務器配置必須有問題嗎? (使用PHP 5.3.16的Apache 2.2.22)

標準Ajax請求正常工作,就像一個普通的PHP Post表單一樣,問題只有在使用Ajax發佈時。

任何幫助將會很棒!

如果你不想點擊鏈接: test_form.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

<!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> 
+0

告訴我們你的代碼 – Amir 2013-03-13 20:42:11

+0

上面的代碼對我來說工作正常,必須是你的服務器上的東西。那些是你正在使用的實際代碼? – 2013-03-13 20:51:31

回答

0

如果瀏覽器正顯示出你的PHP源代碼,那麼它不處理請求通過PHP根本。這可能是Apache錯誤配置(很可能),或者PHP文件錯誤命名(Apache使用文件名來確定它何時應該通過PHP模塊。)如果文件命名正確,請檢查Apache配置;你說某些PHP文件正在工作,所以可能是非工作PHP文件所在的目錄或虛擬主機未配置爲使用mod_php。

0

我覺得很愚蠢...但我明白了。 只有從本地打開html頁面,發佈請求才會有效。我正在使用文件路徑打開它。

+0

是的,這樣做:) – Adrian 2013-03-13 21:26:15