gokul gokul - 4 months ago 9
Ajax Question

how to update in database using ajax call in php

For now my php page updates a column in DB using form $_POST but how to do it without submitting using ajax
my current code is below . how to do it in ajax so that it updates in the column completed as 1 or 2 or 3 or 4

<html>
<head>
<script language="JavaScript" type="text/javascript" src="js/jquery.js"></script>
<script language="JavaScript" type="text/javascript">

function show(){
document.getElementById("completetrips").style.display='inline';
}
function hide(){
document.getElementById("completetrips").style.display='none';
}

</script>
<style>
.color1
{
background-color:green;
}
.color2
{
background-color:red;
}
.color3
{
background-color:orange;
}
.color4
{
background-color:skyblue;
}
</style>
</head>
<body>
<div align="center" style="font-familt:calibri; font-size: 20px">
<form name="select_dates" id= "select_dates" action="" method = "POST" style="font-size: 24px; font-family:calibri">
<select name="date">
<?php $date = date('d-m-Y');
$i=0;
$time = strtotime($date)-6*86400;
while($i<5){
$time+=86400;
$date1=date('d-m-Y', $time);
echo '<option value="'.$date1.'">'.$date1.'</option>';
$i++;}
echo '<option value="'.$date.'" selected>'.$date.'</option>';
$time = strtotime($date)+0*86400;
$i=0;
while($i<5){
$time+=86400;
$date1=date('d-m-Y', $time);
echo '<option value="'.$date1.'">'.$date1.'</option>';
$i++;}
?>
</select>
<br>
<input type="submit" id="submit" name="submit" value="submit"/><br>
<!-- my code -->
<input type="submit" style="float:right;margin-top:80px;margin-right:10px;" id="load" name="submit" value="Refresh"/>
<!-- end of my code -->
</form>
</div>
<div id="clear" style="height: 5px; width:100%; bgcolor:#1c7bcc"></div>


</body>
</html>


<?php

if (isset($_POST['submit'])){

$date_today = $_POST['date'];
error_reporting(E_ALL);
$conn = mysql_connect('localhost', 'root', 'root') or die("error connecting1...");
mysql_select_db("demo",$conn) or die("error connecting database...");

$timezone = 'Asia/Kolkata';
date_default_timezone_set($timezone);



$query = "Select * from `trips` where `date` = '$date_today' ORDER BY `trips`.`book_id` ASC";
if (!mysql_query($query)){echo "not found";}
else{
$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result))
{

// if statemnet here for completed == 0 and hence hide after complettion.
// display results

echo '<html><head><title> Trips Today</title>';
echo '<div align="center" style="font-family:calibri; font-size:25px ">';
echo '<table align="center" ><tr><td>';
echo "Number of trips for the day: ";
echo '</td><td><b>';
echo mysql_num_rows($result)-1;
echo '</b></td></tr><tr><td>';

echo "Day of considertaion is a ";
echo '<b>';
echo date("l",strtotime($date_today));
echo '&nbsp;&nbsp;&nbsp;</td><td>';
echo $date_today;
echo '</b></td></tr></table>';

echo "</div> <button type='button' onclick='hide()'>Hide trips</button> <button type='button' onclick='show()'>show trips</button><br>";
echo "<div id='completetrips' style='display:inline'> ";
echo "<table align='center' id='tab' border='1' cellspacing='2'>";
echo "<tr align='center'>";
echo" <th align='center'>Trip_id</th>";
echo "<th align='center'>user_id</th>";
echo "<th>book_id</th><th>name</th><th>source</th><th>destination</th><th>time</th><th>date</th><th>phone</th><th>group_id</th><th>Driver id</th><th>Option 1</td><th>Option 2</td><th>Option 3</td><th>Option 4</td></tr><br>";

/* my code */

while($row=mysql_fetch_assoc($result))
{
?>
<tr align='center' class="color<?=$row['completed']?>"><td><?=$row['trip_id']?></td><td><?=$row['user_id']?></td><td><?=$row['book_id']?></td><td><?=$row['name']?></td><td><?=$row['source']?></td><td><?=$row['destination']?></td><td><?=$row['time']?></td><td><?=$row['date']?></td><td><?=$row['phone']?></td><td><?=$row['group_id']?></td><td><?=$row['driver_id']?></td>
<td>
<button name="completed" class="complete">Completed</button></td>
<td><button name="cancelbyus" class="cancel">Cancelled by Us</button></td>
<td><button name="cancelbycus" class="cancelcus">Cancelled by Customer</button></td>
<td><button name="completebutlate" class="completelate">Complete but Late</button></td>
<input name="tripid" id="tripid" style="display:none" value="<?=$row['trip_id']?>" /></tr>
<?PHP
}

}// display ends


}// while rows for todAY

}// if submit

if(isset($_POST['completed']))
{
$conn = mysql_connect('localhost', 'root', 'root') or die("error connecting1...");
mysql_select_db("cubitoindemo",$conn) or die("error connecting database...");
$tripid = $_POST['tripid'];
$update_trips = "UPDATE trips SET completed = 1 where trip_id = '$tripid'";
$quer = mysql_query($update_trips);
}
if(isset($_POST['cancelbyus']))
{
$conn = mysql_connect('localhost', 'root', 'root') or die("error connecting1...");
mysql_select_db("cubitoindemo",$conn) or die("error connecting database...");

$tripid = $_POST['tripid'];
$update_trips = "UPDATE trips SET completed = 2 where trip_id = '$tripid'";
$quer = mysql_query($update_trips);
}
if(isset($_POST['cancelbycus']))
{
$conn = mysql_connect('localhost', 'root', 'root') or die("error connecting1...");
mysql_select_db("cubitoindemo",$conn) or die("error connecting database...");

$tripid = $_POST['tripid'];
$update_trips = "UPDATE trips SET completed = 3 where trip_id = '$tripid'";
$quer = mysql_query($update_trips);
}
if(isset($_POST['completebutlate']))
{
$conn = mysql_connect('localhost', 'root', 'root') or die("error connecting1...");
mysql_select_db("cubitoindemo",$conn) or die("error connecting database...");

$tripid = $_POST['tripid'];
$update_trips = "UPDATE trips SET completed = 4 where trip_id = '$tripid'";
$quer = mysql_query($update_trips);
}

/* end of my code */


?>
</div>

Answer

This is not the exact solution but a sample on how to proceed.

function handleClick(id,mode){

    $.ajax({
        url:'trips.php',
        data:{mode:mode,id:id},  // pass data 
        dataType:'json',
        success:function(data){
            //success logic
        }
        error:function(data){
            //error logic
        }
    });

}

PHP file trips.php

$id = $_Get['id']; // data passed here
$sql = "UPDATE tab_name
    SET colm='$id';

//then execute the query