你能把刷新和更新js脚本放在同一个函数中吗?

时间:2017-03-15 19:13:18

标签: javascript php jquery html

现在,我有一个HTML表单,代码如下所示。它有一个按钮,在该按钮中打开一个新选项卡(URL),并在每次用户更改信息时更新PHP页面上的信息。现在的PHP页面显示在另一台设备上,并且包含更新的信息,因为我使用.txt并将其拉到同一台服务器上。现在的问题是更新的信息仅显示页面在其末尾刷新的时间,这不是我想要的。

我的问题是现在,我的js函数已满并更新变量。我是否也可以在其中包含刷新功能?

目标:按钮在同一时间更新并刷新URL(PHP页面),以便最终用户不必在其末端刷新它,它刷新同一服务器上的URL。

我尝试在那里使用loc,但它似乎无法正常工作。

HTML:

<!DOCTYPE html>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
			<script language="javascript" type="text/javascript">
			function dynamicdropdown(listindex) {
			  document.getElementById('senator').className = listindex;
			}
			</script>
			
			<style>
			optgroup {
			  display: none;
			}

			select.agriculture 
			optgroup.agriculture,
			select.appropriations 
			optgroup.appropriations
			{
			  display: block;
			}
			
			div#header{
			padding: 1px;
			color: yellow;
			padding-left: 9px;
			background-color: #000080;
			}
			
			.category_div{
			padding: 3px;
			}
			
			.sub_category_div{
			padding: 3px;
			}
			
			.microphone{
			padding: 3px;
			}

			.body{
			padding-right: 5px;
			}	
			</style>
	</head>
	
<body>

	<div class="header" id="header">
	<h1>Indiana State Senate IT</h1>
	</div>

	<div class="room130">
	<h3>Room 130</h3>
	<form target="Room 130" action = "test.php" method="POST">
		<div class="category_div" id="category_div">Committee:
			<select id="committee" name="committee" onchange="javascript: dynamicdropdown(this.options[this.selectedIndex].value);">
				<option value="">Select Committee</option>
				<option value="agriculture">AGRICULTURE</option>
				<option value="appropriations">APPROPRIATIONS</option>
			</select>
		</div>
		
		<div class="sub_category_div" id="sub_category_div">
		Individual:
			<select name="senator" id="senator">
				<option value="">Select individual</option>
					<optgroup class="agriculture">
						<option value="THE CHAIR">THE CHAIR</option>
						<option value="THE PRESENTER">THE PRESENTER</option>
					</optgroup>
			</select>
		</div>
		
		<div class="microphone" id="microphone">Microphone:
			<select id="microphone" name = "microphone">
				<option value=" "> </option>
				<option value="ON">ON</option>
				<option value="OFF">OFF</option>
			</select>
		</div>
			<button onclick="refresh()">Refresh</button>
			<button onclick="updateData()">Update</button>
		
		</form>
	</div>

PHP代码:

<!DOCTYPE html>

<html>
<head>
	<title>Room 130</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript"> 
	$(document).ready( function() {
	$('#java').delay("90000").fadeOut();
		});
		
		function updateData() {
			$.getJSON("sr.iga.local/data.txt", 
				function(data) {
					var senator = data[0];
					var microphone = data[1];
					$("#java").text("Please remind " + senator + " to make sure their microphone is " + microphone + ". Thank you.");}
				function(data){
				window.location.reload(true); });
		}
</script>
<style>
	body {
			background-color: #000080;
			color: white;
			font-size: 72px;
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;
			height: 100%;
			font-weight: bold;
	}
	
	.java {
		width: 90%;
		margin: 200px;
		padding-top: 100px;
		text-align: center;
	}
</style>
</head>
<body>

<div class="java" id="java">

	<?php 
	session_start();
	
	if(isset($_POST["senator"]) && isset($_POST["microphone"])) { // If the page receives POST data, it needs to be stored
    $senator = $_POST["senator"];
    $microphone = $_POST["microphone"];
    $data = json_encode(Array($senator, $microphone)); // Put values into an array and encode it for storage
    file_put_contents('data.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('data.txt')); // Retrieve the contents of the text file and decode them back into an array
    $senator = $data[0];
    $microphone = $data[1];
	}
	echo "Please remind ". $senator. " to make sure their microphone is " .$microphone. ". Thank you.";
	
	
	?>
</div>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

如果您进行常规POST,页面将自动更新。我不知道这里的问题是什么。