dongxie3963 2012-05-01 05:21
浏览 9
已采纳

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

The following form displays the success message every time on page launch in the browser logically but the form should display the success message only after it is submitted successfully.

$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));
}

The html part of the form is as following:

<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>

This is how the success message is displayed within a div on the page:

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

What I'm doing wrong here?

  • 写回答

3条回答 默认 最新

  • dongwen9975 2012-05-01 05:34
    关注

    I have created two function to show message

    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;
        }   
    }
    

    In your example use like below

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

    For dispaly message ,make sure there is session_start();

    <?php echo showMessageStack(); ?>
    

    It will display your message after form submit only

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分