2

I have a form to get information and when i type just spaces with no any other character it gets posted, How could i avoid inserting just spaces in the form?

<form action="index.php" method="post">
    <textarea name="textA"></textarea>
    <input type="submit" name="sent" value="Send">
</form>
<?php
    if(isset($_POST['sent']) && !empty($_POST['textA'])){
        $insert=new Insert();
        $insert->insertData($_POST['textA']);
    }
?>

1 Answer 1

6

Just use trim() which will remove all leading and trailing spaces. If there are only spaces in the string then it will become an empty string and empty() will be true.

if(isset($_POST['sent']) && !empty(trim($_POST['textA']))){

You can also do things like str_replace() to replace spaces with and empty string or preg_replace() to do the same but this should do what you need.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.