gpt4 book ai didi

php - 如何在成功提交表单后在 div 中创建成功消息?

转载 作者:太空狗 更新时间:2023-10-29 15:06:24 34 4
gpt4 key购买 nike

从逻辑上讲,以下表单每次在浏览器中启动页面时都会显示成功消息,但该表单应仅在提交成功后才显示成功消息。

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

$message = "Record has been updated successfully.";

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
$updateSQL = sprintf("UPDATE table SET name=%s, email=%s,

GetSQLValueString($_POST['name'], "text"),
GetSQLValueString($_POST['email'], "text"),

mysql_select_db($database_test, $test);
$Result1 = mysql_query($updateSQL, $test) or die(mysql_error());

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

表单的html部分如下:

<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
<table align="center" class="test_table">
<tr valign="baseline">
<td align="right" nowrap="nowrap" class="table-title">Name:</td>
<td class="table-content"><input type="text" name="name" value="<?php echo htmlentities($row_user['name'], ENT_COMPAT, 'utf-8'); ?>" size="32" /></td>
</tr>
<tr valign="baseline">
<td align="right" nowrap="nowrap" class="table-title">E-mail:</td>
<td class="table-content"><input type="text" name="email" value="<?php echo htmlentities($row_user['email'], ENT_COMPAT, 'utf-8'); ?>" size="32" /></td>
</tr>
</table>
<input type="hidden" name="MM_update" value="form1" />
<input type="hidden" name="id" value="<?php echo $row_user['id']; ?>" />
<input class="submit" name="UpdateRecord" type="submit" id="UpdateRecord" value="Update Record" /></form>

这是在页面上的 div 中显示成功消息的方式:

<p> <?php 
if (!empty($message)) {
echo "<div class=\"successmessage\">" . $message . "</div>";
}
?></p>

我做错了什么?

最佳答案

我创建了两个函数来显示消息

function addMessageStack($message,$type='success')
{
$_SESSION['sess_MessageStack'][] = array($type,$message);
}
function showMessageStack()
{
$str = '';
if(count($_SESSION['sess_MessageStack']) > 0)
{
for($i=0;$i<count($_SESSION['sess_MessageStack']);$i++)
{
$str.="<div class='".$_SESSION['sess_MessageStack'][$i][0]."Message left'>".$_SESSION['sess_MessageStack'][$i][1]."</div>";
}
unset($_SESSION['sess_MessageStack']);
return $str;
}
}

在你的例子中使用如下

addMessageStack('Record has been updated successfully.');    
header(sprintf("Location: %s", $updateGoTo));
exit;

对于显示消息,确保有session_start();

<?php echo showMessageStack(); ?>

它只会在表单提交后显示您的消息

关于php - 如何在成功提交表单后在 div 中创建成功消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10393890/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com