在表单提交后返回/重定向到锚定的div

时间:2013-02-26 17:40:06

标签: php html forms redirect

就我在几个帖子中研究过,以下表格应在提交后返回div #CCC

$editFormAction = $_SERVER['PHP_SELF']."#CCC";

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "formX")) { 
$updateSQL = sprintf("UPDATE foodtable SET food_preference=%s  WHERE food_id=%s",    
                       GetSQLValueString($_POST['food_preference'], "text"),
                       GetSQLValueString($_POST['food_id'], "int"));

  mysql_select_db($database_XYZ, $XYZ);
  $Result1 = mysql_query($updateSQL, $XYZ) or die(mysql_error());

  $updateGoTo = "preference.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
    $updateGoTo .= $_SERVER['QUERY_STRING'];
      }
  header(sprintf("Location: %s", $updateGoTo));
    exit ();
}

<body>

<div id ="AAA">
<p>Some text</p>
</div>

<div id ="BBB">
<p>Some more text</p>
</div>

<div id ="CCC">
     <form action="<?php echo $editFormAction; ?>" method="post" name="formX" id="formX">
     <table align="center">
     <tr valign="baseline">
     <td align="right" nowrap="nowrap">Update Food Name:</td>
     <td><input type="text" name="food_name" value="" size="32" /></td>
     </tr>
     <tr valign="baseline">
     <td nowrap="nowrap" align="right">
     <input type="hidden" name="MM_update" value="formX" />
     <input type="hidden" name="food_id" value="<?php echo $query['food_id']; ?>" />
     </td>
     <td>
     <input type='submit' value='Update Food Name'/>"
    </td>
    </tr>
    </table>
   </form>
</div>

</body>

但是在提交时它没有返回div #CCC。上面的代码块有什么缺点吗?

感谢,

1 个答案:

答案 0 :(得分:2)

表单正在提交到您的页面#CCC但行

header(sprintf("Location: %s", $updateGoTo));

在处理完表单后重定向您。