# PHP field validation

**URL:** <https://forum.kirupa.com/t/php-field-validation/636291>\
**Category:** programming\
**Created:** [March 24, 2017, 3:43pm UTC](https://forum.kirupa.com/t/php-field-validation/636291 "2017-03-24T15:43:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![junkerjorg](https://avatars.discourse-cdn.com/v4/letter/j/9d8465/32.png) [@junkerjorg](https://forum.kirupa.com/u/junkerjorg)\
**Post date:** [March 24, 2017, 3:43pm UTC](https://forum.kirupa.com/t/php-field-validation/636291/1 "2017-03-24T15:43:13Z")

</div>

I have a very simple contact us form but there is no validation to make certain the fields are not blank, thus I am getting blank emails from the form. Can someone point me to a solution I can easily fit into my existing code?

---

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [March 25, 2017, 3:16am UTC](https://forum.kirupa.com/t/php-field-validation/636291/2 "2017-03-25T03:16:28Z")

</div>

Can you post your PHP code or tell us what version of PHP you are targeting? 🙂

One very simple way would be to do this:

```
$blah = $_POST['input_blah'];

if (empty($blah)) {
    echo 'Field can't be empty!';
    return false;
}
```

---

<div class="post-metadata">

**Author:** ![junkerjorg](https://avatars.discourse-cdn.com/v4/letter/j/9d8465/32.png) [@junkerjorg](https://forum.kirupa.com/u/junkerjorg)\
**Post date:** [March 27, 2017, 12:22am UTC](https://forum.kirupa.com/t/php-field-validation/636291/4 "2017-03-27T00:22:48Z")

</div>

Kirupa, I just tried your method and it worked wonderfully, thank you! One other question, is there a way to make the echo result appear on the same page instead of opening a new page with the error?

---

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [March 27, 2017, 2:24am UTC](https://forum.kirupa.com/t/php-field-validation/636291/5 "2017-03-27T02:24:03Z")

</div>

For displaying the error on the same page, you can do that entirely in JavaScript and not have to deal with the server at all. The exact details will vary, but you can see one approach below:

```
<html>

<head>
	<title>Form Test</title>
</head>

<body>

	<div id="textContainer">
		<form action="#">
			<input id="nameField" type="text"></input>
			<input id="submitButton" type="Submit"></input>
		</form>
	</div>

	<script>
		var nameField = document.querySelector("#nameField");
		var submitButton = document.querySelector("#submitButton");

		submitButton.addEventListener("click", submitForm, false);

		function submitForm(e) {

			if (nameField.value == "") {
				alert("Value empty!");
				e.preventDefault();
			} else {
				// form submission will just happen
			}
		}
	</script>
</body>

</html>

```

Hope this helps 😛

---

<div class="post-metadata">

**Author:** ![junkerjorg](https://avatars.discourse-cdn.com/v4/letter/j/9d8465/32.png) [@junkerjorg](https://forum.kirupa.com/u/junkerjorg)\
**Post date:** [March 27, 2017, 3:06am UTC](https://forum.kirupa.com/t/php-field-validation/636291/6 "2017-03-27T03:06:23Z")

</div>

You’re the best! Thank you.👍
