Wie zu validieren, Formular mit php und zeigen Sie den Fehlertext in html

Ich soll zu validieren, Formular mit php und dann machen Sie die Eingabe speichern im json-Datei. Ich benutzte span class echo die Fehlermeldung aus writer.php zu html. Aber es ist nicht das echo der Fehlertext in html, es bezeichnet writer.php mit "leere Seite".

<head>
    <title>Data Buku</title>
    <link rel="stylesheet" type="text/css" href="style.css">
    <link href='http://fonts.googleapis.com/css?family=Ribeye+Marrow' rel='stylesheet' type='text/css'>
    <link href='http://fonts.googleapis.com/css?family=Comfortaa' rel='stylesheet' type='text/css'>
</head>

<body>
<div class="center">
    <h1>Data Buku</h1>

        <p><span class="error">* required field.</span></p>

        <hr>

        <form action="writer.php" method="post">
            <span class="error"><?php echo $error;?></span>
            <h2>Informasi Pengarang</h2>
            Nama: <br><input type="text" name="nama" id="nama" />
            <span class="error">* <?php echo $namaErr;?></span><br>
            Nomor Telepon: <br><input type="text" name="telepon" id="telepon" />
            <span class="error">* <?php echo $nomorErr;?></span><br>
            e-Mail: <br><input type="email" name="email" id="email" />
            <span class="error">* <?php echo $emailErr;?></span><br>

            <h2>Tulisan</h2>
            Judul: <br><input type="text" name="judul" id="judul" />
            <span class="error">* <?php echo $judulErr;?></span><br>
            Konten: <br><textarea name = "konten" rows="6" cols="50" id="konten"></textarea>
            <span class="error">* <?php echo $kontenErr;?></span><br>
            <input type="submit" id="submit" name = submit value="Create" />
            <input type="reset" id="reset" value="Reset" />
        </form>
</div>      
</body>

Dies ist meine php-Datei

<?php
    //Append new form data in json string saved in json file

    //path and name of the file
$filetxt = 'dataInJson.json';

    //error message
$namaErr = "";
$nomorErr = "";
$emailErr = "";
$judulErr = "";
$kontenErr = "";

    //check if all form data are submited, else output error message
    if(isset($_POST['submit'])) {
    //if form fields are empty, outputs message, else, gets their data
    if(empty($_POST['nama'])) {
        $namaErr = "Write your name";
    }
    if(empty($_POST['telepon'])){
        $nomorErr = "Write the phone number";
    }
    if(empty($_POST['email'])){
        $emailErr = "Write the email";
    }
    if(empty($_POST['judul'])){
        $judulErr = "Write the title";
    }
    if(empty($_POST['konten'])) {
        $kontenErr = "Write the content";
    }
    else {

    //gets and adds form data into an array
    $data = array(
      'nama'=> $_POST['nama'],
      'telepon'=> $_POST['telepon'],
      'email'=> $_POST['email'],
      'judul'=> $_POST['judul'],
      'konten'=> $_POST['konten'],
    );

    //path and name of the file
    $filetxt = 'dataInJson.json';

    $arr_data = array();        //to store all form data

    //check if the file exists
    if(file_exists($filetxt)) {
      //gets json-data from file
      $jsondata = file_get_contents($filetxt);

      //converts json string into array
      $arr_data = json_decode($jsondata, true);
    }

    //appends the array with new form data
    $arr_data[] = $data;

    //encodes the array into a string in JSON format (JSON_PRETTY_PRINT - uses whitespace in json-string, for human readable)
    $jsondata = json_encode($arr_data, JSON_PRETTY_PRINT);

    //saves the json string in "dataInJson.json"
    //outputs error message if data cannot be saved
    if(file_put_contents('dataInJson.json', $jsondata)){
        echo '<script type="text/javascript">alert("Data has been submitted");</script>';
    }
    else{
        echo 'Tidak dapat menyimpan data di "dataInJson.json"';
    }
  }
}
    else echo 'Form fields not submited';
?>
  • Sind diese auf der gleichen Seite?
  • Nein, es gibt zwei Seiten, html und php
  • Ok, ich kombinierte Sie zu einem so die Fehler arbeiten
InformationsquelleAutor user3396 | 2014-11-04
Schreibe einen Kommentar