Forum Moderators: open

Message Too Old, No Replies

Count Clicks On IMG (JAVASCRIPT ASP)

         

Mololo1

5:10 pm on Sep 19, 2007 (gmt 0)

10+ Year Member



Hello,

I'm trying to build my own sandwich store online where people can buy subs and add veggies.

I used to have two textfields [Nb of subs] [Nb of subs with veggies] next to the Sub Name (Tuna sandwich). But now, I want to replace those textfields buy image-buttons that can be clicked.Then I want to show the (X) and (Y) and the price.

imgA imgB ClicksA Description Price
[+Sandwich][+veggies] (X)(Y)TUNA SANDWCHICH $Price
ClicksB

Later, the information is sent to my database to record the order.

----------------

<script type="text/javascript">
var counttuna = 0;
window.onload=function(){

oc=document.getElementById('tuna');

oc.onclick=function() {
showtuna(++counttuna);
}
}
function showtuna(word){
document.getElementById('counttuna').firstChild.nodeValue=word;
}
</script>

</head>
<body>

<img src="tuna.gif" id=tuna><span id=counttuna></span>Tuna sandwich

----
Obviously, I need help... :)

Trace

7:44 pm on Sep 19, 2007 (gmt 0)

10+ Year Member



If I understood you correctly, all you need is to increment the number when the image is clicked?

Something like this?

<script type="text/javascript">
function addItem(theCount){
document.getElementById(theCount).innerHTML = parseFloat(document.getElementById(theCount).innerHTML)+1;
}
</script>

<img src="tuna.gif" onclick="addItem('counttuna');"><span id="counttuna">0</span> Tuna sandwich

Mololo1

7:52 pm on Sep 19, 2007 (gmt 0)

10+ Year Member



Wow!
Thank you very much, it's exactly what I needed.
It works just perfect> For ordering sandwiches and drinks from a menu, it's so much more user-friendly then filling forms.
I'm so happy!

Trace

1:57 pm on Sep 20, 2007 (gmt 0)

10+ Year Member



Well I'm happy you're happy... but I wasn't.
Here's a more complete version for you to play with;
<style type="text/css">
body{
font: 12px arial;
}
.itemCount{
cursor:hand;
font-weight:bold;
}
</style>

<script type="text/javascript">
/* add 1 to the total */
function addItem(theCount){
document.getElementById(theCount).innerHTML = parseFloat(document.getElementById(theCount).innerHTML)+1;
}
/* removes 1 from total as long as total is above 0 */
function removeItem(theCount){
if(parseFloat(document.getElementById(theCount).innerHTML) > 0){
document.getElementById(theCount).innerHTML = parseFloat(document.getElementById(theCount).innerHTML)-1;
}
}
/* resets all items according the tagToCheck param to 0 */
function resetAllItems(tagToCheck){
var numElements = document.getElementsByTagName(tagToCheck).length;
var i = 0;
while (i < numElements){
document.getElementsByTagName(tagToCheck)[i].innerHTML = 0;
i++
}
}
/* post all item counts to your ASP page */
function postItems(){
document.getElementById('txtTuna').value = document.getElementById('counttuna').innerHTML;
document.getElementById('txtHam').value = document.getElementById('countHam').innerHTML;
document.getElementById('txtCola').value = document.getElementById('countCola').innerHTML;
document.getElementById('frmPostItems').submit();
}
</script>

<p>Click item to add or click number to remove. <a href="jsDisabled.html" onclick="resetAllItems('span'); return false;">Click here to reset.</a></p>

<p><a href="jsDisabled.html" onclick="addItem('counttuna'); return false;"><img src="tuna.gif" alt="click to add item" /></a>
<br /><span id="counttuna" onclick="removeItem('counttuna');" class="itemCount">0</span> Tuna sandwich</p>

<p><a href="jsDisabled.html" onclick="addItem('countHam'); return false;"><img src="ham.gif" alt="click to add item" /></a>
<br /><span id="countHam" onclick="removeItem('countHam');" class="itemCount">0</span> Ham sandwich</p>

<p><a href="jsDisabled.html" onclick="addItem('countCola'); return false;"><img src="cola.gif" alt="click to add item" /></a>
<br /><span id="countCola" onclick="removeItem('countCola');" class="itemCount">0</span> Cola</p>

<!-- form to post totals -->
<form action="addToDatabase.asp" method="post" id="frmPostItems">
<input type="hidden" name="txtTuna" id="txtTuna" />
<input type="hidden" name="txtHam" id="txtHam" />
<input type="hidden" name="txtCola" id="txtCola" />

<input type="button" value="Insert into database" onclick="postItems();" />
</form>