/    Sign up×
Community /Pin to ProfileBookmark

Validation array from mySQL table

I have built my own simple shopping cart using HTML form and PHP code. I have a mySQL database table of products with 3 databae felds – product name, product price and product type.

Customers can select multiple products to add to their shopping cart, and if one of the selected products has a special product type then an extra form field must be completed.

So when a customer selects products my PHP code selects all 3 product fields from the table and inputs the product type and product price to the shopping cart (i.e. stores them for later checkout) and I then store the product type in an array as a hidden form field e.g.
<input type=”hidden” name =”producttype[ ]” value=”value=”<?php echo $producttype; ?>””>

I then want to apply a javascript validation to ensure that the extra form field is selected whenever a slected product has a certin product type, something like this:

var producttype = theForm.elements[‘producttype[ ]’];

for (var i=0; i<producttype.length; i++) {
if (producttype[i] == “something”
&& theForm.extrafield.value == “”) {
alert(“Please enter Extra Field for selected Product.”);
theForm.extrafield.focus();
return (false);
}
}

However that has no affect and I cannot get it to work. My javascript skills are not that great, so can anyone see where I am going wrong here and help me out here please? Perhaps I have got it totally wrong??

Many thanks in advance!!

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@FangApr 27.2011 — [CODE]if (producttype[i][COLOR="Blue"].value[/COLOR] == "something"[/CODE]
I assume you are not really checking for "something"?
×

Success!

Help @paulb spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...