Welcome to WebmasterWorld Guest from

Forum Moderators: open

Message Too Old, No Replies

JavaScript Problem



11:22 pm on Feb 15, 2010 (gmt 0)

5+ Year Member

Hi there! I have been getting into writing JavaScript and PHP, using AJAX techniques. I am running into a problem that I know has to be a simple error or overlook on my part, but I can not figure out what.

Basically I am making a very simple content update that will update a text file on the server, and echo the results to a
element's innerHTML without reloading the page.

Now, on the first click of the form, the technique works. What I can't figure out is, when I click the button again, I get "Message: Object doesn't support this property or method" and it points me directly to the function I am calling with onClick in the XHTML.

If anyone has any insight on this issue it would be very much appreciated!

I'll strip down the code as much as possible.

Here is the XHTML:

// Form
<textarea id="newContent" rows="4" cols="40"></textarea><br /><br />

<input type="button" value="Change Content" onClick="changeContent()" />

// P element where contents from PHP are displayed
Show/Load Content:
<p id="showContent" class="spacedText"></p>

Here is the JavaScript:

// Create the Request Object function
function createXMLObject()
if (window.XMLHttpRequest)
XHRO=new window.XMLHttpRequest();
XHRO=new ActiveXObject("Microsoft.XMLHTTP");

// Push PHP result to P element
function showNewContent()

// This is the function called in the form when clicked
function changeContent()

// This calls the PHP
function runPHP(dname)

var parameters=("content="+changeContent);
XHRO.open("POST", dname, true);
XHRO.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
XHRO.setRequestHeader("Content-length", parameters.length);
XHRO.setRequestHeader("Connection", "close");
XHRO.onreadystatechange = function() {
if(XHRO.readyState == 4 && XHRO.status == 200)

The PHP:

header("Content-Type: text/plain");

// Load file
$textDoc = "content.txt";

// Open file for writing
$handle = fopen($textDoc, "w") or die("Unable to Open File");

// Read the new data into a variable
$contents = $_POST["content"];

// Write the new contents to the file
fwrite($handle, $contents);

// Echo results(also verifies info was correctly received
echo $contents;

// Close the file to finalize changes


3:37 pm on Feb 16, 2010 (gmt 0)

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

Welcome to WebmasterWorld!

1. onClick is not valid XHTML. In XHTML, all attributes must be lowercase, so it should be onclick instead.

2. Your runPHP function redefines your global changeContent. This is why it's important to use "var" to declare your variables, otherwise they get created in the global scope (which is messy and inefficient). In your runPHP function, add "var" before changeContent=... and that should fix the problem you're seeing.

Side note: You might want to read "Why most of us should NOT use XHTML" [webmasterworld.com]


4:09 pm on Feb 16, 2010 (gmt 0)

5+ Year Member

Thank you so much for your help, I will give it a shot. Also, thanks for this site too. I have used it off and on for nearly 6 years or so now. It kicks butt!

As for the var, for some reason I had it backwards. I thought the var declaration would make it a global variable.

Thanks again.


4:24 pm on Feb 16, 2010 (gmt 0)

5+ Year Member

Yup that fixed it. I understand now, I used a variable named the same as the function name, so it was getting redefined as a global variable. So one run through was working, but after redefining the changeContent into a variable, causing the second click to attempt to run a variable as a function.

Thanks! I'll give the link you posted a read.

Featured Threads

Hot Threads This Week

Hot Threads This Month