PHP field validation

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?

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

One very simple way would be to do this:

$blah = $_POST['input_blah'];

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

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?

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:


	<title>Form Test</title>


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

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

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

		function submitForm(e) {

			if (nameField.value == "") {
				alert("Value empty!");
			} else {
				// form submission will just happen


Hope this helps :stuck_out_tongue:

You’re the best! Thank you.:+1: