/    Sign up×
Community /Pin to ProfileBookmark

Using hidden form field in Javascript Array for validation

Hi All,

I have an order form where a customer can select a number of artwork prints supplied by different artists and suppliers. The name, price, postage amount and other details for each of the prints are held in a table in a MySQL database. As postage amount may vary between prints and suppliers of the prints we must have the customer select postage per print – not the amount just yes or no.

The customer may select as many of these prints as he desires – printa, printb, printc, printd etc. through to printz. Many of the prints are provided by separate individual suppliers and must therefore have postage paid for each ordered print and this is automatically added by the order form. However some prints are provided by the same supplier and only one postage fee needs to be chosen (yes or no), so these are not automatically added and the customer must choose to pay postage on only one of the prints (not all 3 if he orders 3 of them).

So for postage purposes I have a postage code on the table in the MySQL database. For example printa, printb and printc are all supplied by the same supplier so each have a linked postage code of A. Similarly others may also be linked using different postage codes e.g. printx and printy may have a postage code of B.

So whenever a customer chooses a print that does not have a linked postage code then the postage fee is automatically selected for him and charged to the total. However where prints are linked to the same postage code we must allow the costomer to select postage (yes or no), but we must force him to select yes or no for at least one of the linked prints.

So to do this I need a javascript validation alert to force the customer to choose yes for postage for at least one of the prints, not necessarily all of them, where they have a linked postage code. To do that I can add the the postage linked code as an array for each print selected to a form hidden field so that my javascript can use the field as follows:
<input type=”hidden” name=”postagearray[]” value=”<? echo $postagecode; ?>”>

That is all OK, but now I somehow need to use that array in the javascript to match those prints that have a postage linked code and if no has been selected for postage for all linked prints then produce a javascript alert message. My code may be something like this:

[CODE]
var postagearray = [];

for (var i=0; i<postagearray.length; i++) {

if (theForm.print.value == postagearray[i]) {
alert(“Please select YES for postage for at least one of the linked prints.”);
theForm.postagecode.focus();
return (false);
}
}
[/CODE]

But that is not it I know – not sure how to compare the two prints and see if their postage code matches. While I do know some javascript, how to use arrays is a new area for me. Can anyone please help on this as I am really struggling. Or have I got it totally wrong and am beyond help – I hope not!!

Many, many thanks if anyone can help – I would really appreciate it.

Paulb

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@KorMar 29.2011 — Don't confound PHP with JavaScript. In javascript the value of a [B]name[/B] attribute is a [I]string[/I], or inside a string the braces [] have no special meaning. For PHP they might signal for an array. But not in JavaScript.

So, if you have one or more elements with the [B]name="postagearray[]"[/B], you may circle trough the collection like
<i>
</i>var myElements=document.getElementsByName('postagearray[]');
for(var i=0; i&lt;myElements.length; i++){
//
}
×

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 5.7,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...