Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi, I am trying to send data to a server using POST protocol with XMLHttpRequest, but the xhr object isn't hitting '3'. I know this is because of a code error because I keep getting a 500 error, but I don't know how to change the code to use POST protocol. I'm following an example, but it's not working for this code.

This:

JavaScript
var dataSource = "lecturers.php";
        var requestBody = "test";
        alert(requestBody);

        xhr.open("POST", dataSource, true);
        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xhr.onreadystatechange = getData;
        xhr.send(requestBody);


Was originally this:

JavaScript
xhr.open("GET", "lecturers.php?id=" + Number(new Date), true);
 xhr.onreadystatechange = getData;
 xhr.send(null); 


lecturers.htm

HTML
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <title>Course Lecturers</title>
        <script type="text/javascript" src="lecturers.js"></script>
    </head>
    <body>
        <form>
            <input type = "button" id="butt1" value = "Press for Results" /><br />
        </form>
        <script type="text/javascript">
            var btnResults = document.getElementById("butt1");
            btnResults.onclick = getResults;
        </script>
        <span id="results" /></span>
    </body>
</html>


lecturers.js

<pre lang="Javascript">var xhr = false;

if(window.XMLHttpRequest){
    xhr = new XMLHttpRequest();
} else if(window.ActiveXObject){
    xhr = new ActiveXObject("Microsoft.XMLHTTP");
}


function getResults(){
    if(xhr){
        var dataSource = "lecturers.php";
        var requestBody = "test";
        alert(requestBody);

        xhr.open("POST", dataSource, true);
        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xhr.onreadystatechange = getData;
        xhr.send(requestBody);
    }
}
function getData ()
{
    alert(xhr.readyState);

    if ((xhr.readyState == 4) &&(xhr.status == 200))
    {

        var object = document.getElementById("results");
        object.innerHTML = xhr.responseText;

    }
}


lecturers.xml

<pre lang="xml"><?xml version="1.0" encoding="iso-8859-1"?>
<Courses>
    <Course>
        <title>Web Development</title>
        <lecturer>Jian Yu</lecturer>
        <moderator>Gary Snail</moderator>
        <points>25</points>
        <group>SCCRL</group>
        <Faculty>DCT</Faculty>
    </Course>

    <Course>
        <title>Software Development in Java</title>
        <lecturer>SpongeBob SquarePants</lecturer>
        <moderator>Patrick Star</moderator>
        <points>25</points>
        <group>SCCRL</group>
        <Faculty>DCT</Faculty>
    </Course>

    <Course>
        <title>Financial Accounting</title>
        <lecturer>Squidward Tentacles</lecturer>
        <moderator>Mr Crab</moderator>
        <points>10</points>
        <group>KEDRI</group>
        <Faculty>DCT</Faculty>

        <Course>
            <title>IS Project</title>
            <lecturer>Plankton</lecturer>
            <moderator>Sandy Cheeks</moderator>
            <points>30</points>
            <group>IS</group>
            <Faculty>DCT</Faculty>
        </Course>
    </Course>
</Courses>


lecturers.xsl

<pre lang="HTML"><?xml version="1.0"?><!-- DWXMLSource="results.xml" -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html"/>
    <xsl:template match="/">
        <HTML>
            <HEAD>
                <TITLE> Courses</TITLE>
            </HEAD>
            <BODY>
                Courses with More than 15 Credit Points : <BR/>
                <table border="1">
                    <tr>
                        <th>Title</th>
                        <th>Group</th>
                        <th>Faculty</th>
                    </tr>

                    <xsl:for-each select="Courses">
                        <tr>
                            <td><xsl:value-of select="title"></xsl:value-of></td>
                            <td><xsl:value-of select="group"></xsl:value-of></td>
                            <td><xsl:value-of select="Faculty"></xsl:value-of></td>
                        </tr>
                    </xsl:for-each>
                </table>


                ----------
                <BR/>
                Number of Courses Offered by CSSE :
            </BODY>
        </HTML>
    </xsl:template>
</xsl:stylesheet>



lecturers.php

PHP
<?php
$xmlDoc = new DomDocument;
$xmlDoc->load("lecturers.xml");
$xslDoc = new DomDocument;
$xslDoc->load("lecturers.xsl");
$proc = new XSLTProcessor;
$proc->importStyleSheet($xslDoc);
echo "test";
echo $proc->transformToXML($xmlDoc);
?>


What I have tried:

I have tried changing things around, changing the values of variables etc.
Posted
Updated 25-May-16 14:00pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900