0

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' ?>
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141

1 Answers1

0

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

Community
  • 1
  • 1
Timmetje
  • 7,641
  • 18
  • 36
  • Will this logic separate both strings into two indexes if need be? eg. errormsgs[0] and errormsgs[1] –  Jun 14 '13 at 18:18
  • Yes it does, added some explanation about that in my answer, good luck on learning PHP! and psshh dont use font tags ;) and do accept this answer! :P – Timmetje Jun 14 '13 at 18:21
  • Unfortunately, it's still drawing a blank for me with errors turned on and everything :\ –  Jun 14 '13 at 18:34
  • Oh and everything is between `if($_POST){}` So it doesn't show anything unless a post has been made. added example to answer – Timmetje Jun 14 '13 at 18:43