Jump to content

Getting 'undefined array key' error passing data to PHP process thro XMLHttprequest


jjack46
 Share

Recommended Posts

I'm attempting to pass a text string from a javascript process to a php process, using XMLHttprequest, and am getting an 'undefined array key' error. The php process should save the string to a file. It appears to pass the string to the php code okay as it returns the message 'File saved', but it also gives the error message 'undefined array key' on 'data' on line 2 of the php code. I appears to be saving a blank string to the text file as it overwrites any previous saved text in the file. So it looks like no data is being passed to the php codebut it is still try to save it.  I'm no sure whether the problem is on the javascript side or the php side.  The attached files are test code which reproduces the error.   Clicking on the button in 'TestProgram.php will attempt to pass the test string to 'saveGame.php which should save the text string to 'test26.txt :-

 

Any help would be much appreciated.

TestProgram.php saveGame.php test26.txt

Link to comment
Share on other sites

Spaces are important in HTTP requests. You've sent this to the server: "?d = data", so that key would be "" and the value would be " data"

If you remove the spaces it should work.

It would be better to use $_GET than $_REQUEST to make it clear where the information is meant to come from. $_REQUEST also searches for data in POST data and cookies.

Link to comment
Share on other sites

I made a mistake when I submitted the topic. The variable in the $_POST(......) should have been "data" not "d" but it still has the same error. I didn't submit the code correctly I hope this is better .  I am using $_POST as the string that gets passed can be quite long, up to 56 names.

This is a correct submission of the "TestProgram.php"

<!DOCTYPE html>
<html>
<head>
<title>Test program</title>
<script language="javascript">
function formatOutput() {
	// lots of formatting goes on here
	var formattedText = "This is a test";   //test data               
	getGame(formattedText);
}
function getGame(str){
	//Sends data to the php process "save Game".
	var data = "Test String";
	var xhr = new XMLHttpRequest();
	xhr.open("POST", "saveGame.php", true);
	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xhr.onreadystatechange = function() {
		if (this.readyState === 4 ){
			alert(xhr.responseText);
		}
	};
	xhr.send(data); 
}
</script>
</head>
<body>
	<table width="1000" align="center" border="0">
		<br />
		<tr>
			<td width="500"align="center" colspan="3" <button onclick="formatOutput()"/>Click on me</td>
		</tr>
	</table>	
</body>
</html>

 This is then supposed to pass the data to the "saveGame.php 

<!DOCTYPE html>
<html>
<body>

<?php
$outputString = $_POST["data"];
$fp = fopen("C:\\xampp\\htdocs\\BowlsClub\\GamesSaved\\test26.txt","w");
if (!$fp){
	$message = "Error writing to file. Try again later!" ;
}else{
	fwrite($fp, $outputString);
	$message = "File saved!"; 
}
fclose($fp);

echo $message;
?>
</body>
</html>

 The error occurs at line 6 ($outputString = $_POST["data"];) . It states that "data" is an 'undefined array key'. Screen shot of error attached.

Is it possible to edit a topic once it's been submitted. I couldn't find any help on it.

jjack46

 

testerror.jpg

Link to comment
Share on other sites

The information you pass to the send() method of the XMLHttpRequest object is just a string. The Javascript variable "data" does not exist in the HTTP request, it just holds the string "Test String" temporarily on the client side.

The variable name "data" has to be included as part of the body of the request, like this:

xhr.send("data=Test String"); 

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...