Jump to content

Please Help! Parsing Form With Both Array Elements And Non-array Elements


Greywacke
 Share

Recommended Posts

hi, the form has an array of elements by name id with various identifiers. not all elements in $_POST are arrays.here is the code sofar:

$attribsarr["products_description"] = $_POST["products_description"];foreach ($_POST["id"] as $key => $val) {	// attributes	if ($key!="TEXT_PREFIX19"&&		// consumer full name	   $key!="TEXT_PREFIX21"&&		// consumer cell	   $key!="TEXT_PREFIX22"&&		// consumer email	   $key!="TEXT_PREFIX20"&&		// consumer city / town	   $key!="16"&&					// region id	   $key!="TEXT_PREFIX27") {		// comments		$attribsarr[translate($key,1)] = translate($val,0);	}}

i believe this would do the trick...any help would be appreciated, even if its a nudge in the right direction!ps: i'm guessing an if statement in there to determine if $_POST[$key] is an array could be the solution, but i have no idea how to go about it...translate($id,$keyorval) is a function which gets the name values by the given id values from the appropriate table in the zencart database.

Edited by Pierre 'Greywacke' du Toit
Link to comment
Share on other sites

yeah, did that... just a problem now, the following error shows up on some of the forms:

<b>Warning</b>: Invalid argument supplied for foreach() in <b>/home/dwtphovu/public_html/ferrety/fab/public/request_canopyxchange.php</b> on line <b>142</b><br />
this is the piece of code that gives this problem on the forms with singular options only (no label tags).
$attribsarr["products_description"] = $_POST["products_description"];foreach ($_POST["id"] as $key => $val) {		// attributes	if ($key!="TEXT_PREFIX19"&&				// consumer full name	   $key!="TEXT_PREFIX21"&&				// consumer cell	   $key!="TEXT_PREFIX22"&&				// consumer email	   $key!="TEXT_PREFIX20"&&				// consumer city / town	   $key!="16"&&							// region id	   $key!="TEXT_PREFIX27") {				// comments		if (is_array($val)) {			$tkey = translate($key,1);			$tval = array();			foreach ($_POST["id"][$key] as $key2 => $val2) {				$tval[$key2] = translate($val2,0);			}			$attribsarr[$tkey] = join(", ",$tval);			echo $tkey." = ".$tval."\n";		} else {			$attribsarr[translate($key,1)] = translate($val,0);			echo $key." = ".$val."\n";		}	}}

[ here ] the url for the problematic form. i guess i can check that the element is not a radio (if no label exists) or checkbox, see if that allows the form to be submitted - but this does not change a thing.here is the form validation script:

<!-- bof form validation --><script language="javascript"><!--var labels = document.getElementsByTagName('LABEL');for (var i = 0; i < labels.length; i++) {	if (labels[i].htmlFor != '') {		 var elem = document.getElementById(labels[i].htmlFor);		 if (elem) elem.label = labels[i];	}}function validateForm(form) {	var strFilter = /^([0-9a-z]([-.\w]*[0-9a-z])*@(([0-9a-z])+([-\w]*[0-9a-z])*\.)+[a-z]{2,6})$/i;	for (i=0; i<form.elements.length; i++) {		var elm = form.elements[i];		var val = elm.value;		if (			 elm.label &&												 // if a label exists for the element			 elm.type != "checkbox" &&											// not a checkbox and			 (														// (			 (elm.id == "attrib-22-0" && !strFilter.test(val)) ||						// (email field and invalid email) or			 val == "" ||													// empty field value or			 val == "210" || val == "217" || val == "218" || val == "153"					// any invalid dropdown options			 )														// )		   ) {				var tx = elm.label.innerHTML.replace(" :","").replace(/&/,"&");		   // get the label to alert				alert("Please fill out "+tx+" correctly.");							   // alert invalid data warning				elm.focus();											   // focus on invalid element				return false;											   // cancel formsubmit		}	}	return true;}document.cart_quantity.onsubmit = function () {	return validateForm(this);}//--></script><!-- eof form validation -->

Edited by Pierre 'Greywacke' du Toit
Link to comment
Share on other sites

hi,thing is id IS an array of elements, you can check the zencart page i linked to (this one).every form element except the submit image is part of the id array. some elements like the features checkboxes are subarrays. hence using the is_array function there. there is another hidden element (products_description) that is not part of the id array, which i add to the attributes before parsing the id array.i guess i could do an if (is_array($_POST["id"])) { } around the loop but it seems rather pointless... unless of course the form is submitting via GET!problems solved - the url of the method contained some url for some strange reason... some person didnt know how to use the zencart formbuilder it seems :)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...