Jump to content

checking if forms are filled on submission


ryanmetzler3

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>

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'; }

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;   }

 

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 &&

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 &&

 

thanks @gristoi for finding error... :), also you should help me on finding Card Integration Code 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.