现在我的php页面使用$ _POST格式更新数据库中的列但是如何在不使用ajax提交的情况下更新 我目前的代码如下。如何在ajax中执行此操作,以便在完成为1或2或3或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 ' </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>
答案 0 :(得分:4)
这不是确切的解决方案,而是一个如何进行的示例。
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文件 trips.php
$id = $_Get['id']; // data passed here
$sql = "UPDATE tab_name
SET colm='$id';
//then execute the query
答案 1 :(得分:1)
始终从逻辑中分离演示文稿。将所有PHP放在单独的文件中,并使用代码更新名为update.php的数据库或类似的东西。然后编写一个简单的jquery函数来执行ajax调用。像这样:
$('#update').submit(function() {
$.ajax({
type: "POST",
url: "update.php",
data: { dataToUpdate: $('#update').attr('value') },
beforeSend: function(){
//Do something here like change colors and what not
},
success: function(){
//Write some UI updation or message on success
},
error: function(){
//Display an error message
}
});
return false;
});