Jump to content

checking if forms are filled on submission


ryanmetzler3
Go to solution Solved by gristoi,

Recommended Posts

Here is my html form and my php code. It is supposed to check if any of the forms have not been filled and echo a message if they have not. Then process the registration if they have. But it seems to ignore the blank spots. If I don't fill in fields it just processes the blank registration anyways. Where did I go wrong?

<?php
include ('config.php');
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
	$username = mysql_real_escape_string($_POST['username']);
	$password = mysql_real_escape_string(md5($_POST['password']));
	$firstname = mysql_real_escape_string($_POST['firstname']);
	$lastname = mysql_real_escape_string($_POST['lastname']);
	$email = mysql_real_escape_string($_POST['email']);

if (!isset($username) || !isset($password) || !isset($firstname) || !isset($lastname) || !isset($email) ) {
	echo "You did not fill out the required fields.";
} else {
		$query = mysql_query("SELECT * FROM users WHERE username = '$username'");
		$rows = mysql_num_rows($query);

		if ($rows > 0) {
			die("Username taken!");
		} else {
			$user_input = mysql_query("INSERT INTO users (username, password, firstname, lastname, email) VALUES ('$username','$password','$firstname','$lastname','$email')");	
			echo("Succesfuly Registered!");
		}
	}
}

?>


<html>
	
	<head>
		<title>Register</title>
	</head>
	
	<body>
		<form action="register.php" method="post">
			First Name: <input type="text" name="firstname"><br/>
			Last Name: <input type="text" name="lastname" /><br/>
			Email: <input type="text" name="email" /><br/>
			Username: <input type="text" name="username" /><br/>
			Password: <input type="password" name="password"/><br/>
			<input type="submit" value="Register!"/>	
		</form>
	</body>
	
</html>
Link to comment
Share on other sites

What you're doing to process the form is only checking if those specific variable names are set which you have done.

$username = mysql_real_escape_string($_POST['username']);

So your code checks to see if $username is set, which you have just done above, even if the $_POST data for username is empty, $username is an empty string.

 

So instead of using isset(), you'll want to replace all that with empty() in your argument. e.g

if(empty($username)) { echo 'Username Empty'; }

Link to comment
Share on other sites

try this..... doRegistration is the function on submit button
 

function doRegistration()   {   if(first_name==""&&last_name==""&&user_name==""&&e_mail==""&&pass_word1==""&&pass_word2=="")   {   alert("Please Complete The Form Before Submission")   return false;   }   else   {   return true;   }
Link to comment
Share on other sites

  • Solution

 

try this..... doRegistration is the function on submit button

 

function doRegistration()   {   if(first_name==""&&last_name==""&&user_name==""&&e_mail==""&&pass_word1==""&&pass_word2=="")   {   alert("Please Complete The Form Before Submission")   return false;   }   else   {   return true;   }

 

 

I am afraid kashiflatif90 is incorrect. that if statement will only ever be true if ALL fields are empty on submission . you should use || instead of &&

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.