[英]Can't refresh a php table using ajax
I want to refresh a table from the main file of a web page using an ajax function. 我想使用ajax函数从网页的主文件刷新表。 When the select box change its value the function dayChanged()
has to be executed. 当选择框更改其值时,必须执行功能dayChanged()
。 This function is inside the file "ajax.js". 该函数位于文件“ ajax.js”中。 The problem is that when I select another option of the elect box nothing happens. 问题是,当我选择电子选框的另一个选项时,什么也不会发生。 I include the file like: <script src="ajax.js></script>
. I expect to send the value of the select box to a MySQL query (this query is inside "config.php") and then refresh the table of the main file. Here's the main: 我包含如下文件: <script src="ajax.js></script>
。我希望将选择框的值发送到MySQL查询(此查询位于“ config.php”内部),然后刷新表主文件的位置。
<html>
<head>
<style type="text/css">
body {
background-color: #9C3;
}
p {color:black;}
.table_test {
font-family: Constantia, Lucida Bright, DejaVu Serif, Georgia, serif;
}
</style>
<script src="ajax.js"></script>
</head>
<body><div align='center'>
<div class="table_test">
<table border='1' cellpadding='0' cellspacing='0' width='600' bgcolor='#F6F6F6' bordercolor='#FFFFFF'>
<tr>
<td width='150' style='font-weight: bold'></td>
<td width='150' style='font-weight: bold'>Festa</td>
<td width='150' style='font-weight: bold'>Preu</td>
<td width='150' style='font-weight: bold'>Entrada</td>
<td width='150' style='font-weight: bold'>Final</td>
<td width='150' style='font-weight: bold'>Estil</td>
<td width='150' style='font-weight: bold'>Llista</td>
</tr>
<form name="form1" method="post">
<div id="table_var">
<?php
include ('functions.php');
$dw = date("w");
display_table_by_day($dw);
?>
</table>
</div>
<p>Veure un altre dia de la setmana:
<select name="select_day" id="day_select" onChange="dayChanged()">
<option value="1">Dilluns</option>
<option value="2">Dimarts</option>
<option value="0">Diumenge</option>
</select>
</p>
</form>
</body>
</html>
The function display_table_by_day($dw) is working right. 函数display_table_by_day($ dw)运行正常。 The ajax.js file is: ajax.js文件是:
function dayChanged()
{
var buscaAjax;
if(window.XMLHttpRequest)
{
buscaAjax = new XMLHttpRequest();
}else{
buscaAjax = new ActiveXOject("Microsoft.XMLHTTP");
}
buscaAjax.onreadystatechange = function(){
if(buscaAjax.readyState==4 && buscaAjax.status==200){
document.getElementById('table_var').innerHTML = buscaAjax.responseText;
}
}
var dato = document.form1.select_day.value;
buscaAjax.open("GET","config.php?variable="+dato,true);
buscaAjax.send();
}
Finally, the config.php file is: 最后,config.php文件是:
<?php
error_reporting(E_ALL ^ E_NOTICE);
include ('connection.php');
include ('functions.php');
$dw = $_GET['variable'];
display_table_by_day($dw);
?>
Any idea why is not working? 知道为什么不起作用吗?
You are referencing the select
by name: 您正在按名称引用select
:
var dato = document.form1.select_day.value;
Change it to the id
of the select
like this: 将其更改为select
的id
,如下所示:
var dato = document.form1.day_select.value;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.