-3

今のところ、私のphpページは、$_POSTフォームを使用してDBの列を更新しますが、ajaxを使用して送信せずに更新する方法は、現在のコードです. 1または2または3または4として完成した列で更新されるようにajaxで行う方法

<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 に答える