Jump to content

javascript code that does not work for more than one link.


prichardson

Recommended Posts

Hello,I have this javascript that works when the link appears only once. But I have to have it appear many times through a repeat.The javascript link is:

<!--function SubmitForm( selectedtype ){  document.textsubmitform.WhichType.value = selectedtype;  document.textsubmitform.submit();}-->

I think it has something to do with this line in particular the bold bit, but i am not sure what to change it to: document.textsubmitform.WhichType.value = selectedtype Maybe it is done via a id, and therefore every link has the same id- so would it be a way to identify each link with a unique id?This is my link that displays on the page:

<a href="java script:SubmitForm('AddToBasket')"><font color="#000000">Add 		to basket</font></a>

Any help will be appreciated.Thanks

Link to comment
Share on other sites

I don't understand what the prblem is. The function you have is adding hte pass valyue to the value of the input field then submitting the form. What is it supposed to do? What errors are you getting?

Link to comment
Share on other sites

Hi aspnetguy,The problem is that it works fine when the 'Add to Basket' once. But I have several products on the page, so when there is 10 products on the page each product has this link 'Add to Basket'. When there are 10 products on the page the 'Add to Basket' does not work. How can i get the link to work when the link appears more than once on the page?Why is the link not working when it appears more than once?Thanks

Link to comment
Share on other sites

Hi aspnet,This is the entire code for the form which i am refering about:The code below is where the javascript is having the problem i think. I think it has to do something with the "selectedtype"

<!--function SubmitForm( selectedtype ){  document.textsubmitform.WhichType.value = selectedtype;  document.textsubmitform.submit();}-->

The code below is the form that displays the link "Add to Basket". But I am sure it will be the code above that would be worth the look at as it is the lines that displaying the error

<form action="test3.asp" method="post" name="textsubmitform" id="textsubmitform"> <img src="assets_cm/FILES/image/<%=rsP("ximage")%>" border="0">   <%=rsP("name")%>  £ <%=rsP("price")%>   <a href='<%=strURLPath%>pages/store/products/ec_view.asp?PID=<%=rsP("ProductID")%>'>View Product Details</a>   <a href="java script:SubmitForm('AddToBasket')"><font color="#000000">Add to basket</font></a> <input name="WhichType" id="WhichType" type="hidden"><input name="Username" type="hidden" id="Username" value="<%=strUsername%>"><input name="ProductID" type="hidden" id="ProductID" value="<%=rsP("ProductID")%>"><input name="ximage" type="hidden" id="ximage" value="<%=rsP("ximage")%>"><input name="name" type="hidden" id="name" value="<%=rsP("name")%>"><input name="price" type="hidden" id="price" value="<%=rsP("price")%>"><input name="sizes" type="hidden" id="sizes" value="<%=rsP("sizes")%>"><input name="make" type="hidden" id="make" value="<%=rsP("make")%>"><input name="Quantity" type="hidden" id="Quantity" value="1"><input name="BActive" type="hidden" id="BActive" value="True"></form><BR><BR><%					rsP.movenext wend%><% end if %>

Link to comment
Share on other sites

I assume that you are duplicating all the hidden fields for each product? The reason it is causing an error is because an id can only be used once. The reason WhichType is shown as the error is because it is the first to be found with a duplicate id. You will need to make them have unique id and name.

Link to comment
Share on other sites

Hi aspnetguy,Please can you tell me the code I would need to put in or change to make each one have an unique id?Would you change "selectedtype" to something else? or would it be additional code to put in to make each one have an unique id?I am guessing that it is a word i would need to change from the existing javascript, and I am thinking it is the "selectedtype"? What would it be if it would make the code to work for each product to have an unique id?Thanks

Link to comment
Share on other sites

first of all in the html/asp side of things you will need to keep track of a counter as you loop through your recordset and append the counter number to the end of the id.Then for the link do this

<a href="java script:SubmitForm('AddToBasket',<%= counterNum %>)"><font color="#000000">Add to basket</font></a>

Then change the function to this

<!--function SubmitForm( selectedtype, num ){  document.getElementById("WhichType" + num).value = selectedtype;  document.textsubmitform.submit();}-->

Link to comment
Share on other sites

Thanks aspnetguy,I put in the code, but i am still getting an error. I think we are getting close to solving the problem though...what about at the bottom of the form with this bit of code:

<input name="WhichType" id="WhichType" type="hidden">

Do you think we should change this to include the "num" bit or should add an extra input name as "num"?Thanks

Link to comment
Share on other sites

Hi aspnetguy,Just thinking, and i have not understood how you get the two pieces of code to link together? The <%=counterNum%> and ("WhichType" + num) ? Should we put a <% dim counterNum counterNum = "WhichType" + num%> Or something similar to link the two ?

Link to comment
Share on other sites

Hi Patrick,I have tried javascript and also java script

<a href="java script:SubmitForm('AddToBasket', <%=counterNum%>)"><font color="#000000">Add to basket</font></a><a href="java  script:SubmitForm('AddToBasket', <%=counterNum%>)"><font color="#000000">Add to basket</font></a>

I think <%=counterNum%> is not pulling any value. I think maybe there is a piece of code that is needed to link the <%=counterNum%> to the javascript code.What code should i put to get them to link-up, so the <%=counterNum%> gets a value?

Link to comment
Share on other sites

I think <%=counterNum%> is not pulling any value. I think maybe there is a piece of code that is needed to link the <%=counterNum%> to the javascript code.What code should i put to get them to link-up, so the <%=counterNum%> gets a value?
Well that's in the ASP part of the file. I don't know if you're looping through the different products with For ... Next or Do ... Loop?
Link to comment
Share on other sites

Hi Patrick,Yeah, it is looping:

<%					rsP.movenext wendend if %>

But what i can not see, is where <%=counterNum%> is pulling any value, cause it is not anyway else saying: counterNum=xyzWhere is the number being generated in this:

<a href="java script:SubmitForm('AddToBasket',<%= counterNum %>)"><font color="#000000">Add to basket</font></a>

<!--function SubmitForm( selectedtype, num ){  document.getElementById("WhichType" + num).value = selectedtype;  document.textsubmitform.submit();}-->

Link to comment
Share on other sites

I have got this:
<% counterNum = 0rsP.movenext While counterNum = counterNum + 1		wendend if %>

I'd do something like this:
<%counterNum = 0Do While Not rsP.EOFcounterNum = counterNum + 1 %>...Whatever should be written about the specific product comes here...<a href="java script:SubmitForm('AddToBasket',<%= counterNum %>)"><font color="#000000">Add to basket</font></a><%rsP.MoveNextLoop %>

This will make the recordset loop through every record and add one to the counterNum for every record.

Link to comment
Share on other sites

Thanks Patrick,I have got this but i get an "Expected statement" error... can you see where i have gone wrong?:

<%counterNum = 0Do While Not rsP.EOFcounterNum = counterNum + 1 %><form action="test3.asp" method="post" name="textsubmitform" id="textsubmitform"><img src="assets_cm/FILES/image/<%=rsP("ximage")%>" border="0">   <%=rsP("name")%>  £ <%=rsP("price")%>   <a href='<%=strURLPath%>pages/store/products/ec_view.asp?PID=<%=rsP("ProductID")%>'>View Product Details</a>   <a href="java script:SubmitForm('AddToBasket', <%=counterNum%>)"><font color="#000000">Add to basket</font></a>	<input name="WhichType" id="WhichType" type="hidden"><input name="Username" type="hidden" id="Username" value="<%=strUsername%>"><input name="ProductID" type="hidden" id="ProductID" value="<%=rsP("ProductID")%>"><input name="ximage" type="hidden" id="ximage" value="<%=rsP("ximage")%>"><input name="name" type="hidden" id="name" value="<%=rsP("name")%>"><input name="price" type="hidden" id="price" value="<%=rsP("price")%>"><input name="sizes" type="hidden" id="sizes" value="<%=rsP("sizes")%>"><input name="make" type="hidden" id="make" value="<%=rsP("make")%>"><input name="Quantity" type="hidden" id="Quantity" value="1"><input name="BActive" type="hidden" id="BActive" value="True"></form><BR><BR><%					rsP.movenext Loopend if %>

Link to comment
Share on other sites

Does it say a linenumber?I dunno if you just didn't copy all of it, but you have an "end if" statement, and there's no "if" statement in the beginning.

Link to comment
Share on other sites

Thanks Patrick,I got it to work... it the sense that the page appears and each product has an id... 1,2, etc... But when I press the button "Add to Basket"... it does not work- in that it does not add to the basket... Why is that??[code<%if catID <> "" thenset rsP = objconn.execute ("SELECT * from products where catID=" & catID )counterNum = 0Do While Not rsP.EOFcounterNum = counterNum + 1 %><form action="test3.asp" method="post" name="textsubmitform" id="textsubmitform"><img src="assets_cm/FILES/image/<%=rsP("ximage")%>" border="0">   <%=rsP("name")%>  £ <%=rsP("price")%>   <a href='<%=strURLPath%>pages/store/products/ec_view.asp?PID=<%=rsP("ProductID")%>'>View Product Details</a>   <a href="java script:SubmitForm('AddToBasket', <%=counterNum%>)"><font color="#000000">Add to basket</font></a> <input name="WhichType" id="WhichType" type="hidden"><input name="Username" type="hidden" id="Username" value="<%=strUsername%>"><input name="ProductID" type="hidden" id="ProductID" value="<%=rsP("ProductID")%>"><input name="ximage" type="hidden" id="ximage" value="<%=rsP("ximage")%>"><input name="name" type="hidden" id="name" value="<%=rsP("name")%>"><input name="price" type="hidden" id="price" value="<%=rsP("price")%>"><input name="sizes" type="hidden" id="sizes" value="<%=rsP("sizes")%>"><input name="make" type="hidden" id="make" value="<%=rsP("make")%>"><input name="Quantity" type="hidden" id="Quantity" value="1"><input name="BActive" type="hidden" id="BActive" value="True"></form><BR><BR><% rsP.movenext Loopend if %>[/code]

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...