[SOLVED] What's the best way to check for an empty field and

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Seona
Forum Commoner
Posts: 33
Joined: Wed Dec 08, 2004 11:04 pm

[SOLVED] What's the best way to check for an empty field and

Post by Seona »

Hi guys,

I'm trying to check if a field is empty when a form gets submitted. If it is, I want to stop the rest of the file processing and output an error message. What I have so far is:

Code: Select all

<?php
	import_request_variables('p', 'p_');

	// if no title is entered, abort
	if ($p_SectionName = "")
		die("You must enter a Section Title.\nPlease go back and add a title");
?>
Sadly, this doesn't seem to be working. I try to submit the form with nothing in the above field and it not only doesn't show an error message but the rest of the processing happens and I get the record added to the database.

What am I doing wrong? Is die() not the function to be using here? Is my method of checking the form field wrong? Am I completely barking up the wrong tree?

Any help would be greatly appreciated.

Cheers,

Seona.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Firstly you'd want == rather than = as = is an assignment and == is a comparison (probably just a typo ;))
You probably also want to use either isset or empty for your checks. I prefer empty() but note that a zero value will pass the empty check (i've never had a form where 0 is a valid input .. yet ;))

(i've moved this to PHP Code as it's more PHP form validation related than database)
Seona
Forum Commoner
Posts: 33
Joined: Wed Dec 08, 2004 11:04 pm

Post by Seona »

Ah, that solves it. Thanks. :)

And sorry about the wrong forum thing. I wasn't paying enough attention. :oops:
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

No problem, glad it works ;)
Post Reply