Hi guys, can you please help me with my php code. i've used Javascript to validate the form and it works fine but the problem is i can't get it to stop submitting even if the form is empty

<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 = "" method = "post">
		<input type = "text" name = "address">
			<br/>
		<input type = "submit" value = "Submit" name = "submit">
</body>
</html>

That code checks to see whether the “address” field is not empty, but then displays a message to say that it is empty. Remove the ! in the second if() clause to fix the logic. If that doesn’t help, show us the form.

thanks for your assistance but its running perfectly now, my other problem is diplaying my output in the <form action = "Tson.php">
here is my entire coding for the first page:

<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 = "return validate()" action = "Tson.php" method = "post">
		<input type = "text" name = "address">
			<br/>
		<input type = "submit" value = "Submit" name = "submit">
</body>
</html>```
Now i would like to display whatever the user inputs in the first page to my second page and here is what i have so far:
```<body>
	<?php
	$Tsontso = $_POST['address'];
		echo "Your Name is: $Tsontso";
	?>
</body>```
Please correct me where i made a mistake....
Thank you in advance

Does it help if you close the form properly with </form> rather than just ending the page? I’d like to think it would deal with it, but it’s better practice to explicitly close the form after the final <input>.

ETA: Exactly as written, your code does what I’d expect it to on my test system, I type in “hello” in the text input, press “submit”, and the second code says “Your Name is: hello”.

What happens for you?

it doesn’t display anything

If it doesn’t display anything at all, including the text string “Your Name is:”, that implies the destination script is not running. Could it be a case-sensitive file system, and you’ve called the destination “tson.php” but are trying to call “Tson.php”?

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