2016-11-30 14 views
-1

Hallo Ich habe ein kleines Problem bei der Erstellung eines Inline-Formulars über serverseitige Validierung. Ich habe zwei Dateien, index.php, die ich als Controller verwende, um Daten zur Datenbank hinzuzufügen und form.php, um das Formular anzuzeigen. Das ist, was ich habe:PHP Formular Inline-Validierung

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'] 
); 
} 

umfassen 'jokes.php'; ? >

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

+0

Was ist Ihre Frage? Bitte sei spezifischer! – Akshay

+0

Barney, um es klarer zu machen. Wie bekomme ich eine Inline-Fehlermeldung, die anzeigt, ob der Joke_text fehlt? –

Antwort

0

Hier ist das Beispiel Fehlermeldung für joke_text einzustellen;

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(); 
} 

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

Sie haben diese Logik für alle Fehlermeldung zu tun, wie Beschreibung, Witz usw.

+0

es funktioniert irgendwie: # Witz auf die Datenbank if (isset ($ _ GET [ 'add_joke_to_db'])) {\t \t # hinzufügen, wenn Autor leer ist \t if ($ _POST [ 'joke_text'] == ' ') \t { \t \t $ joke_textError =' Sie müssen diesem Feld Text hinzufügen. '; \t \t enthalten 'form.php'; \t \t exit(); \t} Das Problem, das ich bekomme, ist, dass die Verwendung von Exit die Ausführung des Skripts weiter anhalten wird. –

+0

FYI: was "exit" tun wird ist, wird die Ausführung von dort stoppen – Akshay

+0

Sie sollten 'exit' nur verwenden, nachdem Sie Ihren gesamten erforderlichen Prozess abgeschlossen haben oder Sie Fehler erhalten und Sie die Ausführung beenden möchten! – Akshay