PHP表单内联验证

PHP表单内联验证,php,forms,validation,Php,Forms,Validation,嗨,我在通过服务器端验证创建内联表单时遇到了一个小问题。我有两个文件,index.php,我用它作为控制器向数据库添加数据,form.php显示表单。这就是我所拥有的: Index.php <?php include '../includes/dbconn.php'; # add joke link pressed if (isset($_GET['add_joke'])) { // Build the list of authors for drop-down list try {

嗨,我在通过服务器端验证创建内联表单时遇到了一个小问题。我有两个文件,index.php,我用它作为控制器向数据库添加数据,form.php显示表单。这就是我所拥有的:

Index.php

<?php
include '../includes/dbconn.php';

# add joke link pressed
if (isset($_GET['add_joke']))
{
// Build the list of authors for drop-down list
try
{
    $result = $dbConnection->query('SELECT id, name FROM author');
}
catch (PDOException $e)
{
    $error = 'Error fetching list of authors.' . '<br />' . $e -> getMessage();
    include '../includes/error.php';
    exit();
}

foreach ($result as $row)
{
    $authors_in_db[] = array(
    'id' => $row['id'], 
    'name' => $row['name']
    );
}
include 'form.php';
exit();
}

# add joke to the database
if (isset($_GET['add_joke_to_db']))
{   

# continue with adding joke to the database
try
{
    $sql = 'INSERT INTO joke SET
    joke_text = :joke_text,
    joke_date = CURDATE(),
    author_id = :author_id';

    $s = $dbConnection -> prepare($sql);
    $s -> bindValue(':joke_text', $_POST['joke_text']);
    $s -> bindValue(':author_id', $_POST['author']);
    $s -> execute();
}
catch (PDOException $e)
{
    $error = 'Error adding joke.' . '<br />' . $e -> getMessage();
    include '../includes/error.php';
    exit();
}

header('Location: .');
exit();
}

# delete joke from the database
if (isset($_GET['delete_joke']))
{
try
{
    $sql = 'DELETE FROM joke WHERE id = :id';
    $s = $dbConnection -> prepare($sql);
    $s -> bindValue(':id', $_POST['id']);
    $s -> execute();
}
catch (PDOException $e)
{
    $error = 'Error deleting joke.' . '<br />' . $e -> getMessage();
    include '../includes/error.php';
    exit();
}

header ('Location: .');
exit();
}

# select all jokes from the database
try
{
$sql = 'SELECT joke.id, joke.joke_text, joke.joke_date, author.name,    author.email 
FROM joke INNER JOIN author
ON author_id = author.id';
$result = $dbConnection -> query($sql);
}
catch (PDOException $e)
{
$error = 'Error fetching jokes.' . '<br />' . $e -> getMessage();
include '../includes/error.php';
exit();
}

# add each data item within an array
foreach ($result as $row)
{
$jokes_in_db[] = array(
'joke.id' => $row['id'], 
'joke.joke_text' => $row['joke_text'], 
'joke.joke_date' => $row['joke_date'],
'author.name' => $row['name'],
'author.email' => $row['email']
);
}
包括“笑话.php”; ?>

和form.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Add Joke</title>
    <link rel="stylesheet" type="text/css" href="../includes/styles.css" />
</head>
<body>
    <h1>Add Joke</h1>
    <form action="?add_joke_to_db" method="post">
        <div>
            <label for="joke_text">Type your joke here:</label>
            <textarea id="joke_text" name="joke_text" rows="3" required></textarea>
            <span class="error">* <?php echo $joke_textError;?></span>
        </div>
        <div>
            <label for="author">Author:</label>
            <select name="author" id="author">
                <option value="">Select one</option>
                <?php foreach ($authors_in_db as $data): ?>
                <option value="<?php echo htmlspecialchars($data['id'], ENT_QUOTES, 'UTF-8'); ?>">
                    <?php echo htmlspecialchars($data['name'], ENT_QUOTES, 'UTF-8'); ?>
                </option>
                <?php endforeach; ?>
            </select>
            <span class="error">* <?php echo $author_textError;?></span>
        </div>
        <div>
            <input type="submit" value="Add">
        </div>
    </form>
</body>
下面是为笑话文本设置错误消息的示例

Form.php


您必须对所有错误消息(如描述、笑话等)执行此逻辑。

您的问题是什么?请更具体一点!巴尼,说得更清楚些。如何获得一条显示笑话文本是否丢失的内联错误消息。它的工作原理是:如果isset$\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\我遇到的问题是,使用exit将进一步停止脚本的执行。仅供参考:exit的作用是它将从此处停止执行。只有在完成所需的整个过程或出现错误并希望终止执行后,才应使用exit!仍然不确定如何使此工作:[code]如果author/joke是空的,如果$\u POST['joke\u text']===|$\u POST['author']={$textError='您必须将文本添加到此字段。;包括'form.php';}[/code]是的!但我不喜欢您将其作为最终答案删除!:p只是开玩笑
if (isset($_GET['add_joke_to_db']))
{   

$e_joke = 0;
$author = 0;
# continue with adding joke to the database
try
{
    if($_POST['joke_text'] == '')
    {
       $e_joke = 1;
    }
    if($_POST['author'] == '')
    {
       $author = 1;
    }
    if($e_joke == 0 && $author == 0){
    $sql = 'INSERT INTO joke SET
    joke_text = :joke_text,
    joke_date = CURDATE(),
    author_id = :author_id';

    $s = $dbConnection -> prepare($sql);
    $s -> bindValue(':joke_text', $_POST['joke_text']);
    $s -> bindValue(':author_id', $_POST['author']);
    $s -> execute();
  }
 else
 {
  include '../includes/error.php?joke='.$e_joke.'&auther='.$author;
 }
}
catch (PDOException $e)
{
    $error = 'Error adding joke.' . '<br />' . $e -> getMessage();
    include '../includes/error.php';
    exit();
}

header('Location: .');
exit();
}
       <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Add Joke</title>
        <link rel="stylesheet" type="text/css" href="../includes/styles.css" />
    </head>
    <body>
        <h1>Add Joke</h1>
        <form action="?add_joke_to_db" method="post">
            <div>
                <label for="joke_text">Type your joke here:</label>
                <textarea id="joke_text" name="joke_text" rows="3" required></textarea>
                <?php
                if(isset($_GET["joke"]) && $_GET["joke"]==1)
                { ?>
                <span class="error">* <?php echo "Joke text is missing";?>
               <?php } ?>
</span>
            </div>
            <div>
                <label for="author">Author:</label>
                <select name="author" id="author">
                    <option value="">Select one</option>
                    <?php foreach ($authors_in_db as $data): ?>
                    <option value="<?php echo htmlspecialchars($data['id'], ENT_QUOTES, 'UTF-8'); ?>">
                        <?php echo htmlspecialchars($data['name'], ENT_QUOTES, 'UTF-8'); ?>
                    </option>
                    <?php endforeach; ?>
                </select>
                <?php
                if(isset($_GET["auther"]) && $_GET["auther"]==1)
                { ?>
                <span class="error">* <?php echo "Select Auther";?>
               <?php } ?></span>
            </div>
            <div>
                <input type="submit" value="Add">
            </div>
        </form>
    </body>