JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old July 28th, 2004, 10:18 AM
Justin2k Justin2k is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 27 Justin2k User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Taking digit out of text box into cookie!

Me again! These cookies are driving me nuts!
Ok, this time what l want to do is once a user has changed an amount (which will be a number between 1 & 99) in a text box, l want a cookie, which contains a list of information about various items, to be updated. i.e to change one items amount to the value the user just placed in the text box.

The cookie looks like this: cost=20.00/Smoking/1::cost=25.00/Slips And Trips/4::cost=25.00/Substance/2::

Where cost is the name of the cookie and each item is separated by '::'. The last digit in each item is the one l want to update.

I was wondering if l could use the splice() method to remove then immediatly add a value to the cookie. Not sure how l would do this though.


Reply With Quote
  #2  
Old July 28th, 2004, 10:49 AM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 16
Send a message via ICQ to dhouston
You'd probably do better to use split. Something like the following:

Code:
//Get the cookie value into a string; my syntax may be off
cost=new String(document.cookie); 
fields=new Array();
rows=cost.split("::"); //Creates array named rows whose values are filled by values between "::".
for(val i=0; i<rows.length; i++){
    cols=rows[i].split("/")); //May need to escape the slash by prepending a \ to it.
    fields[cols[1]]=new Array(cols[0],cols[2]); //Making a multi-dimensional associative array to map cost and quantity to name
}


This code makes an array that'd look something like the following:

Code:
Array(
    "Smoking"  => Array(
         [0] => 20.00,
         [1] => 1
    ),
 "Slips and Trips"  => Array(
         [0] => 25.00,
         [1] => 4
    )
 "Substance"  => Array(
         [0] => 25.00,
         [1] => 2
    )
);


You can then update this array as needed based on the quantities changed and then reassemble it into a valid cookie string using the array join() method.

You're starting to do some moderately sophisticated stuff with js, and I can't help wondering if it's not time to start doing it server-side, not only for the sake of reliability (people can turn off js), but also for the sake of ensuring accurate information. It'd be no big deal at all for me to get a bunch of products in my cookie, manually change the prices to 0, and submit a final form so that I get all my merchandise for free.
__________________
Please don't PM me asking for solutions outside the scope of a thread.
Keeping all responses in a thread stands to help others who come along later,
which is after all what this forum's all about.

Reply With Quote
  #3  
Old July 28th, 2004, 11:26 AM
Justin2k Justin2k is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 27 Justin2k User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
The problem is that the ISP which the site will be hosted on does not allow server side scripting! But it will only be other businesses that l will be dealing with, not individuals and if they send no money, l send no goods!

Here is some of my code at the moment:
Code:
	// 'for loop' splits each item in thisCookie array at the '=' sign, this takes out the cookie name from the string (i.e 'cost')
	for(j=0; j<thisCookie.length-1; j++){
		thisCookies = thisCookie[j].split("=");
				
		// 'for loop' splits each item in 'thisCookies' array at each '/' and then places each part in a table	
		for (i=1; i<thisCookies.length; i++){
			pieces = thisCookies[i].split("/");
			document.write('<tr><td width="335">');
			document.write(pieces[1]);
			document.write('</td><td width="95">');
			document.write('<input name="amount'+j+'" type="text" id="amount_'+j+'" value="'+pieces[2]+'" onblur="ChangeNumber('+pieces[1]+',?????);" size="2" maxlength="2"  />');
			document.write('</td><td width="78">');
			document.write("");
			document.write(pieces[0]);
			document.write('</td></tr>');

			total += parseFloat(pieces[0]);
		} 
	}


The part in bold is what l think l should use to get the information that the user enters into the text box. Not sure how to get the value of the text box into the '?????' bit though. Here is the code for the function that is called:
Code:
function ChangeNumber(strObjId, nNb){
 MyArrayOfItem = document.cookie.split("::");
 for(i=0;i<MyArrayOfItem.length-1;i++){
 MyItemStuff = MyArrayOfItem[i].split("/") 
 if(MyItemStuff[1]== strObjId){
 var MyNewItemStuff = MyItemStuff[0]+"/"+MyItemStuff[1]+"/"+nNb;
 MyArrayOfItem[i]=MyNewItemStuff;
 }
 }

 var ret = "";
 for(i=0;i<MyArrayOfItem.length-1;i++){
 if(ret == "") ret = MyArrayOfItem[i];
 else ret += "::" + MyArrayOfItem[i];
 }

 MyCookie = ret+"::";
 document.cookie = MyCookie;
 alert(MyCookie);
 }


What l need is to get 'nNb' to equal the value of the text box, which means that the '?????' has to equal the value of the text box. Any suggestions?

Reply With Quote
  #4  
Old July 28th, 2004, 11:55 AM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 16
Send a message via ICQ to dhouston
Try replacing ????? with this.value.

Reply With Quote
  #5  
Old July 28th, 2004, 12:09 PM
Justin2k Justin2k is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 27 Justin2k User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I changed that part of the line to:

Code:
 onblur="ChangeNumber('+pieces[1]+'this.value');"


But now this error message keeps appearing saying that this error is on that line:
Error: Expected ';'

I usually find that that error just means that something is wrong and not that it actaully needs ';' put somewhere.

Reply With Quote
  #6  
Old July 28th, 2004, 03:18 PM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 16
Send a message via ICQ to dhouston
Try:

Code:
onblur="ChangeNumber(pieces[1] + this.value);"


You're getting a parse error because you've got an unclosed quote (count 'em -- three single quotes). Turns out you don't need any quotes because you're using variables/attributes rather than text strings.

Reply With Quote
  #7  
Old July 28th, 2004, 04:34 PM
Justin2k Justin2k is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 27 Justin2k User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Can't believe l didn't see that! Shows how long l've been sat infront of my computer working on this.

The error doesn't appear now but it seems that 'this.value' is undefined (that l discovered from an alert box). Also the cookie value is not replaced with undefined, does this mean it's not being written to.

Reply With Quote
  #8  
Old July 28th, 2004, 04:38 PM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 16
Send a message via ICQ to dhouston
Oh, crap, I'm slow today. My code assumes it's straight js, but you're writing js, so "this.value," which in straight js would return the value of the current textbox, doesn't exist and registers as undefined. So you actually need to print out the words "this.value" so that when the js is written to the page, it parses as the field attribute. Kind of circuitous. The following ought to work:

Code:
document.write('<input name="amount'+j+'" type="text" id="amount_'+j+'" value="'+pieces[2]+'" onblur="ChangeNumber('+pieces[1]+',this.value);" size="2" maxlength="2"  />');

Reply With Quote
  #9  
Old July 28th, 2004, 04:49 PM
Justin2k Justin2k is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 27 Justin2k User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
hmm, is still undefined!! But now the function isn't being run where as it was before.

Reply With Quote
  #10  
Old July 29th, 2004, 05:08 AM
Justin2k Justin2k is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 27 Justin2k User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Ok, at the moment l have:

Code:
document.write('<input name="amount'+j+'" type="text" id="amount_'+j+'" value="'+pieces[2]+'" onblur="ChangeNumber('+pieces[1]+',this.value);" size="2" maxlength="2"/>');


I'm still getting errors. Whenever l change the value in the text box then click elsewhere on the page this error appears:

Error: missing ) after argument list
Source Code: ChangeNumber(Slips Trips and Falls,this.value);

You can see that the first part is working correctly but 'this.value' isn't.

Reply With Quote
  #11  
Old July 29th, 2004, 07:46 AM
Justin2k Justin2k is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 27 Justin2k User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Ok, l've managed to get the function to be called but only when l specify the first element of 'onblur' (e.g. Smoking). So 'this.value' is now working correctly.

Code:
document.write('<input name="amount'+j+'" type="text" id="amount_'+j+'" value="'+pieces[2]+'" onblur="ChangeNumber(\'Smoking\',this.value);" size="2" maxlength="2"/>');


What l want is that the 'Smoking' value to be the value of 'pieces[1]'.

Reply With Quote
  #12  
Old July 29th, 2004, 07:59 AM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 16
Send a message via ICQ to dhouston
The problem in the "missing )" error was that you didn't have your first parameter in quotes, which is why, when you add "Smoking" wrapped in quotes, it works. Try this:

Code:
document.write('<input name="amount'+j+'" type="text" id="amount_'+j+'" value="'+pieces[2]+'" onblur="ChangeNumber(\'' + pieces[1] + '\',this.value);" size="2" maxlength="2"/>');

Reply With Quote
  #13  
Old July 29th, 2004, 08:22 AM
Justin2k Justin2k is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 27 Justin2k User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thumbs up

Thanks a lot dhouston!!!!! It all works now! At last! Its amazing how l was so close, yet so far.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJavaScript Development > Taking digit out of text box into cookie!


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap