POST METHOD IN AJAX

POST METHOD IN AJAX

hi, this is my first time using AJAX, i try to use GET method and i have succeed but when i try to POST the data it failed. can you help me how to POST data form AJAX to PHP file. when i try to POST the validate function is not functioning, and i also cannot save the data to my server. this is my code.
JS code

function getXMLHttp()
{
  var xmlHttp
  try
  {
    //Firefox, Opera 8.0+, Safari
    xmlHttp = new XMLHttpRequest();
  }
  catch(e)
  {
    //Internet Explorer
    try
    {
      xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch(e)
    {
      try
      {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch(e)
      {
        alert("Your browser does not support AJAX!")
        return false;
      }
    }
  }
  return xmlHttp;
}
function checkR1(A1)
  {
  var Q1 = document.getElementById("R1A1").value=A1;
  }
function checkR2(A2)
  {
  var Q2 = document.getElementById("R2A2").value=A2;
  }
function checkR3(A3)
  {
  var Q3 = document.getElementById("R3A3").value=A3;
  }
 
function checkR4(A4)
  {
  var Q4 = document.getElementById("R4A4").value=A4;
  }
function checkR5(A5)
  {
  var Q5 = document.getElementById("R5A5").value=A5;
  }
function sum(P1)
  {
    var R1Q1 = parseInt(document.getElementById("R1A1").value);
    var R2Q2 = document.getElementById("R2A2").value*1;
    var R3Q3 = document.getElementById("R3A3").value*1;
    var R4Q4 = document.getElementById("R4A4").value*1;
    var R5Q5 = document.getElementById("R5A5").value*1;
    document.getElementById("R3P3").value= (((R1Q1+R2Q2+R3Q3+R4Q4+R5Q5)/25)*100).toFixed(2);
  }
function checkRadio (frmName, rbGroupName) {
  var radios = document[frmName].elements[rbGroupName];
 for (var i=0; i <radios.length; i++) {
  if (radios[i].checked) {
   return true;
  }
 }
 return false;
}
function MakeRequest()
{
for (var a=1; a <6; a++) {
         if (!checkRadio("lec_part3","R"+[a]))
         {
                alert("Please choose answer for the question No. "+[a]);
                return false;
          }
         
        }
  var xmlHttp = getXMLHttp();
  xmlHttp.onreadystatechange = function()
  {
    if(xmlHttp.readyState == 4)
    {
      HandleResponse(xmlHttp.responseText);
    }
  }
var lecnamevalue=encodeURIComponent(document.getElementById("lec_name").value)
var lecsubvalue=encodeURIComponent(document.getElementById("lec_sub").value)
var R3P3value=encodeURIComponent(document.getElementById("R3P3").value)
var parameters="lec_name="+lecnamevalue+"&lec_sub="+lecsubvalue+"&R3P3="+R3P3value
  xmlHttp.open("POST", "save_lec_p3.php", true);
 xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
 
  xmlHttp.send(parameters);
}
function HandleResponse(response)
{
  document.getElementById('ResponseDiv').innerHTML = response;
}

PHP Code

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title></title>
</head>

<body>
<?php

                        $lec_name=trim(addslashes(htmlspecialchars(rawurldecode($_POST["lec_name"]))));
                $lec_sub=trim(addslashes(htmlspecialchars(rawurldecode($_POST["lec_sub"]))));
                        $R3P3=trim(addslashes(htmlspecialchars(rawurldecode($_POST["R3P3"]))));
   
$con = mysql_connect("localhost","root","");
if (!$con)
{
 die('Could not connect: ' . mysql_error());
}

mysql_select_db("ie_lecsys_lecturer", $con);

mysql_query("UPDATE `lec_result` SET `lec_mrk_p3` = (`lec_mrk_p3` + '$R3P3' )/`lec_no_std` WHERE `lec_name` = '$lec_name'AND `lec_sub` = '$lec_sub' ");
mysql_close($con);
?>

</body>

</html>

Please help me. TQ

At
03/19/2011 - 05:37

This is for Validation Part

for (var a=1; a <6; a++) {
         if (!checkRadio("lec_part3","R"+[a]))
         {
                alert("Please choose answer for the question No. "+[a]);
                return false;
          }
         
        }

  var xmlHttp = getXMLHttp();
  xmlHttp.onreadystatechange = function()
  {
    if(xmlHttp.readyState == 4)
    {
      HandleResponse(xmlHttp.responseText);
    }
  }

var R3P3value=encodeURIComponent(document.getElementById("R3P3").value)
  xmlHttp.open("GET", "save_lec_p3.php?R3P3="+R3P3value, true);
 
 
  xmlHttp.send();
}

function HandleResponse(response)
{
  document.getElementById('ResponseDiv').innerHTML = response;
}

reply

Add Comment

Put code snippets inside language tags:
[language] [/language]

Examples:
[javascript] [/javascript]
[actionscript] [/actionscript]
[csharp] [/csharp]

See here for supported languages.

Javascript must be enabled to submit anonymous comments - or you can login.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.