Page 1 of 1

PHP/MYQL newbie needs help

Posted: Thu Feb 05, 2004 10:42 am
by Matta
If I have a form with lots of fields, how can I write code that will check each one to make sure it is entered, and if not, print the error, and if so, run the query? Here's what I have:
if (empty($_POST['field1']) || empty($_POST['field2'])) {
$f1 = FALSE;
$f2 = FALSE;
$message .= '<p>You must complete every part of the form.</p>';

} else {
$f1= escape_data($_POST['field1']);
$f2 = escape_data($_POST['field2']);
}
if ($f1 && $f2) {

$query = "INSERT INTO tablename (field1,field2) VALUES ('$f1', '$f2')";
$result = @mysql_query ($query);
}
This is ok if I only have one or two fields, but if I have 30 or so, all the copy/pasting can get tedious. Is there a function that can find out what all the field names in my form are, set variables to them (I'm not sure if that part is even necessary), make sure they're all filled out, and put them into the table?
Thanks.

Posted: Thu Feb 05, 2004 11:04 am
by mark-s
You could use javascript. This function checks all text & file upload fields in the form & displays a messagebox with the missing fields displayed. put this between your <head></head> tags

Code: Select all

<script type="text/javascript">
	<!-- Hide the script from older browsers
	function isblank(s)
	&#123;
		for(var i=0;i<s.length;i++)
		&#123;
			var c = s.charAt(i);
			if((c != ' ')&&(c !='\n')&&(c != '\t')) return false;
		&#125;
		return true;
	&#125;
	
	function verify(f)
	&#123;
		var msg;
		var empty_fields = "";
		var errors = "";
		for(var i=0;i<f.length;i++)
		&#123;
			var e = f.elements&#1111;i];
			if(((e.type == "text")||(e.type == "file"))&& !e.optional)
			&#123;
				if((e.value == null)||(e.value == "")|| isblank(e.value))
				&#123;
					empty_fields += "\n		" + e.name;
					continue;
				&#125;
			&#125;
		&#125;
	
	if(!empty_fields && !errors) return true;
	
		msg = "______________________________________________________\n\n"
		msg += "The update was not submitted because of the following error(s).\n";
		msg += "Please correct there error(s) and re-submit.\n";
		msg += "______________________________________________________\n\n"
	
	
	if (empty_fields)
	&#123;
		msg += "-The following required fields are empty:"
			+ empty_fields + "\n";
	
		if(errors) msg += "\n";
	&#125;
	msg += errors;
	alert(msg);
	return false;
	&#125;
	// end hiding -->
	</script>
Then, put this in the first <form> tag.

Code: Select all

onSubmit="return verify(this)"

Posted: Thu Feb 05, 2004 11:29 am
by kettle_drum
Or if you still want to have the user submit the form and for php to check it, you could easily make your own function to check that required fields were filled in. Just have it except an array of the field names that are required, then the function just needs to loop through the array and check to see that there is a value in it:

Code: Select all

function field_check($required){
   foreach($required as $q){
      if(empty($_POST[$q])){
         return "Error: The following field was not filled in: $q";
      }
   }
}
If the function doesnt return anything then you know that all the required fields were filled in. Easy :)

Posted: Thu Feb 05, 2004 1:16 pm
by Matta
Thanks!