Page 1 of 1

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

Posted: Thu Jan 06, 2005 6:49 pm
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.

Posted: Thu Jan 06, 2005 6:52 pm
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)

Posted: Thu Jan 06, 2005 7:01 pm
by Seona
Ah, that solves it. Thanks. :)

And sorry about the wrong forum thing. I wasn't paying enough attention. :oops:

Posted: Thu Jan 06, 2005 7:01 pm
by markl999
No problem, glad it works ;)