PHP Form error message

I am learning PHP forms. I created form and implemented validation that field should not be empty.

Its working but error should once user click on submit. But issue is that error is visible always. I am using godaddy as server. Attached is screenshot

We cannot help you with what is wrong with your code unless you post the actual code in the forum (no pictures.)

To post code, either add bbcode [code][/code] tags or three markdown back-ticks ``` on their own lines before and after your code.

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>This is contact form</title>
	<style>
		body
		{
			background: #2E8B57;
			color: #fff;
		}

		.container
		{
			max-width: 700px;
			width: 100%;
			margin: 0 auto;
		}

		input.femail 
		{
    		margin: 15px 0px;
		}

		textarea.fmessage 
		{
    		margin: 15px 0px;
		}
	</style>
</head>
<body>


	<?php
		if ($_SERVER['REQUEST_METHOD'] == "POST") 
		
		{

			if (empty($_POST["name"])) 
			{
    			echo "Name is required";
  			} 
  			else 
  			{
    			$name = test_input($_POST["name"]);
  			}
			




		}

	?>


	<div class="container">

	<h1>Fill the form below and we will call you back</h1>
	<form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>">
		Nane:<input type="text" name="name" class="fname"><br>

		Email:<input type="text" name="email" class="femail"><br>
		Subject:<input type="text" name="subject" class="fsubject"><br>
		Message:<textarea name="comment" rows="5" cols="40" class="fmessage"></textarea><br>
		<input type="submit" value=Send class="fsub">
	</form>
</div>
</body>
</html>

What does the ‘view source’ in your browser show?

If it shows the raw php code, it means that either you didn’t request the page through a URL on a web server, i.e. directly opened the page through the file system in your browser, or that php is not installed/working on that web server and that whatever browser/client you are using rendered the string in the raw php code as html to be output on the page (I just tested in three different browsers and none of them did this.)

If it doesn’t show the raw php code, then either you requested the page using a post method request, perhaps from a form on some other page, or you have a version of the php code that only has one = (an assignment operator) in the comparison with the "POST" value.

BTW - don’t do this -

This is junk code either directly or indirectly from w3schools. It is validating the raw post value, which could contain all white-space characters, then uses the result from the test_input() function, which trims the value, throughout the rest of the code (the other things the test_input function does are improper for input data - delete this function from your code and don’t ever use it). This will allow someone to submit white-space character(s) that will then get converted to an empty string. Do you really think that your code should attempt to use an empty string?

Instead, your code should trim all the input data at once, using one single php statement, by keeping the post data as a set, in an array variable, then accessing elements in this trimmed working copy of the post data throughout the rest of the code.

Don’t do this as well -

To get a form to submit to the same page, simply leave out the entire action="…" attribute.

1 Like
Sponsor our Newsletter | Privacy Policy | Terms of Service