2016-12-12 39 views
0

我想在用戶提交數據時使用自定義函數來顯示內聯錯誤。這些錯誤位於名爲formErrors的數組中,並放置在functions.php中。我檢查了日誌,並且在頁面加載或數據提交時沒有顯示錯誤。這是的functions.phpPHP自定義函數返回數組值

function formErrors() 
{ 
    $formErrors = array(); 
    $formErrors['joke_text'] = ''; 
    $formErrors['author'] = ''; 
    return $formErrors; 
} 

function listAuthors() 
{ 
    global $dbConnection; 
    // get list of authors 
    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(); 
    } 

    // add values into array 
    foreach ($result as $row) 
    { 
     $authors_in_db[] = array(
     'id' => $row['id'], 
     'name' => $row['name'] 
     ); 
    } 

    //Call the form script 
    include 'form.php'; 
    exit(); 
} 

在index.php文件就是$ formErrors數組用於:

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

# add joke link pressed 
if (isset($_GET['add_joke'])) 
{ 
    $formErrors = formErrors(); 
    listAuthors(); 

} 

# add joke to the database 
if (isset($_GET['add_joke_to_db'])) 
{ 
    # check for empty fields 
    if (trim(($_POST['joke_text'] == ''))) 
    { 
     $formErrors['joke_text'] = '* Joke text field cannot be empty.'; 
    } 
    if (trim(($_POST['author'] == ''))) 
    { 
     $formErrors['author'] = '* Author field cannot be empty.'; 
    } 

    # if errors found, show the form again 
    if (!empty($formErrors)) 
    { 
     listAuthors(); 
    } 
    else 
    # else if no errors found, 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(); 
    } 
} 
?> 

我覺得是$ formErrors [ 'joke_text'] & $ formErrors錯誤['作者']但不確定如何讓它檢查空字段。

這如果您提交相同的頁面是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"></textarea> 
       <?php if(isset($formErrors)){ ?> 
        <span class="error"> 
       <?php echo $formErrors['joke_text'];?></span><?php } ?> 
      </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($formErrors)){ ?> 
        <span class="error"><?php echo $formErrors['author'];?></span> <?php } ?> 
      </div> 
      <div> 
       <input type="submit" value="Add"> 
      </div> 
     </form> 
    </body> 
</html> 
+0

我不明白你的問題?發生了什麼?爲什麼把'exit();'放在代碼中的幾個地方? –

回答

1

這隻作品。如果你想顯示其他頁面上的錯誤,那麼你必須使用$ _SESSION來傳遞數據。也被稱爲Flash消息