Forum Moderators: open
<P> element's innerHTML without reloading the page.
// Form
<form>
<textarea id="newContent" rows="4" cols="40"></textarea><br /><br />
<input type="button" value="Change Content" onClick="changeContent()" />
</form>
// P element where contents from PHP are displayed
Show/Load Content:
<p id="showContent" class="spacedText"></p>
// Create the Request Object function
function createXMLObject()
{
if (window.XMLHttpRequest)
{
XHRO=new window.XMLHttpRequest();
}
else
{
XHRO=new ActiveXObject("Microsoft.XMLHTTP");
}
textDoc=XHRO;
}
// Push PHP result to P element
function showNewContent()
{
document.getElementById("showContent").innerHTML=textDoc;
}
// This is the function called in the form when clicked
function changeContent()
{
runPHP("changeContent.php");
}
// This calls the PHP
function runPHP(dname)
{
createXMLObject();
changeContent=document.getElementById("newContent").value;
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)
{
textDoc=XHRO.responseText;
showNewContent();
}
}
XHRO.send(parameters);
}
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
fclose($handle);