How to stop a form submitting even when empty

Hi guys, can you please help me with this code. my form keeps on submitting even if the form is empty. Thanks in advance

<?php
		if(isset($_POST["submit"])){
			if(!empty($_POST['address'])){
				echo "The form is empty";
				return false;
			}
		}
	?>
	<script>
		function validate(){
			var Tsontso = document.forms["Otris"]["address"].value;
				if(Tsontso == ""){
					alert("Please fill in the form");
					return false;
				}
				else{
					var Tsontso = document.forms["Otris"]["address"].value == "add";
				}
		}
	</script>
1 Like

Please show the html code also. Are you doing by this way

// All Elements Goes here

If you’re posting code, you need to precede it with a line containing only three backticks ``` and end it with a line contining only three backticks.

<html>
<head>
	<?php
		if(isset($_POST["submit"])){
			if(!empty($_POST['address'])){
				echo "The form is empty";
				return false;
			}
		}
	?>
	<script>
		function validate(){
			var Tsontso = document.forms["Otris"]["address"].value;
				if(Tsontso == ""){
					alert("Please fill in the form");
					return false;
				}
		}
	</script>
</head>
<body>
	<form name = "Otris" onsubmit = "validate()" action = "Tson.php" method = "post">
		<input type = "text" name = "address">
			<br/>
		<input type = "submit" value = "Submit" name = "submit">
</body>
</html>

Use this at form

onsubmit=“return validate();” instead of onsubmit=“validate()”

And one more important thing is please close your form using

 

Thank you for your assistance, it’s running perfectly now

Until someone uses it with JavaScript turned off!

2 Likes

can you also help me display the on to the <form action = "Tson.php">
here is my code for trying to display the input:
Hello<?php echo $_POST["address"]; ?>

As i said previously do you closing the form using

. Because as per your code your are not doing that. I also like to advice you that when you ask any particular person in that case you should mention the name of that person.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.