开发者

I can not convert my ajax from "GET" to "POST"

 <script language='javascript' type='text/javascri开发者_运维百科pt'>

function ajaxFunction(){
  var ajaxRequest;  

  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;
        }
    }
  }
  ajaxRequest.onreadystatechange = function(){
    if(ajaxRequest.readyState == 4){
        var ajaxDisplay = document.getElementById('pagecomments');
        ajaxDisplay.innerHTML = ajaxRequest.responseText;

    }
  }
  var message = document.getElementById('message').value;
  var wpm = document.getElementById('wpm').value;
  var queryString = '?message=' + message + '&wpm=' + wpm;
  request.open("POST", ajaxprofilechat.php, true);  
  http.send(null); 

  }

  </script>

Hi, I am trying to convert this script that I have from the ajax GET method to POST method, as I think that it will solve one of the problems that I am having on my website. However, I am very, very novice in ajax coding and although I have attempted to convert to POST, there is an error because this script is not working. Could someone who is good with ajax please help me fix this problem that I have? All I want this to do is to work using the POST method. Do I even need to use variable and query strings when using POST? Any help would be greatly appreciated. Thanks for your time!

Here is ajaxprofilechat

mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($dbname);
$age = $_POST['message'];
$wpm = $_POST['wpm'];
$age = mysql_real_escape_string($age); 
$wpm = mysql_real_escape_string($wpm);
$chatname6 = ($_SESSION['username']);
$message6 = $_GET['site_message'];
$month6 = date("F");
$dayofmonth6 = date("d");
$year6 = date("Y");
$date10 = "$month6 $dayofmonth6 $year6";
$hours6 = date("g");
  $min6 = date("i");
 $sec6 = date("s");
 $amorpm6 = date("A");
 $time6 = "$hours6:$min6 $amorpm6";

 if (strlen($age)>4)
 {
 mysql_query("INSERT INTO guestbook VALUES        ('','$wpm','$chatname6','$age','$date10','$time6')");
echo "&nbsp;<h80><b>Comment Posted</b></h80><p><p>";
}
else
 {
 echo "&nbsp;<h80><b>Your comment must be greater than four characters</b></h80><p>";
 }
 ?> 


Add quotes around the url:

request.open("POST", 'ajaxprofilechat.php', true);  

Without them, javascript is looking for the variable ajaxprofilechat with property php.


as Gus said correct the ajaxprofilechat.php .

ajaxRequest.open("POST", 'ajaxprofilechat.php', true);

plz you note you are using different ajax objects each time , you have to stick with the object ajaxRequest .

then make sure you send your params this way :

var params= 'message=' + message + '&wpm=' + wpm;
  ajaxRequest.open("POST", 'ajaxprofilechat.php', true);  
  ajaxRequest.send(params);

also add this line before the send

ajaxRequest.setRequestHeader('Content-Type','application/x-www-form-urlencoded');

also try to make things simple to make sure everything is working use hard coded values in the first time :

var message = 'test';
  var wpm = 'test';

also use chrome developper tools or firebug to see errors details , hope this helps

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜