Monday, 9 September 2013

Load the current page via Ajax in php

Load the current page via Ajax in php

I have a problem on my ajax script and I need to load the page without
refreshing the page. Base on this code.
index.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>
And this is my Ajax script. I created a dropdown control(just a sample) on
the html table code. When I select a value on the dropdown -- select
class="phase" onchange="trackPhases(this.value), it should automatically
load the page via Ajax not a refresh.
<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","index.php?q="+str,true);
//xmlhttp.send();
}
My index.php is fine though. But there is something wrong with my Ajax
script. Please help me.
EDIT:
<?php
mysql_connect("localhost", "xx", "password");
mysql_select_db('database');
$query = "SELECT * FROM project";
$result = mysql_query($query);
?>
EDIT

EDIT update.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_entry SET phase_status="'.$phase_status.'" WHERE
id = '.$id;
$retval = mysql_query($sql);
if(! $retval ){
die('Could not update data: ' . mysql_error());
}
?>

No comments:

Post a Comment