2013-05-11 73 views
0

我使用ajax,只是更新數據庫..問題是,更新後,我使用警報來顯示輸出。但是,當我單擊警告框時,整個頁面會重新加載。有什麼辦法可以避免它?ajax調用沒有從警報刷新響應

首先(trailnew.php) - 三個按鈕的選擇下拉按鈕。

當我選擇的選擇下拉按鈕,然後單擊編輯按鈕

二線我從aanew.php形式(與其他一些東西)

這種形式的第三驗證是aanew完成.php

目標 - 我希望成功的結果可以在trailnew.php中查看,無需重新加載。

任何替代的想法是非常welcomed..This是新me..Thanks ..

這裏是代碼。 Trailnew.php

<?php 
session_start(); 
include('connect.php'); 
?> 

<html> 
<head> 

    <script src="http://code.jquery.com/jquery-latest.js"></script> 

<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.js"></script> 
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"></script> 
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/additional-methods.js"></script> 
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/additional-methods.min.js"></script> 




<script> 



    $(document).ready(function(){ 



    $("#form1").validate({ 
     debug: false, 
    rules: { 

    plid:"required", 
     }, 
    messages: { 

    plid: "Please select a pack name id..", 
    }, 

    submitHandler: function(form) { 

    $.ajax 
    ({ 




type: "POST", 
url: "aanew.php", 
data: $('#form1').serialize(), 
cache: false, 

success: function(response) { 
    $('#result1').html(response); 

     } 
     }); 
     } 

    }); 




    }); 



    </script> 


</head> 
<body> 
<div class="result3"></div> 
Packing List 
</br> 
<form id="form1" name="form1" action="" method="post"> 
<?php 


echo '<select name="plid" id="plid">'; 
echo '<option value="" selected="selected">--Select the Pack Name--</option>'; 
$tempholder = array(); 
$sql="SELECT CONCAT(p.pl_no, '_', DATE_FORMAT(p.pl_dt, '%d/%m/%Y') , '_', g.acname) AS plname, g.gl_id,p.pl_no,p.pl_dt,p.no_box,p.pl_id,g.acname FROM packlist p, glmast g WHERE g.gl_id = p.gl_id ORDER BY pl_dt DESC , pl_no DESC LIMIT 30"; 


$query = mysql_query($sql) or die(mysql_error()); 
$nr = mysql_num_rows($query); 
for ($i=0; $i<$nr; $i++){ 
$r = mysql_fetch_array($query); 
if (!in_array($r['plname'], $tempholder)){ 
$tempholder[$i] = $r['plname']; 
echo "<option value='$r[pl_id]'>".$r["plname"]."</option>"; 
} 
} 

echo '</select>'; 

?><br/> 

<input type="submit" name="delete" value="Delete"/><br/> 
<input type="submit" name="edit" id="edit" value="Edit"/><br/> 

</form> 
<form> 
<input type="submit" name="new" id="new" class="new" value="New" /><br/> 
</form> 



     <?php 
     $e=isset($_POST['form2']) && $_POST['form2']; 
if($e) 
{ 
echo "Done"; 


} 
?> 

<div id="result1"></div> 

</body> 

</html> 

aanew.php

<?php 
session_start(); 
include('connect.php'); 
?> 
<html> 
<head> 

<script src="http://code.jquery.com/jquery-latest.js"></script> 

<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.js"></script> 
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"></script> 
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/additional-methods.js"></script> 
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/additional-methods.min.js"></script> 
<script> 

$("#form2").validate({ 
     debug: false, 
    rules: { 

    plnoedit:"required", 
    pldtedit:"required", 
    noboxedit:"required", 
    glidedit:"required", 


     }, 
    messages: { 

    plnoedit: "Please select a pack list id..", 
    pldtedit: "Please select a item id id..", 
    noboxedit: "Please select a quantity id..", 
    glidedit: "Please select a value id..", 

     }, 

     submitHandler: function(form) { 

    $.ajax 
    ({ 


type: "POST", 
url: "trailnew.php", 
data: $('#form2').serialize(), 
cache: false, 

success: function(response) { 
    $('#result1').html(response); 

     } 
     }); 
     } 



    }); 

</script> 

</head> 

     <body> 
     <div id="#result1"></div> 
     <?php 

    $e=isset($_POST['plid']) && $_POST['plid']; 
    $f=isset($_POST['edit']) && $_POST['edit']; 
    if($e&&$f) 
    { 


    $id=$_POST['plid']; 
    $query5=mysql_query("SELECT g.gl_id, p.pl_no, p.pl_dt, p.no_box, p.pl_id,g.acname 
     FROM packlist p, glmast g 
     WHERE g.gl_id = p.gl_id 
     AND p.pl_id ='".$id."' 
     LIMIT 0 , 30") or die(mysql_error()); 

    $row=mysql_fetch_array($query5); 
    $pl_no=$row['pl_no']; 
    $pl_dt=$row['pl_dt']; 
    $no_box=$row['no_box']; 
    $acname=$row['acname']; 



    ?> 
     <form name="form2" id="form2" method="post" action=""> 
     <P> 
      <LABEL for="plnoedit">PackList No 
         <INPUT type="text" id="plnoedit" name="plnoedit" value= <?php echo $pl_no; ?> /></LABEL><BR><BR> 
         <input type="hidden" name="myFormsName" value="form5id" id="myFormsName"> 
      <LABEL for="pldtedit">Date 
         <INPUT type="text" id="pldtedit" name="pldtedit" value= <?php echo $pl_dt; ?> /></LABEL><BR><BR> 

      <LABEL for="noboxedit">Box No 
         <INPUT type="text" id="noboxedit" name="noboxedit" value= <?php echo $no_box; ?> /></LABEL><BR><BR> 
      <LABEL for="glidedit">Party Name 
         <INPUT type="text" id="glidedit" name="glidedit" value= <?php echo $acname; ?> /></LABEL><BR><BR> 

      <INPUT type="submit" id="editsubmit" name="editsubmit" value="Submit"> <INPUT type="reset"> 
      </P> 

     </form> 
     <?php 
     //pagination starts 
      $id=$_POST['plid']; 
       $per_page = 4; 
     $query12=mysql_query("SELECT `pld_id`,`pl_id`, `item_id`, `quantity`, `size`, `potency`, `box` FROM `packlistdtl` WHERE pl_id ='".$id."' GROUP BY `box` ORDER BY `box` desc") or die(mysql_error()); 
     // figure out the total pages in the database 

     $total_results = mysql_num_rows($query12); 
     $total_pages = ceil($total_results/$per_page); 

     // check if the 'page' variable is set in the URL (ex: view-paginated.php?page=1) 
     if (isset($_GET['page']) && is_numeric($_GET['page'])) 
     { 
       $show_page = $_GET['page']; 

       // make sure the $show_page value is valid 
       if ($show_page > 0 && $show_page <= $total_pages) 
       { 
         $start = ($show_page -1) * $per_page; 
         $end = $start + $per_page; 
       } 
       else 
       { 
         // error - show first set of results 
         $start = 0; 
         $end = $per_page; 
       }    
     } 
     else 
     { 
       // if page isn't set, show first set of results 
       $start = 0; 
       $end = $per_page; 
     } 

     // display pagination 

     echo "<p><a href='view.php'>View All</a> | <b>View Page:</b> "; 
     for ($i = 1; $i <= $total_pages; $i++) 
     { 
       echo "<a href='view.php?page=$i'>$i</a> "; 
     } 
     echo "</p>"; 

     // display data in table 
     echo "<table border='1' cellpadding='10'>"; 
     echo "<tr> <th>Box</th> <th>Item Name</th> <th>Size</th> <th>Potency</th> <th>Quantity</th> <th></th> <th></th></tr>"; 

     // loop through results of database query, displaying them in the table 
     for ($i = $start; $i < $end; $i++) 
     { 
     $item_id=mysql_result($query12, $i, 'item_id'); 


     $query13=mysql_query("SELECT `item_name` FROM `itemmast` WHERE item_id ='".$item_id."'") or die(mysql_error()); 

     $row1=mysql_fetch_array($query13); 
    $item_name=$row1['item_name']; 
       // make sure that PHP doesn't try to show results that don't exist 
       if ($i == $total_results) { break; } 

       // echo out the contents of each row into a table 
       echo "<tr>"; 

       echo '<td>' . mysql_result($query12, $i, 'box') . '</td>'; 
       echo '<td>' .$item_name. '</td>'; 
       echo '<td>' . mysql_result($query12, $i, 'size') . '</td>'; 
        echo '<td>' . mysql_result($query12, $i, 'potency') . '</td>'; 
        echo '<td>' . mysql_result($query12, $i, 'quantity') . '</td>'; 
       echo '<td><a href="edit.php?id=' . mysql_result($query12, $i, 'pld_id') . '">Edit</a></td>'; 
       echo '<td><a href="delete.php?id=' . mysql_result($query12, $i, 'pld_id') . '">Delete</a></td>'; 
       echo "</tr>"; 
     } 
     // close table> 
     echo "</table>"; 

     // pagination 






    } 


    ?> 



     </body> 
     </html> 
+2

發表您的代碼段!你的AJAX回調函數是什麼? – zavg 2013-05-11 20:05:42

+0

@zavg發表了代碼.. – user2234992 2013-05-11 20:13:47

+0

我同意Orangepill,提交按鈕啓動窗體的行動,導致您的情況下重新加載整個頁面。 – zavg 2013-05-11 20:34:45

回答

0

可能需要從點擊處理程序鏈接或按鈕返回false防止默認動作