变量不保存在.txt(中)

时间:2017-03-31 20:12:35

标签: javascript php

我想知道为什么我的php页面中的变量没有保存到我的.txt。我已经发布了以下代码。所有变量都匹配。

每次刷新新页面时,消息都会消失,我检查了我的txt文件,变量不会保存在数组中。

function updateData() {
					$.getJSON("#host/information.txt", 
						function(data) {
							var senator = data[0];
							var cmicrophone = data[1];
							var microphone = data[2];
							var words = data[3];
							$("#java").text(senator + cmicrophone +  microphone + words);}
						 );
				}

<?php 
	session_start();
	
	if(isset($_POST["senator"]) && isset($_POST["cmicrophone"]) && isset($_POST["microphone"]) && isset($_POST["words"])) { // If the page receives POST data, it needs to be stored
    $senator = $_POST["senator"];
	$cmicrophone = $_POST["cmicrophone"];
    $microphone = $_POST["microphone"];
	$words = $_POST["words"];
    $data = json_encode(Array($senator, $cmicrophone, $microphone, $words)); // Put values into an array and encode it for storage
    file_put_contents('information.txt', $data); // Put the JSON-encoded array into a text file. This function replaces the contents of the text file, which is exactly what is needed in this application. To append instead of replacing the contents, you need a FILE_APPEND flag.
	} else { // If there's no POST data, the values are retrieved from the storage
    $data = json_decode(file_get_contents('information.txt')); // Retrieve the contents of the text file and decode them back into an array
    $senator = $data[0];
	$cmicrophone = $data[1];
    $microphone = $data[2];
	$words = $data[3];
	}
	echo "". $senator. "" .$cmicrophone. "" .$microphone. "";

	$wordformat = wordwrap($words, 32, "\n", false);
	
	echo "".$words. "";
	
	
	?>

如果代码正确,那么它只是

  

$。getJSON()

位置不正确?变量都是正确的。

0 个答案:

没有答案