Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Include config file
- require_once "include/config.php";
- // Define variables and initialize with empty values
- $title = $text = "";
- $title_err = $text_err = "";
- // Processing form data when form is submitted
- if($_SERVER["REQUEST_METHOD"] == "POST"){
- // Validate Title
- $input_title = trim($_POST["titel"]);
- if(empty($input_title)){
- $title_err = "Please enter your title.";
- } elseif(!filter_var($input_title, FILTER_VALIDATE_REGEXP, array("options"=>array("regexp"=>"/^[a-zA-Z\s]+$/")))){
- $title_err = "Please enter a valid title.";
- } else{
- $title = $input_title;
- }
- // Validate text
- $input_text = trim($_POST["tekst"]);
- if(empty($input_text)){
- $text_err = "Please enter your text.";
- } else{
- $text = $input_text;
- }
- // Check input errors before inserting in database
- if(empty($title_err) && empty($text_err)){
- // Prepare an insert statement
- $sql = "INSERT INTO blog (titel, tekst) VALUES (?, ?)";
- if($stmt = mysqli_prepare($link, $sql)){
- // Bind variables to the prepared statement as parameters
- mysqli_stmt_bind_param($stmt, "string", $param_title, $param_text);
- // Set parameters
- $param_title = $title;
- $param_text = $text;
- // Attempt to execute the prepared statement
- if(mysqli_stmt_execute($stmt)){
- // Records created successfully. Redirect to landing page
- header("location: index.php");
- exit();
- } else{
- echo "Something went wrong. Please try again later.";
- }
- }
- // Close statement
- mysqli_stmt_close($stmt);
- }
- // Close connection
- mysqli_close($link);
- }
- ?>
- <?php
- include 'include/header.php';
- ?>
- <h2>Blog aanmaken</h2>
- <p>Vul de gegevens in om de blog aan te maken.</p>
- <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
- <div class="form-group <?php echo (!empty($title_err)) ? 'has-error' : ''; ?>">
- <label>Titel</label>
- <input type="text" name="titel" class="form-control" value="<?php echo $title; ?>">
- <span class="help-block"><?php echo $title_err;?></span>
- </div>
- <div class="form-group <?php echo (!empty($text_err)) ? 'has-error' : ''; ?>">
- <label>Tekst</label>
- <textarea name="tekst" ><?php echo $text; ?></textarea>
- <span class="help-block"><?php echo $text_err;?></span>
- </div>
- <input type="submit" class="btn btn-primary" value="Submit">
- <a href="index.php" class="btn btn-default">Cancel</a>
- </form>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement