PHP Paramaters lost

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
royalprotection
Forum Newbie
Posts: 1
Joined: Mon Aug 23, 2010 6:58 pm

PHP Paramaters lost

Post by royalprotection »

I am having some issues and I cannot figure out the problem. My parameters are being sent via an XMLHttpRequest() and I can view that they are sent using Firebug. When I try and print them out using a PHP script, they are always blank. Everything I have read about PHP says I am doing this correctly. Here is my javascript:


function saveData()
{
url = "saveEvents.php"
var outputData = "TESTING OUTPUT";

xmlhttp = false;

// branch for native XMLHttpRequest object
if(window.XMLHttpRequest && !(window.ActiveXObject)) {
try {
xmlhttp = new XMLHttpRequest();
} catch(e) {
xmlhttp = false;
}
// branch for IE/Windows ActiveX version
} else if(window.ActiveXObject) {
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch(e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch(e) {
xmlhttp = false;
}
}
}

if(xmlhttp) {
xmlhttp.open("POST", url, false);

//Send the proper header information along with the request
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.setRequestHeader("Content-length", outputData.length);
xmlhttp.setRequestHeader("Connection", "close");
xmlhttp.onreadystatechange = function() {//Call a function when the state changes.

if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
alert(xmlhttp.responseText);
}

xmlhttp.send("data=" + outputData);
}
}


And here is my PHP code(saveEvents.php):

Code: Select all


<?php 
$File = "events.txt"; 
$Handle = fopen($File, 'w'); 
$Data = $_POST['data']; 
fwrite($Handle, $Data);  
print "Data Added: data=".$Data; 
fclose($Handle); 
?>


What I get is this:

Data Added: data=
Nothing gets written to the text file either. Am I missing something here?
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: PHP Paramaters lost

Post by JakeJ »

I'm not real clear on this.. are you sending a form? It could just be that you have bad form data to begin with.

Also, while this doesn't fix whatever technical problem you have, you might just be better off using jQuery so you don't have to mess with all of that stuff. jQuery form plug in is great and seems to be flawless.
Post Reply