-2

我想用 HTML 或 PHP 验证 HTML 表单。通过验证,我的意思是确保在 2 个文本框中的每一个中输入至少 1 个文本字符。并且要么只输入字母数字字符,要么确保任何标点符号不会以“/”结尾。

目前我的表单由 2 个 php 页面组成(复制如下),然后发布到一个 txt 文件。

我要么在获得一些(非常基本的)关于如何操作的说明,要么在下面的脚本中提出建议。

//HTML
<form style="" method="post" action="addtopic2.php">
Topic:<input name="topic" id="topicbox" maxlength="100" type="text"><br>
Outline: <textarea input wrap="nowrap" rows="10" cols="120" 
name="outline"></textarea>    
<br><input name="submit" value="Submit" type="submit">
</form>
//HTML

<?php
$t = "Topic:";
$o = "Outline:";

$topic = $_POST['topic'];
$outline = $_POST['outline'];

$data = "$t $topic | $o $outline |\n";

$fh = fopen("users.txt", "a");
fwrite($fh, $data); 
fclose($fh); 
?>

//尝试使用以下内容,但在插入任何一个页面时似乎都不起作用。

<?php
if($_POST['Submit'] == "submit")
{
  $errorMessage = "";
  if(empty($_POST['topic']))
  {
$errorMessage .= "<li>A topic needs to be entered</li>";
  }
  if(empty($_POST['outline']))
  {
    $errorMessage .= "<li>An outline needs to be entered</li>";
  }
  $vartopic = $_POST['formtopic'];
  $varoutline = $_POST['formoutline'];
  if(!empty($errorMessage))
  {
    echo("<p>There was an error with your form:</p>\n");
        echo("<ul>" . $errorMessage . "</ul>\n");
  }
}
?>


<?php
if (isset($_POST['topic']) && isset($_POST['outline'])) {
     $topic = trim($_POST['topic']);
    $outline = trim($_POST['outline']);
}
else {
  echo '<p>Fill the form</p>';
}
?>
4

2 回答 2

0

Try

$file = "users.txt";
$errorMessage = array ();

if ($_POST ['Submit'] == "submit") {
    if (empty ( $_POST ['topic'] )) {
        $errorMessage [] = "A topic needs to be entered<";
    }
    if (empty ( $_POST ['outline'] )) {
        $errorMessage [] = "An outline needs to be entered";
    }

    if (count ( $errorMessage ) == 0) {
        $data = $_POST ['topic'] . "|" . $_POST ['outline'] . "\n";
        $fh = fopen ( $file, "a" );
        fwrite ( $fh, $data );
        fclose ( $fh );
    } else {
        print ("<p>There was an error with your form:</p>\n") ;
        print ("<ul>") ;
        foreach ( $errorMessage as $error ) {
            print "<li>" . $error . "<li>";
        }
        print ("</ul>") ;
    }
}

// To Read Your File

$content = file ( $file );
print ("<p>File Details</p>\n") ;
foreach ( $content as $info ) {
    list ( $topic, $outline ) = explode ( "|", $info );
    print ("<ul>") ;
    print "<li>Topic: " . $topic . "<li>";
    print "<li>Outline:" . $outline . "<li>";
    print ("</ul>") ;

}
于 2012-04-10T23:22:50.797 回答
0

试试这个(单个文件)解决方案:

<form style="" method="post">

    Topic:<input name="topic" id="topicbox" maxlength="100" type="text" value='<?= $_REQUEST['topic'] ?>><br>
    Outline: <textarea input wrap="nowrap" rows="10" cols="120" name="outline"><?= $_REQUEST['outline'] ?></textarea>
    <p/>
    <input name="submit" value="Submit" type="submit">

</form>

<?php

    if ($_REQUEST['submit'] !== 'submit')
    {
        exit;
    }

    if ($_REQUEST['topic'] == '')
    {
        echo "<p>Missing Topic.</p>\n";
        exit;
    }

    if ($_REQUEST['outline'] == '')
    {
        echo "<p>Missing Outline.</p>\n";
        exit;
    }

    $t = "Topic:";
    $o = "Outline:";

    $topic = $_POST['topic'];
    $outline = $_POST['outline'];

    $data = "$t $topic | $o $outline |\n";

    $fh = fopen("users.txt", "a");
    fwrite($fh, $data); 
    fclose($fh); 
?>
于 2016-04-07T04:48:27.507 回答