-3

现在,我的 php 页面使用表单 $_POST 更新 DB 中的一列,但如何在不使用 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 '&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>
4

2 回答 2

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
于 2013-09-24T09:42:30.207 回答
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;
});
于 2013-09-24T09:57:09.557 回答