0
<?php
// post_add.php
   if( isset ($_POST['title']) && ($_POST['body']) &&  !empty($_POST) ){ 
    require 'connection.php';
    $stmt = $conn->prepare('INSERT INTO posts (title,body) VALUES (:title, :body)');


    $stmt->bindValue(':title', $_POST['title']);
    $stmt->bindValue(':body', $_POST['body']);

    $stmt->execute();


    echo 'Entry posted. <a href="post_view.php?id='.$conn->lastInsertId().'">View</a>';
   }else if(empty($_POST['title']) || empty($_POST['body']) ){
    echo "no values entered";
   }
?>

As my code is , empty fields are never submitted to my database which was my problem previously and with some research I concorted this code .Now i'm trying to inform the user that the required fields were not filled if the click the submit button before entering anything in them.My code ends up displaying my pseudo (echo) error message right away when the file is loaded, i tried redirects(which were horrible), the joys of being a PHP newbie :).I don't require answers but guidance on how to come to a solution.

Edited by pritaeas: Removed fake signature. Use the signature option in your profile settings.

3
Contributors
4
Replies
31
Views
3 Years
Discussion Span
Last Post by Eagle.Avik
1

Have you thought about using jquery or some javascript to check your input fields before sending? I would combine javascript and php methods to ensure your data is secure.

Looking at your code your need to fix a couple of things with your if statement.

Checking if $_POST is empty is not needed if you are checking for title and body within the post array.

If you give your create an error array you would be able to set error messages for individal items, like title or body. Then you could return the error array back to your form and echo out the message.

Below is on way of going about it.

if( isset($_POST) ){
    $errors = array();
    if(!isset($_POST['title'])){
        $errors['title'] = 'Error: Title is required.';
    }

    if(!isset($_POST['body']){
        $errors['body'] = 'Error: Body is required.';
    }

    if(isset($errors['title']) && isset($errors['body'])){
        foreach($errors as $error){
            echo $error . '<br />';
        }
    }else{
        //Run code below
    }
}

For myself I would combine jquery form validation and ajax to handle messages.

0

@gabrielcastillo
I know jquery and was able to find the jquery form validation plugin but i haven't delved into ajax yet....thanks

0

and by the way the validation plugin worked great.No more blank entries to my database, now to learn ajax

0

add

required="required"

this will let the browser check if the field is required and if the data is inserted or not. it is so simple, no need to write any js or jquery or php.

example field:

<input name="title" type="text" id="password" placeholder="title" value="" 

required="required">
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.