我正在使用 ajax 来简单地更新数据库。问题是更新后,我使用警报来显示输出。但是,当我单击警报框时,整个页面会重新加载。有什么办法可以避免吗??
First Up(trailnew.php) - 带有三个按钮的选择下拉按钮。
当我选择选择下拉按钮并单击编辑按钮时
第二 - 我从 aanew.php 得到一个表格(还有一些其他的东西)
此表单的第三次验证在 aanew.php 中完成
目标——我希望在 trailnew.php 中查看成功结果,而不需要重新加载。
任何替代想法都非常受欢迎..这对我来说是新的..谢谢..
这是代码。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>