我的逻辑是尝试验证两个文本字段(具体如何,检查错误字符串),并使用一个标志,如果关闭,将用户恢复为原始表单,其最新数据仍然写入在字段和收音机框中选中它们的最后一个选项(默认值应为红色)。
一旦我尝试加载 entry.php ,它就会显示为空白。 top.php 很好,但是每当你点击提交时,它都不会去任何地方,只是保持(奇怪和随机?)缩进textarea中的默认字符串。
无限的荣誉,并提前感谢所有人!
源代码:
(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' ?>
答案 0 :(得分:0)
你的第一个错误就在这里:
$errormsgs = []; // array
$errormsgs += "Your ...";
+=
操作数无法在数组上运行,
$a += $b;
与:
相同$a = $a + $b;
用于计算和填充。
我认为您的意思是执行以下操作:
$errormsgs = array();
$errormsgs[0] = "Your ...";
$errormsgs[1] = "Your ...";
或
$errormsgs = array();
$errormsgs[] = "Your ...";
$errormsgs[] = "Your ...";
如果你执行$errormsgs[] = "text";
它只会向数组添加另一个值,自动递增索引。在这种情况下,一个以索引0开头的空数组,依此类推。
您也可以$errormsgs[0] = "text";
或$errormsgs["error_1"] = "text";
如果您想要显式数组键/索引。
您的第二个错误
除非发布了帖子,否则不会显示任何内容,因此请更改
if($_POST){
//Everything
}
到
// initialize variables
if($_POST){
//Do all checks
}
//show form here
免费额外提示:
尝试启用错误报告,以便您可以看到错误而不是空白屏幕。
PHP production server - turn on error messages
你会得到类似的东西:
致命错误:行上不支持的操作数类型###