dsfdsfdsfdsf1223 2013-06-14 18:04
浏览 44
已采纳

PHP表单处理与正则表达式绘制空白

My logic has been to try and validate the two text fields (for specifically how, check out the error strings), and use a flag that if turned off reverts the user back to the original form with his most recent data still written in the fields and the radio box checked off to their last choice (default should be red).

As soon as I try to load the entry.php it comes up completely blank. The top.php is fine, but whenever you click on the submit, it doesn't go anywhere and just keeps (weirdly and randomly?) indenting the default string in the textarea.

Infinite kudos and thanks to everyone in advance!

Source Code:

(Entry.php)

<?php
        if($_POST){
            $rchecked = "";
            $ychecked = "";
            $bchecked = "";
            $errormsgs = [];
            $valid = true;

            if(!preg_match("/^[-a-zA-Z0-9' ]{1,50}$/", $_POST['btitle'])){
                $errormsgs += "Your a need to fill in a blog title
                 that is no longer than 50 characters with only these
                 acceptable characters: Upper/lower case letters, spaces,
                 hyphens, and digits!";
                $valid = false;
            }
            if(!preg_match("/^[-a-zA-Z0-9<>' ]{1,500}$/", $_POST['bentry'])){
                $errormsgs += "You blog entry can't be empty or filled with
                only spaces, and can only use upper or lower case letters,
                spaces, hyphens, single quote marks, <> and digits. Lastly, it
                can't exceed 500 characters in length.";
                $valid = false;
            }

            if($valid){
    ?>
                <table border="1">
                    <font color="<?php echo $_POST['color']; ?>">
                    <tr>
                    <td>Blog Title:</td>
                    <td><?php echo $_POST['btitle']; ?></td>
                    </tr>
                    <tr>
                    <td>Blog Post:</td>
                    <td><?php echo $_POST['bentry']; ?></td>
                    </tr>
                    </font>
                </table>
    <?php
            }
            else{
                    include('top.php');
                    function selected($rchecked, $ychecked, $bchecked){
                        if ($_POST['color'] == "") $rchecked = "checked";
                        if ($_POST['color'] == "Yellow") $ychecked = "checked";
                        if ($_POST['color'] == "Blue" ) $bchecked = "checked";
                    }            
                }
        }
    ?>

(top.php)

    <?php include 'header.php' ?>
        <font color=#EEEED1>
        <form method="POST">
            <center>
                Your Blog Title:
                <input type=text name=btitle value="<?php echo $_POST['btitle'] ?>" ><?php echo $errormsgs[0]; ?><br>
                <textarea name=bentry cols="80" rows="20">
                    <?php echo isset($_POST['bentry']) ? $_POST['bentry'] : "What's on your mind?"; ?>
                </textarea><br><br>
                <?php echo $errormsgs[1]; ?>
                <table class="t1">
                    <tr><td>
                        <input type=radio name=color value="Red" <?php echo $rchecked; ?> ><font color="Red"> Red</font>
                    </td></tr>
                    <tr><td>
                        <input type=radio name=color value="Yellow" <?php echo $ychecked; ?> ><font color="Yellow"> Yellow</font>
                    </td></tr>
                    <tr><td>
                        <input type=radio name=color value="Blue" <?php echo $bchecked; ?> ><font color="Blue">  Blue</font><br><br>
                    </td></tr>
                </table>
                <input type=submit value="Create Blog!">
            </center>
        </form>
        </font>
    <?php include 'footer.php' ?>
  • 写回答

1条回答 默认 最新

  • dougan7523 2013-06-14 18:15
    关注

    your first error is in here:

    $errormsgs = []; // array
    
    
    $errormsgs += "Your ...";
    

    += operand wont work on an array,

    $a += $b; 
    

    Is the same as:

    $a = $a + $b;
    

    Used for calculations and stuff.

    I think you mean to do the following:

    $errormsgs = array();
    $errormsgs[0] =  "Your ...";
    $errormsgs[1] =  "Your ...";
    

    or

    $errormsgs = array();
    $errormsgs[] =  "Your ...";
    $errormsgs[] =  "Your ...";
    

    If you do $errormsgs[] = "text"; It will simply add another value to your array auto-incrementing the index. In this case an empty array starting with index 0 and so on.

    You can also just do $errormsgs[0] = "text"; or $errormsgs["error_1"] = "text"; If you want explicit array keys/indexes.

    Your second error

    Nothing is shown unless a post has been made, so change

    if($_POST){
        //Everything
    }
    

    to

    // initialize variables
    if($_POST){
        //Do all checks
    }
    //show form here
    

    Free extra tip:

    Try enabling error reporting so you can see an error in stead of a blank screen.

    PHP production server - turn on error messages

    You would have gotten something like:

    Fatal error: Unsupported operand types on line ###

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题