Javascript Hidden field problem...

This is a discussion on "Javascript Hidden field problem..." within the JavaScript Forum section. This forum, and the thread "Javascript Hidden field problem... are both part of the Program Your Website category.



 Subscribe in a reader

Go Back   Webforumz.com > Main Forums > Program Your Website > JavaScript Forum

Notices


Reply
 
LinkBack Thread Tools
  #1  
Old Jun 19th, 2006, 22:13
Junior Member
Join Date: Jun 2006
Location: England
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Javascript Hidden field problem...

Hi,

I have the following piece of code:

<input type="hidden" name="item_number">

But how can i add a value thats equal to a variable, something like:

<input type="hidden" name="item_number" value = $var1>

Where var1 is set previously using javascript.

Thanks.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote

  #2  
Old Jun 20th, 2006, 18:44
Most Reputable Member
Join Date: Apr 2006
Location: Cornwall, UK
Posts: 1,310
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Javascript Hidden field problem...

Give your field an 'id', it can be the same as the 'name', then use the code
Code: Select all
document.getElementById('fieldid').value = whatever;
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #3  
Old Jun 20th, 2006, 18:47
Junior Member
Join Date: Jun 2006
Location: England
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Javascript Hidden field problem...

ive been trying for the last couple of days to get this working:

<script type="text/javascript">
document.write('<input type="hidden" name="item_name" value="'+displaytext1+'">');
</script>


without success! ive tried alot of variations. it links into paypal, but paypal does not show the description.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #4  
Old Jun 20th, 2006, 18:55
Most Reputable Member
Join Date: Apr 2006
Location: Cornwall, UK
Posts: 1,310
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Javascript Hidden field problem...

I don't understand what you are trying to say in this post. My previous post answered the question you originally asked.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #5  
Old Jun 20th, 2006, 18:57
Junior Member
Join Date: Jun 2006
Location: England
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Javascript Hidden field problem...

Sorry, im trying to feed the paypal shopping cart with a description, so nothing is displayed on my page, but is on the paypal page.

theres no problem hard coding the values, but i need to use a variable for the item description as its dynamic.

Thanks.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #6  
Old Jun 20th, 2006, 19:12
Most Reputable Member
Join Date: Apr 2006
Location: Cornwall, UK
Posts: 1,310
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Javascript Hidden field problem...

Where is this description to be obtained from?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #7  
Old Jun 20th, 2006, 19:15
Junior Member
Join Date: Jun 2006
Location: England
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Javascript Hidden field problem...

it is loaded on startup. a product id is passed in the URL and then this is decoded into a description - displaytext1.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #8  
Old Jun 20th, 2006, 19:29
Most Reputable Member
Join Date: Apr 2006
Location: Cornwall, UK
Posts: 1,310
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Javascript Hidden field problem...

Ok. You need to process the file server side using php or some such.

You can access variables passed as part of the url with the $_GET function.
Code: Select all
$id = $_GET['prodID'];
You can then use the switch statement to decode this into a description.
Code: Select all
switch ($id) {
   case 1 : $desc = 'prod 1 desc';
   case 2 : $desc = 'prod 2 desc';
   ...
}
Then in your form code
Code: Select all
<input type='hidden' ... value=<?php print ("'".$desc); ?>' />
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #9  
Old Jun 20th, 2006, 19:31
Junior Member
Join Date: Jun 2006
Location: England
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Javascript Hidden field problem...

Ive already done steps 1 and 2, but was told I can use javascript to achieve the hidden field problem. I have no PHP experience, thats why I want to avoid this, my pag is purely html and javascript.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #10  
Old Jun 20th, 2006, 19:49
Most Reputable Member
Join Date: Apr 2006
Location: Cornwall, UK
Posts: 1,310
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Javascript Hidden field problem...

To extract the id passed in the url using JavaScript you need to now the position of the id in the list of parameters that follow the '?' delimeter in the url. Then you can use;
Code: Select all
var prodID = location.search.substring(1);
or whatever its position is.
Decode the id into a description and use the
Code: Select all
document.getElementById...
example I posted earlier.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #11  
Old Jun 20th, 2006, 21:05
Junior Member
Join Date: Jun 2006
Location: England
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Javascript Hidden field problem...

Thanks, i;ve now got the following:

<input type="hidden" name="business" id="email" value="">
<input type="hidden" name="item_name" id="descr" value="">
<input type="hidden" name="amount" id="num" value = "">

<script type="text/javascript">
document.getElementById("email").value = "email@hotmail.com";
document.getElementById("descr").value = displaytext1;
document.getElementById("num").value = num4;
</script>

but only the email works, cos its hard coded, but still cant get the variables to work.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #12  
Old Jun 20th, 2006, 21:10
Most Reputable Member
Join Date: Apr 2006
Location: Cornwall, UK
Posts: 1,310
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Javascript Hidden field problem...

Show me a copy of the url from the browsers address bar and the code you are using to set the variables from the parameters passed in the url.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #13  
Old Jun 20th, 2006, 21:28
Junior Member
Join Date: Jun 2006
Location: England
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Javascript Hidden field problem...

file:///D:/Details.htm?n1=33700&n2=mit&n3=18&n4=25&n5=4

<script type="text/javascript">
function GetVars( def )
{
this._def_ = def;
var query, queries = top.location.search.substring(1).split( /\&/ );
for ( var i=0; (query = queries[ i ]); i++ )
{
query = query.split( /\=/ );
this[query[0]] = ( typeof query[1] == 'undefined' ) ? def : unescape(query[1]).replace( /\+/g, " " );
}
}
GetVars.prototype.exists = function( key )
{
return ( typeof this[key] != 'undefined' );
}
GetVars.prototype.assign = function( key )
{
return ( this.exists( key ) ) ? this[key] : this._def_;
}
var displaytext1;
var displaytext2;

var _GET = new GetVars( '' );
var num1 = _GET.assign( 'n1' );
var num2 = _GET.assign( 'n2' );
var num3 = _GET.assign( 'n3' );
var num4 = _GET.assign( 'n4' );
var num5 = _GET.assign( 'n5' );

switch (num1)
{
case '33700':
displaytext1='Test';
.
.
.

Is this ok? I know the assignments at the bottom definitely work. Getvars is called 'onload'.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #14  
Old Jun 20th, 2006, 22:00
Most Reputable Member
Join Date: Apr 2006
Location: Cornwall, UK
Posts: 1,310
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Javascript Hidden field problem...

With this piece of code
Code: Select all
     var query, queries = top.location.search.substring(1).split( /\&/ );
i think you intended to pick up the whole of the parameter list and split them into an array.

This is not what is happening. You are only picking up the first parameter, substring(1), therefore you are trying to split 'n1=33700'.

I also think the rest of your code is over complicated for what you are trying to do.

Golden rule - Keep It Simple.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #15  
Old Jun 20th, 2006, 22:06
Junior Member
Join Date: Jun 2006
Location: England
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Javascript Hidden field problem...

after this code, ive 'writeln' the variables to the page and they are all correct, ie theres no 'n1=' within any of the variables, they contain the text i need.

its just the getelementbyid, im not sure whats wrong with the variable assignemnt im trying to do, been at this since yesterday!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Tags
javascript, hidden, field, problem

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Text field &amp; list menu size problem longstand Web Page Design 2 Nov 1st, 2007 12:38
Need javascript to set input field value field jdadwilson JavaScript Forum 3 May 9th, 2007 04:47
Display Problem: Form-->Legend field in Firefox SuperGrover1981 Web Page Design 2 Sep 5th, 2006 18:05
Hidden layers using CSS and JavaScript blis102 Web Page Design 1 Jun 29th, 2006 21:17
Odd input field not displaying problem hankhill Classic ASP 1 Jun 26th, 2006 23:11


All times are GMT. The time now is 17:28.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42