如何在表单响应返回后刷新页面

时间:2013-08-02 10:08:01

标签: php jquery html mysql ajax

我在几个网站上搜索过我的问题,但我无法得到它。我正在寻找的是如何在响应成功删除行消息后刷新页面。我的页面admin2.php显示了来自DB的管理员信息以及学生的表格(来自数据库),学生的每一行旁边都有一个复选框。我将表单(表)发送到Ajax上,转到(admin3Response.php)页面进行检查,然后回复一下。这是我的代码有问题。

admin2.php

      <tr><th>Admin Information</th>
        <th style="width: 212px"></th></tr>
      <tr><td><b>Admin Name: <?php echo $name; ?></b><span name="adminName"></span></td>
        <td style="width: 212px"><b>Admin ID: <?php echo $userID; ?></b><span name="adminID"></span></td></tr>
      <tr><td><b>Phone: <?php echo $phone; ?></b><span name="adminPhone"></span></td>
        <td style="width: 212px"><span></span></td></tr>

admin2.php 这是我想要刷新的形式。从这张表我可以选择我要删除的学生。

else if(isset($dropStudent))
{

if(mysql_num_rows($QueryResult7) == 0)
  {
            ?>
    <h2><?php echo ("There are no students available.");?></h2>
    <p>
    <div class="form_settings">
    <input class="submit" type="button" onclick="goBack()" name="delete0" value="Back" style="margin-left: 0px; width: 89px">
    </div>
    <?php
     }
else 
{
?>

<form name="form3" id ="form3" method="" action="admin2.php"  >
<input type="hidden" value="Delete" name="deleteStudent">
<?php
echo $adminUser;
?>
<TABLE style="width:100%; border-spacing:1;">


<TR><TH>Choose</TH><TH>USER_ID</TH><TH>STUDENT NAME</TH><TH>EMAIL</TH><TH>PHONE</TH>                   <TH>MAJOR</TH><TH>GPA</TH><TH>DEGREE</TH></TR>
<?php

$Row7 = mysql_fetch_row($QueryResult7);
// reads row of query into indexed array
// advances result pointer
do
{
echo '<TR><TD><input type="checkbox" name="checkbox[]" value="' . $Row7[0] .  '"></TD>'            .
"<TD>{$Row7[0]}</TD>

<TD>{$Row7[1]}</TD>" .
"<TD>{$Row7[2]}</TD>

<TD>{$Row7[3]}</TD>" .
"<TD>{$Row7[4]}</TD>

<TD>{$Row7[5]}</TD>" .
"<TD>{$Row7[6]}</TD>

</TR>";

$Row7 = mysql_fetch_row($QueryResult7);

} while ($Row7); // tests for content of null

?>

<input type="hidden" value="<?php echo $countStds; ?>" name="countStds">
<input type="hidden" value="<?php echo $adminUser; ?>" name="adminUser">


<tr>
<td colspan="10"> 

        

</tr>
<?php
mysql_close($DBConnect);
?>
</TABLE>
</form>
<script>
// $("#form3") grabs the above form with an ID of "form3"
$("#form3").on('submit', function(){

    // Grab all form values
    var values = $("#form3").serialize();
    // Send form values over ajax
    $.ajax({
        method: 'POST',
        url: 'admin3Response.php',
        data: values,
        // execute when response comes back from admin3Response.php
        success: function(data){
            // data = whatever admin3Response echo'd back
            // check for success
            if (data.lastIndexOf("success") >= 0) {

                // no errors
                    alert("Student was dropped successfully");

        window.location.reload(true); // It reload an empty page; didn't work
                //alert("TODO: redirect user somewhere?");
            } else {
                // Display error message
                alert(data);
            }
        }
    })
    return false;
});
  </script>          
  <?php

  }
  }

admin3Response.php

else if(isset($deleteStudent))

{

 if (isset($checkbox))
{       

 for($i=0; $i<$countStds; $i++)
{
    $delStudent = $checkbox[$i];
    $wholeStudents = mysql_query("DELETE USERS, ENROLLMENTS, OVERIDES FROM          USERS, ENROLLMENTS, OVERIDES WHERE USERS.USER_ID = ENROLLMENTS.USER_ID AND   ENROLLMENTS.USER_ID = OVERIDES.USER_ID AND USERS.USER_ID = '$delStudent'"); 
    $delUserStudent = mysql_query("DELETE USERS FROM USERS WHERE USER_ID =    '$delStudent'");
    $delOverStudent = mysql_query("DELETE OVERIDES, USERS FROM OVERIDES, USERS  WHERE USERS.USER_ID = OVERIDES.USER_ID AND USERS.USER_ID = '$delStudent'");
    $delEnrStudent = mysql_query("DELETE ENROLLMENTS, USERS FROM ENROLLMENTS,  USERS WHERE USERS.USER_ID = ENROLLMENTS.USER_ID AND USERS.USER_ID = '$delStudent'");     
}

 echo ("You have deleted "); echo count($checkbox);  echo (" student(s)   `enter code here`successfully."); 


echo ("success");
 }
 else
 { 
 echo ("No student selected"); 
 }

 mysql_close($DBConnect);    

 }

我尝试使用“window.location.reload(true);”,但它重新加载一个空页面。我的猜测是我需要使用get方法将所有变量从admin3Response.php传递给admin2.php,但我不知道该怎么做。任何帮助都会受到赞赏。

2 个答案:

答案 0 :(得分:0)

很多方面,我想你可以这样做:

    window.location.reload();
    history.go(0);
    window.location.href=window.location.href;

如果您想重定向到另一个页面更改

 window.location="Path"

答案 1 :(得分:0)

试过这个?

if (data.lastIndexOf("success") >= 0) {
    // no errors
    alert("Student was dropped successfully");
    window.location = window.location;
}