Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-09-2006, 11:27 AM   PM User | #1
edmonds
New Coder

 
Join Date: Nov 2005
Posts: 23
Thanks: 0
Thanked 1 Time in 1 Post
edmonds is an unknown quantity at this point
How do i get a Variable from a script?

hi
i have some variables that make a php url:

function compute1(){
var url ='greensills-cards.php?';
var preturn = 'window-boxes-herbs';
var pplants = document.getElementById("product1").value;
var pprice = document.getElementById("price1").value;

{ document.getElementById("addr1").href=url+'prod='+pplants+'&price='+pprice+'&return='+preturn;}

}

This works.

I have a script that produces an alert:

function get_check_value()
{
var pherbs = "";
for (var i=0; i < document.HerbWindowBoxes.herb.length; i++)
{
if (document.HerbWindowBoxes.herb[i].checked)
{
pherbs = pherbs + document.HerbWindowBoxes.herb[i].value + "\n";
}
}
pherbs = "You selected the following:\n" + pherbs;
alert(pherbs);
return false;
}

This works.

But i want to put the two together so that i get a var pherbs to put into my php url.

This (abomination) does not work:

function compute1(){
var url ='greensills-cards.php?';
var preturn = 'window-boxes-herbs';
var pplants = document.getElementById("product1").value;
var pprice = document.getElementById("price1").value;
var pherbs = function get_check_value()
{
var pherbs = "";
for (var i=0; i < document.HerbWindowBoxes.herb.length; i++)
{
if (document.HerbWindowBoxes.herb[i].checked)
{
pherbs = pherbs + document.HerbWindowBoxes.herb[i].value + "\n";
}
}
pherbs = pherbs;
return false;
}


{ document.getElementById("addr1").href=url+'prod='+pplants+', '+pherbs+'&price='+pprice+'&return='+preturn;}

}

So, how do i get my variable pherbs frm this script?

thanks
edmonds is offline   Reply With Quote
Old 05-09-2006, 11:44 AM   PM User | #2
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
You have an absolutelly of no use
return false which will stop the code. Remove it

And remove also the useless brackets, see red

{ document.getElementById("addr1").href=url+'prod='+pplants+', '+pherbs+'&price='+pprice+'&return='+preturn;}

and remove the absolutely useless asignment

pherbs = pherbs;
__________________
KOR
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Kor is offline   Reply With Quote
Old 05-09-2006, 12:26 PM   PM User | #3
edmonds
New Coder

 
Join Date: Nov 2005
Posts: 23
Thanks: 0
Thanked 1 Time in 1 Post
edmonds is an unknown quantity at this point
hi kor
thanks for replying.
i did what you said:

function compute1(){
var url ='greensills-cards.php?';
var preturn = 'window-boxes-herbs';
var pplants = document.getElementById("product1").value;
var pprice = document.getElementById("price1").value;
var pherbs = function get_check_value()
{
var pherbs = "";
for (var i=0; i < document.HerbWindowBoxes.herb.length; i++)
{
if (document.HerbWindowBoxes.herb[i].checked)
{
pherbs = pherbs + document.HerbWindowBoxes.herb[i].value + "\n";
}
}
}

document.getElementById("addr1").href=url+'prod='+pplants+', '+pherbs+'&price='+pprice+'&return='+preturn;

}

But it doesn't run the script - it just prints the script:

This is what i get on my php page:

Herb Window Box, function get_check_value(){var pherbs = \"\";for (var i=0; i < document.HerbWindowBoxes.herb.length; i ){if (document.HerbWindowBoxes.herb[i].checked){pherbs = pherbs document.HerbWindowBoxes.herb[i].value \"\\n\";}}}, for: £17.95

So, i need the script to run.
edmonds is offline   Reply With Quote
Old 05-09-2006, 12:31 PM   PM User | #4
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
remove red, you can not insert a function within another function in that way. And you don't need that function at all:

var pherbs = function get_check_value()
{

var pherbs = "";
for (var i=0; i < document.HerbWindowBoxes.herb.length; i++)
{
if (document.HerbWindowBoxes.herb[i].checked)
{
pherbs = pherbs + document.HerbWindowBoxes.herb[i].value + "\n";
}
}
}
__________________
KOR
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Kor is offline   Reply With Quote
Old 05-09-2006, 12:51 PM   PM User | #5
edmonds
New Coder

 
Join Date: Nov 2005
Posts: 23
Thanks: 0
Thanked 1 Time in 1 Post
edmonds is an unknown quantity at this point
hi kor
ok, tried that:

function compute1(){
var url ='greensills-cards.php?';
var preturn = 'window-boxes-herbs';
var pplants = document.getElementById("product1").value;
var pprice = document.getElementById("price1").value;
var pherbs = var pherbs = "";
for (var i=0; i < document.HerbWindowBoxes.herb.length; i++)
{
if (document.HerbWindowBoxes.herb[i].checked)
{
pherbs = pherbs + document.HerbWindowBoxes.herb[i].value + "\n";
}
}


document.getElementById("addr1").href=url+'prod='+pplants+', '+pherbs+'&price='+pprice+'&return='+preturn;

}

It wasn't too happy so i tried:

function compute1(){
var url ='greensills-cards.php?';
var preturn = 'window-boxes-herbs';
var pplants = document.getElementById("product1").value;
var pprice = document.getElementById("price1").value;
var pherbs = "";
for (var i=0; i < document.HerbWindowBoxes.herb.length; i++)
{
if (document.HerbWindowBoxes.herb[i].checked)
{
pherbs = pherbs + document.HerbWindowBoxes.herb[i].value + "\n";
}
}


document.getElementById("addr1").href=url+'prod='+pplants+', '+pherbs+'&price='+pprice+'&return='+preturn;

}

removing the 'var pherbs ='

still doesn't work.

i get this console error:
for (var i = 0;i < document.HerbWindowBoxes.herb.length;i++)
Line 1 of script
compute1();
At unknown location

thanks
tom
edmonds is offline   Reply With Quote
Old 05-09-2006, 01:14 PM   PM User | #6
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
is your form named "HerbWindowBoxes"? Are there your radios or checkboxes named all "herb"? can we see the whole code? (By the way, use the Forums feature to insert the code. For instance put your code between if it is long.)
__________________
KOR
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Kor is offline   Reply With Quote
Old 05-09-2006, 02:30 PM   PM User | #7
edmonds
New Coder

 
Join Date: Nov 2005
Posts: 23
Thanks: 0
Thanked 1 Time in 1 Post
edmonds is an unknown quantity at this point
aha!
quite right
thanks kor
the checkboxes were called something else.

renamed them and now it works
thanks
edmonds is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:18 PM.


Advertisement
Log in to turn off these ads.