SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2003
    Location
    ontario
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question onclick OR onsubmit, which one?

    Hi All,
    I have a e-cart that I want to check the total $$ in the cart and limit a transaction to $25

    I have an alert button that tells the user they must add more to cart or take out items. But (here's the problem) after the alert button pops up the user is still taken to the next page without removing or adding items. How do i get the sumbit button to check the total and only submit if the total is exactly $25 ? thanks.

    PHP Code:
    ------some code here....

    function 
    totalCart() {
        if(
    subtotal 25.00) {
             
    alert("Please fill cart");
        }
        }

    --------------
    some code here
    <FORM onsubmit="totalCart()"METHOD="POST" ACTION="submitform.php"

  2. #2
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,236
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Use onsubmit! If somebody presses enter to submit your form, onclick won't do anything. Onsubmit will still run the validation function even if the user is navigating through your form with the keyboard.

  3. #3
    SitePoint Enthusiast
    Join Date
    Oct 2003
    Location
    ontario
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OnSubmit doesn't work either... it still gives access to the next page without the total being $25 ???

  4. #4
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,236
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Is "subtotal" a form field? You're not defining "subtotal" as anything. You also have to return a false value to stop submission of the form. Try this one instead:
    Code:
    ------some code here....
    
    function totalCart(subtotal) {
        if(subtotal < 25.00) {
             alert("Please fill cart" );
             return false;
        }
        }
    
    --------------some code here
    <FORM onsubmit="totalCart(this.elements['subtotal'])" 
    METHOD="POST" ACTION="submitform.php">

  5. #5
    SitePoint Enthusiast
    Join Date
    Oct 2003
    Location
    ontario
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by vgarcia
    Is "subtotal" a form field? You're not defining "subtotal" as anything. You also have to return a false value to stop submission of the form.
    vgarcia... thanks I tried your code but the form still went through without an alert box even? I'm not sure what I am doing wrong .. the followig is my javascript code that represents the values of the cart.
    Code:
    <script LANGUAGE="JavaScript">
    	function showBasket() {
    		index = document.cookie.indexOf("TheBasket");
    		countbegin = (document.cookie.indexOf("=", index) + 1);
            	countend = document.cookie.indexOf(";", index);
            	if (countend == -1) {
                		countend = document.cookie.length;
            	}
    		fulllist = document.cookie.substring(countbegin, countend);
    		subtotal = 0;
    		document.writeln('<CENTER><FORM NAME="updateform"><center><font size="5" color="#000000" face="Verdana, Arial, Helvetica, sans-serif"><b><img src="images/viewcart.jpg"></B></FONT></center><HR WIDTH=75% size=1 COLOR="#000000">');
    		document.writeln('<TABLE BORDER=0 CELLSPACING=1 CELLPADDING=2>');
    
    document.writeln('<TR><TD BGCOLOR="#B3B3B3"><font size="-2" face="Verdana, Arial, Helvetica, sans-serif"><b><?php echo $txt_qty ?></b></FONT></TD><TD BGCOLOR="#B3B3B3"><font size="-2" face="Verdana, Arial, Helvetica, sans-serif"><b><?php echo $txt_code_no ?></b></FONT></TD><TD BGCOLOR="#B3B3B3"><font size="-2" face="Verdana, Arial, Helvetica, sans-serif"><b><?php echo $txt_item ?></b></FONT></TD><TD BGCOLOR="#B3B3B3"><font size="-2" face="Verdana, Arial, Helvetica, sans-serif"><b><?php echo $txt_option ?></b></FONT></TD><TD BGCOLOR="#B3B3B3"><font size="-2" face="Verdana, Arial, Helvetica, sans-serif"><b><?php echo $txt_price ?></b></FONT></TD><td BGCOLOR="#B3B3B3"><font size="-2" face="Verdana, Arial, Helvetica, sans-serif"><b><?php echo $txt_sub_total ?></b></FONT><TD BGCOLOR="#B3B3B3"><b><FONT COLOR="#B3B3B3">.</FONT></b></TD></TR>');
    		itemlist = 0;
    		for (var i = 0; i <= fulllist.length; i++) {
    			if (fulllist.substring(i,i+1) == '[') {
    				itemstart = i+1;
    				thisitem = 1;
    			} else if (fulllist.substring(i,i+1) == ']') {
    				itemend = i;
    				thequantity = fulllist.substring(itemstart, itemend);
    				itemtotal = 0;
    				itemtotal = (eval(theprice*thequantity));
    				temptotal = itemtotal * 100;
    				subtotal = subtotal + itemtotal;
    				itemlist=itemlist+1;
    				document.write('<tr><td align=middle BGCOLOR="#D9D9D9"><?php echo "'+thequantity+'" ?></td><td BGCOLOR="#D9D9D9"><font size="-2" face="Verdana, Arial, Helvetica, sans-serif"><a href=prod_show.php?code_no='+thenumber+'>'+thenumber+'</A></FONT></td>');
    
    				document.write('<td align=left BGCOLOR="#D9D9D9"><font size="-2" face="Verdana, Arial, Helvetica, sans-serif"><a href=prod_show.php?code_no='+thenumber+'>'+theitem+'</A></FONT></td><td align=left BGCOLOR="#D9D9D9"><font size="-2" face="Verdana, Arial, Helvetica, sans-serif">'+theoption+'</FONT></td><td align=right BGCOLOR="#D9D9D9"><font size="-2" face="Verdana, Arial, Helvetica, sans-serif">'+theprice+'</FONT></td><td align=right BGCOLOR="#D9D9D9"><font size="-2" face="Verdana, Arial, Helvetica, sans-serif">'+top.cart.alterError(itemtotal)+'</FONT></td><td WIDTH="38" align=right BGCOLOR="#D9D9D9"><a href="javascript:removeItem('+itemlist+')"><IMG SRC="images/remove.gif" WIDTH="13" HEIGHT="15" BORDER="0" ALT="<?php echo $txt_remove ?>"></a>&nbsp;</td></tr>');
    
    			} else if (fulllist.substring(i,i+1) == '|') {
    				if (thisitem==1) theitem = fulllist.substring(itemstart, i);
    				if (thisitem==2) theprice = fulllist.substring(itemstart, i);
    				if (thisitem==3) thenumber = fulllist.substring(itemstart, i);
    				if (thisitem==4) theoption = fulllist.substring(itemstart, i);
    				if (thisitem==5) theweight = fulllist.substring(itemstart, i);
    				thisitem++;
    				itemstart=i+1;
    			}
    		}
    		document.writeln('<tr><td align=right BGCOLOR="#B3B3B3" colspan=5><font size="-2" face="Verdana, Arial, Helvetica, sans-serif"><b><?php echo $txt_total ?></b></FONT></td><td align=right BGCOLOR="#D9D9D9"><font size="-2" face="Verdana, Arial, Helvetica, sans-serif">'+top.cart.alterError(subtotal)+'</FONT></td><td BGCOLOR="#B3B3B3"><FONT COLOR="#B3B3B3">.</FONT></td></tr>');
    		document.writeln('</TABLE><CENTER><CENTER><font size="1" face="Verdana, Arial, Helvetica, sans-serif"><IMG SRC="images/remove.gif" WIDTH="13" HEIGHT="15" BORDER="0" ALT="<?php echo $txt_remove ?>"> = <?php echo $txt_remove ?></FONT></CENTER></CENTER>');
    		document.writeln('</FORM>');
    
    	}
    
    
    	function updateItem(itemno, newquant) {
    		newItemList = null;
    		itemlist = 0;
    		for (var i = 0; i <= fulllist.length; i++) {
    			if (fulllist.substring(i,i+1) == '[') {
    				thisitem = 1;
    				itemstart = i+1;
    				fullstart = i+1;
    			} else if (fulllist.substring(i,i+1) == ']') {
    				itemend = i;
    				itemlist=itemlist+1;
    				if (itemlist != itemno) {
    					newItemList = newItemList+'['+fulllist.substring(fullstart, itemend)+']';
    				} else {
    	newItemList = newItemList + '['+theitem+'|'+theprice+'|'+theoption+'|'+thenumber+'|'+theweight+'|'+newquant+'|'+thecat+']';
    				}
    			} else if (fulllist.substring(i,i+1) == '|') {
    				if (thisitem==1) theitem = fulllist.substring(itemstart, i);
    				if (thisitem==2) theprice = fulllist.substring(itemstart, i);
    				if (thisitem==3) theoption = fulllist.substring(itemstart, i);
    				if (thisitem==4) thenumber = fulllist.substring(itemstart, i);
    				if (thisitem==5) theweight = fulllist.substring(itemstart, i);
    				thisitem++;
    				itemstart=i+1;
    			}
    		}
    		index = document.cookie.indexOf("TheBasket");
    		document.cookie="TheBasket="+newItemList;
    		self.location = "basket.php";
                    top.frames[2].location = "middle_right.php";
    	
    
    	}
    
    	function removeItem(itemno) {
    		newItemList = null;
    		itemlist = 0;
    		for (var i = 0; i <= fulllist.length; i++) {
    			if (fulllist.substring(i,i+1) == '[') {
    				itemstart = i+1;
    			} else if (fulllist.substring(i,i+1) == ']') {
    				itemend = i;
    				theitem = fulllist.substring(itemstart, itemend);
    				itemlist=itemlist+1;
    				if (itemlist != itemno) {
    					newItemList = newItemList+'['+fulllist.substring(itemstart, itemend)+']';
    				}
    			}
    		}
    		index = document.cookie.indexOf("TheBasket");
    		document.cookie="TheBasket="+newItemList;
    		self.location = "basket.php";
                    top.frames[2].location = "middle_right.php";
    	
    	}
    
    	function clearBasket() {
    		if (confirm('<?php echo $txt_reset_shopping_cart ?>?')) {
    			index = document.cookie.indexOf("TheBasket");
    			document.cookie="TheBasket=.";
    			self.location = "basket_empty.php";
                    top.frames[2].location = "middle_right.php";
    		}
    	}
    	
    function totalCart(subtotal) {
        if(subtotal < 25.00) {
             alert("Please fill cart" );
             return false;
        }
        }
    
    </script>

  6. #6
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A very common goof. Just add the return statement.
    HTML Code:
    <FORM onsubmit="[b]return [/b]totalCart()" METHOD="POST" ACTION="submitform.php">
    Remember, your totalCart() function returns a true or false, but that boolean value must be returned to the event. A false will cancel the event, where the true will allows it's progression.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  7. #7
    SitePoint Enthusiast
    Join Date
    Oct 2003
    Location
    ontario
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by beetle
    A very common goof. Just add the return statement.
    THanks beetle,

    that did the trick!! Now how do I add in a variable?
    Such as:
    Code:
    function totalCart() {
        if(subtotal < 25.00) {
             alert("Please fill cart" )
             return false;
        }else if(subtotal > 25.00) {
           carttotal = (eval(subtotal - 25.00));
             alert("Too many items. Please remove $('carttotal')" )
             return false;
        }
        }
    I want carttotal to actually show a dollar value.

  8. #8
    SitePoint Enthusiast
    Join Date
    Oct 2003
    Location
    ontario
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    [QUOTE='workinghard']THanks beetle,
    that did the trick!! Now how do I add in a variable?
    Such as:
    Code:
    else if(subtotal > 25.00) {
           carttotal = (eval(subtotal - 25.00));
             alert("Too many items. Please remove $('carttotal')" )
             return false;
    QUOTE]


    Thanks, I GOT IT!

    Code:
    else if(subtotal > 25.00) {
          var carttotal = (eval(subtotal - 25.00));
             alert("Too many items.\n Please remove $" + carttotal)
             return false;


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •