Forum Moderators: open

Message Too Old, No Replies

Form Validation: Verify Qty based on item #

         

gabe8496

7:55 pm on Oct 3, 2005 (gmt 0)

10+ Year Member



Ok, need some more help here.

I need to be able to verify that the qty of a certain item meets a required # if not, alert.

Deals with clothing and quantites.
Scenario:

Logo id is 05BCT

2 shirts are available with this logo:
05BCT_Wht(baseid) with avialable sizes: SM-3X
05BCT_Red(baseid) with available sizes: SM-2X

The user can choose either shirt color and whatever quantities he/she wants. What I need to do is that on Submit (Checkout), that the form is verified with the following:
Any shirt/line that has the logo 05BCT must total 24 in qty.

So, they can order something like:
05BCT_Wht: 5-SM, 10-LG
05BCT_Red: 5-SM, 4-LG

As long as the qty is 24 with the said logo, then all is good.

BUT if they order something like:
05BCT_Wht: 5-SM, 10-LG
05BCT_Red: 5-SM, 3-LG

Alert them when checking out because that is only 23 qty, not 24.

Hope someone can help me out. I know i need a function on submit but that is where i'm stumped. Not too familiar with functions.

whoisgregg

3:18 am on Oct 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Do you have any sample code you can post here? Like the relevant portions of the form? It would help us greatly to have something to work from rather than write your form from scratch. :)

gabe8496

5:00 am on Oct 4, 2005 (gmt 0)

10+ Year Member



Thanks for you response. Below is some coding I have:

//display number of products in cart
$query = "SELECT * from customitems_carttemp WHERE sess = '$sessid'";
$results = mysql_query($query)
or die (mysql_query());
$rows = mysql_num_rows($results);
echo $rows;
<HTML CODE HERE>
while ($row = mysql_fetch_array($results)) {
echo "<td align='center'><form name='update' method='POST' action='change.php'>
<input type='hidden' name='baseid' value='$row[baseid]'>
<input type='hidden' name='hidden' value='$row[hidden]'>
<input type='text' name='qty' size='2' value='$row[qty]'>";
echo "</td>";
echo "<td align='left'>";
echo " <a href='javascript:;'>".$row[baseid]." with text ".$row[personalization]."</a></td>";
echo "<td align='left'>";
echo "<a href='javascript:;'>".$row[sub]."</a></td>";
echo "<td align='right'>$".number_format($row[price], 2)."</td>";
echo "<td align='right'>";
//get extended price
$extprice = number_format($row[price] * $row[qty], 2);
echo "$".$extprice."";
echo "</td>";
echo "<td align='center'>";
echo "<input type='submit' name='Submit' value='Update'></form></td></form></td>";
echo "<td>";
echo "<form name='remove' method='POST' action='delete.php'>
<input type='hidden' name='baseid' value='$row[baseid]'>
<input type='hidden' name='hidden' value='$row[hidden]'>
<input type='hidden' name='qty' size='2' value='$row[qty]'>";
echo "<input type='submit' name='Submit' value='Delete'></form></td>";
echo "</tr>";
//add extended price to total

$total = $extprice + $total;


Hope this helps. let me know if I need to post more.

So, to recap, there is currently 1 logo ($logo) and multiple shirts ($baseid). The user can have a mixed qty of shirts (ie. red shirts and white shirts) aslong as the qty of shirts with the that logo add up to 24.

Hope i'm explaining clearly.

Thanks!

Bernard Marx

6:36 am on Oct 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Could you provide a sample output that has all the PHP resolved to HTML?

gabe8496

12:44 pm on Oct 4, 2005 (gmt 0)

10+ Year Member



sure. Let me do that now.

gabe8496

2:27 pm on Oct 4, 2005 (gmt 0)

10+ Year Member



Kinda long..but it has a few items:
<table width="100%">
<form name='update' method='POST' action='change.php'>
<tr>
<td align="center">
<input type='hidden' name='baseid' value='C05BCT_WHT'>
<input type='hidden' name='hidden' value='466'>
<input type='text' name='qty' size='2' value='5'>
</td>
<td align='left'>C05BCT_WHT with text TEST</td>
<td align='left'>SM</td>
<td align='right'>$5.90</td>
<td align='right'>$29.50</td>
<td align='center'></td>
<td>
<input type='hidden' name='hidden' value='466'>
</td>
</tr>
<tr>
<td align="center">
<input type='hidden' name='baseid' value='C05BCT_WHT'>
<input type='hidden' name='hidden' value='467'>
<input type='text' name='qty' size='2' value='5'>
</td>
<td align='left'>C05BCT_WHT with text TEST</td>
<td align='left'>LG</td>
<td align='right'>$5.90</td>
<td align='right'>$29.50</td>
<td align='center'></td>
<td>
<input type='hidden' name='hidden' value='467'></td>
</tr>
<tr>
<td align="center">
<input type='hidden' name='baseid' value='C05BCT_WHT'>
<input type='hidden' name='hidden' value='468'>
<input type='text' name='qty' size='2' value='5'></td>
<td align='left'>C05BCT_WHT with text TEST</td>
<td align='left'>1X</td>
<td align='right'>$5.90</td>
<td align='right'>$29.50</td>
<td align='center'></td>
<td>
<input type='hidden' name='hidden' value='468'></td>
</tr>
<tr><td align="center">
<input type='hidden' name='baseid' value='C05BCT_PNK'>
<input type='hidden' name='hidden' value='469'>
<input type='text' name='qty' size='2' value='5'></td>
<td align='left'>C05BCT_PNK with text TEST</td>
<td align='left'>SM</td>
<td align='right'>$6.90</td>
<td align='right'>$34.50</td>
<td align='center'></td><td>
<input type='hidden' name='hidden' value='469'></td>
</tr>
<tr>
<td align="center">
<input type='hidden' name='baseid' value='C05BCT_PNK'>
<input type='hidden' name='hidden' value='470'>
<input type='text' name='qty' size='2' value='5'>
</td>
<td align='left'>C05BCT_PNK with text TEST</td>
<td align='left'>LG</td>
<td align='right'>$6.90</td>
<td align='right'>$34.50</td>
<td align='center'></td><td>
<input type='hidden' name='hidden' value='470'>
</td>
</tr>
<tr>
<td align="center">
<input type='hidden' name='baseid' value='C05BCT_PNK'>
<input type='hidden' name='hidden' value='471'>
<input type='text' name='qty' size='2' value='5'>
</td>
<td align='left'>C05BCT_PNK with text TEST</td>
<td align='left'>2X</td>
<td align='right'>$7.90</td>
<td align='right'>$39.50</td>
<td align='center'></td><td>
<input type='hidden' name='hidden' value='471'></td></tr>
<tr>
<td colspan="7"><input type='submit' name='Submit' value='Update'></td>
</tr>
</form>
</table>

gabe8496

2:32 pm on Oct 4, 2005 (gmt 0)

10+ Year Member



Notice how each item begins with C05BCT_ followed by either Wht or Pnk. this is the baseid for the shirt. What you don't see is the logo id, which is C05BCT. Each shirt is also followed by a custom text that, in this case, reads TEST.
So,
C05BCT_PNK with text TEST
C05BCT_Wht with text TEST
these both items have the $logo=C05BCT and have $personalization=TEST

So, I need to have a function that will:
Count the # of shirts (qty) that have the same $logoid and $personalization. If the qty >= 24, then ok but if not, create an alert.

HOpe this is legible

Bernard Marx

11:37 am on Oct 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry to make more requests, gabe. This is do-able as it is, but I'd like to straighten out a couple of things in the hope of making the eventual code simpler, and less fragile.

1) In the sample output, all the items have the same logoid (C05BCT). These may be different each time. However, are we likely to get any situation where two more different logoids appear in the same form?
- and thus will require counting 2 or more sets to see if they number >=24 individually.

2)This is being posted to a PHP script. Am I right in saying that the names that are used multiple times should really be suffixed with []. eg

name="baseid[]" and name="hidden[]"

gabe8496

3:25 pm on Oct 5, 2005 (gmt 0)

10+ Year Member



Both of your questions are correct.

There will be times when the customer has 2 different logoids and both will need to be checked. the min. requirement (qty=24) is set by each logoid. so if the user has 05bct and 05cst as logoids then each one will be checked to make sure they meet the 24 min requirement. So in that scenario, it would check twice.

As for the array brackets, yes, they are their, i just removed them from the code when i posted it. but they are in an array.

Again, thanks for your patience and help.

Bernard Marx

9:09 pm on Oct 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This one should do it. You if you can get it to do what you want. Then I'll explain it, if need be.

Place a call to it in the form tag itself (better than submit button)

<form ... onsubmit="return checkQuantity(this)">

Watch out for the broken pipes!

/* 
This script contains the use of the ¦¦ token.
The WM editor will corrupt the pipes.
Please replace them with unbroken pipes
(or it won't work!)
*/
function checkQuantity(form)
{
var minimum = 24;
var idElms = form.elements.baseid;
var qtyElms = form.elements.qty;
var message = '';
var OK = true;

var counts = {};
var logoId, k;

for(k=0;k<idElms.length;k++)
{
logoId = idElms[k].value.split('_')[0];
counts[logoId] = (counts[logoId]¦¦0) + qtyElms[k].value*1;
}

for(logoId in counts)
{
if( counts[logoId] < minimum )
message += logoId + 'under 24. Only '+ counts[logoId] + 'items\n';
OK = false;
}

if(! OK) alert(message);
/*temp*/inspect(counts)
return OK;
}

/* temporary testing fn */
function inspect(counts)
{
var out = 'inspection:\n\n';
for(var id in counts)
out += id+' : '+ counts[id] +'\n';
alert(out);
}

Bernard Marx

9:22 pm on Oct 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Oh yes. For the PHP-style names that'll be:

var idElms = form.elements['baseid[]'];
var qtyElms = form.elements['qty[]'];

gabe8496

3:43 pm on Oct 6, 2005 (gmt 0)

10+ Year Member



Thanks! I tested but get an "Expected identifier".

Here is the code:


function validate(quantity)
{
var minimum = '24';
var idElms = form.elements.['baseid[]'];
var qtyElms = form.elements.['qty[]'];
var message = '';
var OK = true;
var counts = {};
var logo, k;
for(k=0;k<idElms.length;k++)
{
logo = idElms[k].value.split('_')[0];
counts[logo] = (counts[logo]¦¦0) + qtyElms[k].value*1;
}
for(logo in counts)
{
if( counts[logo] < minimum )
message += logo + 'under 24. Only '+ counts[logo] + 'items
';
OK = false;
}
if(! OK) alert(message);
/*temp*/inspect(counts)
return OK;
}
/* temporary testing fn */
function inspect(counts)
{
var out = 'inspection:
';
for(var id in counts)
out += id+' : '+ counts[id] +'
';
alert(out);
}

I replaced logoid with logo (i renamed the fields in my form). I also replaced the broken pipe with the correct characters.
In the <form> tags i have: onSubmit="return validate(this)" instead because I also have a checkbox that needs to be selected(disclaimer).

Would this show any problems?
Also, trying to read exactly what the function does. Is it counting the # of times logo[] shows up and determines if it is less than 24? or is it adding up the qty[] for each item that has the logo[] and checking to make sure qty[] > 24?

Bernard Marx

6:09 pm on Oct 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



function validate(quantity)

I used the argument name form, you have changed it to quantity.

Either change it back to form,
or change all the uses of the variable form in the function to quantity.

gabe8496

7:37 pm on Oct 6, 2005 (gmt 0)

10+ Year Member



I have it set to form but i still get that Expected identifier

Bernard Marx

9:03 pm on Oct 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sent you the code direct.

gabe8496

9:22 pm on Oct 6, 2005 (gmt 0)

10+ Year Member



Sent you an email.

I've been filtered.T (firewall)

gabe8496

8:20 pm on Oct 7, 2005 (gmt 0)

10+ Year Member



Another question, with this script, i can no longer validate that the user checkmarks the "I agree to terms" box.

If I call the function in my <form> tag it disable the qty check.

How can I add this function into the qty checked script. i've been trying it for a good amount of time and still can't figure that out.

My original script:


function validate(cart){
if (cart.disclaimer.checked == false) {
alert('You must acknowledge that you agree to the terms before continuing.');
return false;
};
else return true;
}

I had this inside <script> tags in the head content.

Bernard Marx

8:48 pm on Oct 7, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Put this section somewhere in the new function:


if (! form.disclaimer.checked)
{
OK = false;
alert('You must acknowledge that you agree to the terms before continuing.');
}

gabe8496

4:56 pm on Nov 1, 2005 (gmt 0)

10+ Year Member



Ok...trying to understand this. But need some help. Can anyone provide me a brief explanation on how this works. I can understand the variables...but why get the length of var idElems?

Just need to understand how this all works.

Thanks!

Bernard Marx

9:24 pm on Nov 1, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



why get the length of var idElms?

idElms is a node list. We use it's length in the process of iterating over it.