Forum Moderators: open
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.
//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;
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!
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
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[]"
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.
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);
}
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);
}
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?
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;
}