Jump to content


  • Posts

  • Joined

  • Last visited

Everything posted by jjack46

  1. 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
  2. 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
  • Create New...