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 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?