【问题标题】:Form action status message after successful submission提交成功后的表单动作状态信息
【发布时间】:2012-03-25 15:23:30
【问题描述】:

表单删除存储在MySql数据库中的与sql命令相关的页面(example.php)上的所有数据。

代码详情如下:

if ((isset($_POST['id'])) && ($_POST['id'] != "")) {
  $deleteSQL = sprintf("DELETE FROM users WHERE id=%s",
                       GetSQLValueString($_POST['id'], "int"));


  mysql_select_db($database_example, $example);
  $Result1 = mysql_query($deleteSQL, $example) or die(mysql_error());


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

$colname_deleterecord = "5";
if (isset($_GET['id'])) {
  $colname_deleterecord = $_GET['id'];
}
mysql_select_db($database_example, $example);
$query_deleterecord = sprintf("SELECT id, user_name, email, address FROM users WHERE id = %s", GetSQLValueString($colname_deletetrecord, "int"));
$deleterecord = mysql_query($query_deleterecord, $example) or die(mysql_error());
$row_deleterecord = mysql_fetch_assoc($deleteecord);
$totalRows_deleterecord = mysql_num_rows($deleterecord);

 <form id="form1" name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <label>
      <input type="submit" name="Delete Record" id="Delete Record" value="Delete Record" />
    </label>
    <input name="id" type="hidden" id="id" value="<?php echo $row_deleterecord['id']; ?>" />
  </form>

提交成功后,如何在纯php语法的同一页面上显示类似(“记录已删除”)的表单动作状态消息?

提前致谢。

【问题讨论】:

    标签: php mysql forms


    【解决方案1】:

    你不能用纯 php 来做,你需要用 ajax 来做......因为它关于客户端脚本和服务器端脚本......一旦你的表单被提交,它就会被重定向到另一个 php 脚本服务器端...为了在客户端发布消息,您只需要使用 AJAX...在我看来这是唯一的可能性

    【讨论】:

    • 我已经将表单提交状态消息集成到几个基于纯 php 语法的表单相关 Web 应用程序中,但不幸的是我在这种情况下遗漏了一些东西并且无法使其正常工作。
    【解决方案2】:

    成功删除后,您创建状态消息,

    // After mysql delete command
    $msg = urlencode("Record deleted");
    

    编辑您的header 命令,使其看起来像这样

    header(sprintf("Location: %s", $deleteGoTo . ?msg={$msg}));
    

    然后选择您想在页面上显示消息的位置并放置代码

    echo isset($_GET['msg']) ? urldecode($msg) : ''; // This line will display the content of the variable $msg if it is set
    

    我可以看到您在同一页面上提交表单,您不需要使用header 功能,在这种情况下,您将状态消息和显示消息更改为如下所示

    $msg = "Record deleted";
    echo isset($msg) ? $msg : ''; // This line will display the content of the variable $msg if it is set
    

    第二种方法会更快。

    【讨论】:

    • 第一个解决方案给出 Parse error: syntax error, unexpected header(sprintf("Location: %s", $deleteGoTo . ?msg={$msg})); in header(sprintf("Location: %s", $deleteGoTo . ?msg={$msg})); when the variable $msg is defined after mysql result in 以下方式:mysql_select_db($database_example, $example); $Result1 = mysql_query($deleteSQL, $example) or die(mysql_error()); $msg = urlencode("Record deleted"); 另一方面第二解决方案在表单提交成功后页面不打印任何状态信息。
    【解决方案3】:

    我终于解决了这个问题。

    诀窍是将查询字符串(键/值对)分配给标头位置变量或直接将标头位置变量替换为其关联的值。

    然后将查询字符串(键/值对)与函数和表单变量关联起来,并用它包裹一个div,提交成功后应该在页面上显示表单动作状态消息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-17
      • 1970-01-01
      • 1970-01-01
      • 2013-05-03
      • 2016-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多