2013-09-24 20 views
-3

現在我的PHP網頁更新更新使用表單$ _ POST,但如何做到這一點,而無需使用AJAX 我當前的代碼如下提交DB列。如何做到這一點在AJAX,使得其在完成作爲1或2或3或4如何在數據庫中使用Ajax調用PHP中

<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> 
+1

這是一大堆代碼混淆! –

+4

我們在這裏不是烤蛋糕給你,當你不知道如何。 –

+0

我沒有問你的代碼,我是問,所有@JoranDenHouting – gokul

回答

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 
+0

謝謝@Arun Kumar M – gokul

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; 
}); 
相關問題