如何在表单中显示错误消息?

时间:2015-10-25 11:53:21

标签: php

当我点击提交按钮时,页面只显示消息,但是现在我想在单击提交按钮后在表单内显示消息。如何添加一些代码或更改以下代码以使消息可以在表单内运行? 这是我的PHP代码:

  <?php 

function topic_go($id){
    echo "<meta http-equiv=\"refresh\" content=\"0;url=main_forum.php?act=topic&id=".$id."\">";
}
$id = $_GET['id'];

if(!$_SESSION['sign_in']){
    $sql4= "SELECT * FROM categories WHERE level <".$_SESSION['userlevel']."+1";
    $res4= mysql_query($sql4) or die (mysql_error());
    $row4 = mysql_fetch_assoc($res4);
    $sql5= "SELECT * FROM sub_categories WHERE sub_id ='".$id."'";
    $res5 = mysql_query($sql5) or die (mysql_error());
    $row5 = mysql_fetch_assoc($res5);
    echo "<script type=\"text/javascript\">"; 
    echo "alert('Please Login To Create Topic!');";
    echo "window.location='main_forum.php?act=forum&id=".$row5['sub_id']."'";
    echo "</script>";
}else{

if($id){
    $sql="SELECT * FROM sub_categories WHERE sub_id = '".$id."'";
    $res = mysql_query($sql) or die(mysql_error());
    if(mysql_num_rows($res) == 0){
        echo "The forum you are trying to create a topic on, does not exist!\n";
    }else{
        $row1 = mysql_fetch_assoc($res);
        if ($row1['level'] == 1 && $admin_user_level == 0){
            echo "You are not an administrator, you cannot post on this forum";
        }else {
        if (!$_POST['submit']) {
        echo "<table bgcolor=\"#CFFAE4\" cellspacing=\"10\" align=\"center\">\n";
        echo "<form method=\"post\" action=\"./main_forum.php?act=create&id=".$id."\">\n";
        echo "<tr><td>Forum Sub Category</td><td><select name=\"cat\" style=\"font-size:16px;\">\n";
        $sql2= "SELECT * FROM categories WHERE level <".$admin_user_level."+1";
        $res2= mysql_query($sql2) or die (mysql_error());
        while($row = mysql_fetch_assoc($res2)){
            $sql3= "SELECT * FROM sub_categories WHERE sub_cid = '".$row['cat_id']."'";
            $res3 = mysql_query($sql3) or die (mysql_error());

            echo "<option value=\"0\">".$row['cat_name']."</option>\n";
            while($row2 = mysql_fetch_assoc($res3)){
                $selected = ($row2['sub_id'] == $id) ? " SELECTED" : "";
                echo "<option value=\"".$row2['sub_id']."\"".$selected.">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;".$row2['sub_name']."</option>\n";
            }

        }
        echo "</select></td></tr>\n";
        echo "<tr><td valign=\"top\">Topic Title</td><td><textarea name=\"title\" style=\"width:400px;height:50px;font-size:16px\"></textarea></td></tr>\n";
        echo "<tr><td valign=\"top\">Message</td><td><textarea name=\"message\" style=\"width:500px;height:300px;font-size:20px;\"></textarea></td></tr>\n";
        echo "<tr><td colspan=\"2\" align=\"right\"><input type=\"button\" onClick=\"history.go(-1);\" value=\"Back\" class=\"btnz btnz_color\"> <input type=\"submit\" name=\"submit\" value=\"Create Topic\" class=\"btnz btnz_color\"></td></tr>\n";
        echo "</form></table>\n";
        }else{
            $cat = $_POST['cat'];
            $title = $_POST['title'];
            $msg = $_POST['message'];

            if($cat && $title && $msg){
                $sql = "SELECT level FROM sub_categories WHERE sub_id ='".$cat."'";
                $res = mysql_query($sql) or die (mysql_error());
                if(mysql_num_rows($res)==0){
                    echo "This forum sub category does not exist!\n";
                } else{
                    $row = mysql_fetch_assoc($res);
                    if ($row['level'] == 1 && $admin_user_level !=1){
                        echo "You are not an admin therefore you cannot post a new topic!";
                    }else{
                        if (strlen($title) < 3 || strlen($title) > 1000){
                            echo "The title must between 3 and 1000 characters!\n";
                        }else{
                            if(strlen($msg) < 3 || strlen($msg) > 10000){
                                echo  "The message must between 3 and 10,000 characters!\n";
                            }else{
                                $date = date("m-d-y") . " at " . date("h:i:s");
                                $time = time();
                                $sql2 = "INSERT INTO topics (topic_cid, topic_title, topic_uid, topic_date, topic_time, topic_message) 
                                VALUES('".$cat."','".$title."','".$_SESSION['userid']."','".$date."','".$time."','".$msg."')";
                                $res2 = mysql_query($sql2) or die (mysql_error());
                                $tid = mysql_insert_id();
                                topic_go($tid);
                            }
                        }
                    }
                }
            }else{
                echo  "Please supply all fields!\n";
} }}    }
}
}?>

谢谢你的帮助!

0 个答案:

没有答案