Do not allow blank field

Hello,

Is there a code that will prevent or notify the encoder not to allow blank field?

Please advise. Thanks.

The stuff in bold is confusing me so.

Are you asking if there is a way PHP can check for blank form fields?

Sorry for the wrong grammar.

Let me say this way.

Example

Name: _____
Address: ______
Phone Number: _____

If there are any field that is black or not filled up. It will return an error like this.

Error:

  • Name field is empty
  • Address field is empty
  • Phone number field is empty

I hope this is clear. Thank you.

Yes. Instead of using java script.

Easy :slight_smile:

All you have to do is check if the input element is empty by calling its ID from the $_POST array.

So if you have a form field control such as:

<input type="text" name="personName" id="fullName" />

And the person clicks a submit button with the following code below:

<intput type="submit" name="sendData" id="checkEmpty" />

All one has to do is:

[php]

<?php // If form is submitted (sending data to server) if(isset($_POST['checkEmpty'])) { // Check if control fullName is not set or is empty if(!isset($_POST['fullName']) || $_POST['fullName'] === "") { //Print out a message to the user that a field is empty print("We can't greet you properly if we don't know your name!"); } } [/php]

Here is my code.

In here I need the system not to accept blank field. This also include the publisher must have a content.

[code]<?php
$errorMessage = ‘’;
if (isset($_POST[‘submit’])){
include ‘db.php’;//Connect to database
$title=$_POST[‘title’] ;
$author= $_POST[‘author’] ;
$name=$_POST[‘name’] ;
$copy=$_POST[‘copy’] ;
$title_search = mysql_query(“SELECT Title FROM books WHERE Title=’$title’ AND Author=’$author’”);
if(mysql_num_rows($title_search) != 0) {
$errorMessage = ‘Record Already Exist!
’;
$errorMessage = $errorMessage . ‘Testing another error message.’;
}else {
$ins = mysql_query(“INSERT INTO books(Title, Author, PublisherName, CopyrightYear) VALUES (’$title’, ‘$author’, ‘$name’, ‘$copy’)”);
if($ins) { // make sure the insert was sucessfull before clearing data
unset($_POST[‘title’]);
unset($_POST[‘author’]);
unset($_POST[‘name’]);
unset($_POST[‘copy’]);
}
}
}
mysql_close($conn);
?>

Books <?php if ($errorMessage != ''){echo $errorMessage;} ?>
Title:
Author
Publisher Name - Select - <?php include 'db.php';
					$sql = mysql_query("SELECT names FROM publisher");

					while($row1 = mysql_fetch_array($sql)){
						if($row1[0] == $_POST['name']) {
						echo "<option value='$row1[0]' selected>$row1[0]</option>\n";
						} else {
						echo "<option value='$row1[0]'>$row1[0]</option>\n";
						}
					}
				?>
			</select>
		</td>
	</tr>
	<tr>
		<td>Copyright Year</td>
		<td><input type="text" name="copy" value="<?php echo isset($_POST['copy']) ? $_POST['copy'] : '' ?>" /></td>
	</tr>
	<tr>
		<td>&nbsp;</td>
		<td><input type="submit" name="submit" value="add" /></td>
	</tr>
</table>
<?php include("db.php"); $result=mysql_query("SELECT * FROM books"); while($test = mysql_fetch_array($result)){ $id = $test['BookID']; echo ""; echo""; echo""; echo""; echo""; echo""; echo""; } mysql_close($conn); ?>
" .$test['BookID']."" .$test['Title']."". $test['Author']. "". $test['PublisherName']. "". $test['CopyrightYear']. " Edit"; echo" Delete"; echo "
[/code]

I successfully made it to work. Just made it simple.

see the code below

[php] if( $_POST[foperator] == ‘’)
{
$errorMessage = $errorMessage . ‘Operator field is required! Who handled this lot?
’;
}

if( $_POST[fprocess] == '0')
	{
	$errorMessage = $errorMessage . '<font color="red">Please select what process this lot came from</font><br>';
	}	

if( $_POST[fmodel] == '0')
	{
	$errorMessage = $errorMessage . '<font color="red">Please select what model is the lot. If the model do not exist, please add it using <b>Add model name menu</b></font><br>';
	}	

if( $_POST[flotno] == '')
	{
	$errorMessage = $errorMessage . '<font color="red">Lot Number cannot be blank!</font><br>';
	}	

if( $_POST[finput] == '' or $_POST[finput] == 0)
	{
	$errorMessage = $errorMessage . '<font color="red">Input field must have a value and not zero or blank!</font><br>';
	}	
	[/php]
Sponsor our Newsletter | Privacy Policy | Terms of Service