我的 ajax 脚本有问题,我需要在不刷新页面的情况下加载页面。基于此代码。
索引.php
<div class="header-page" class="clearfix" role="banner">
<div class="container">
<img src="images/logo.png" />
</div>
</div>
<!-- end of HEADER -->
<form>
<div id="contents">
<div class="main-container">
<div class="container">
<table id="tableID">
<tr class="data-head">
<td>Name</td>
<td>Phase</td>
<td>Money 1</td>
<td>Money 2</td>
<td>Money 3</td>
</tr>
<?php
while ($row = mysql_fetch_row($result, MYSQL_BOTH)) {
$id = $row[0];
$companyname = $row[1];
$client = $row[2];
$package = $row[3];
$payment1 = $row[4];
$payment2 = $row[5];
$payment3 = $row[6];
echo '<tr id="'.$id.'">';
echo '<td><b>'.$client.'</b></td>';
echo '<td>';
echo '<select class="phase" onchange="trackPhases(this.value)">';
if($phase_status=='Design'){
echo '<option value="'.$phase_status.''.$id.'" selected>'.$phase_status.'</option>';
echo '<option value="Build-Out'.$id.'">Build-Out</option>';
echo '<option value="Launch'.$id.'">Launch</option>';
}
if($phase_status=='Build-Out'){
echo '<option value="Design'.$id.'">Design</option>';
echo '<option value="'.$phase_status.''.$id.'" selected>'.$phase_status.'</option>';
echo '<option value="Launch'.$id.'">Launch</option>';
}
if($phase_status=='Launch'){
echo '<option value="Design'.$id.'">Design</option>';
echo '<option value="Build-Out'.$id.'">Build-Out</option>';
echo '<option value="'.$phase_status.''.$id.'" selected>'.$phase_status.'</option>';
}
echo '</select>';
echo '</td>';
echo '</tr>';
}
?>
</table>
</div>
</div>
</div>
</form>
<div id="txtHint"></div>
这是我的 Ajax 脚本。我在 html 表格代码上创建了一个下拉控件(只是一个示例)。当我在下拉列表中选择一个值 --select class="phase" onchange="trackPhases(this.value)
时,它应该通过 Ajax 自动加载页面,而不是刷新。
<script type="text/javascript">
function trackPhases(str)
{
if (str=="")
{
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","update.php?q="+str,true);
xmlhttp.send();
}
我的 index.php 虽然很好。但是我的 Ajax 脚本有问题。请帮我。
编辑:
<?php
mysql_connect("localhost", "xx", "password");
mysql_select_db('database');
$query = "SELECT * FROM project";
$result = mysql_query($query);
?>
编辑
编辑 更新.php
<?php
mysql_connect("localhost", "xx", "password");
mysql_select_db('database');
$query = "SELECT * FROM project";
$result = mysql_query($query);
$q = $_GET['q'];
$id = ereg_replace("[^0-9]", "",$q);
$phase_status = preg_replace('/[0-9]+/', '', $q);
$sql = 'UPDATE project SET phase_status="'.$phase_status.'" WHERE id = '.$id;
$retval = mysql_query($sql);
if(! $retval ){
die('Could not update data: ' . mysql_error());
}
?>