...

View Full Version : How do i get a Variable from a script?



edmonds
05-09-2006, 11:27 AM
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

Kor
05-09-2006, 11:44 AM
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;

edmonds
05-09-2006, 12:26 PM
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.

Kor
05-09-2006, 12:31 PM
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";
}
}
}

edmonds
05-09-2006, 12:51 PM
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

Kor
05-09-2006, 01:14 PM
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.)

edmonds
05-09-2006, 02:30 PM
aha!
quite right
thanks kor
the checkboxes were called something else.

renamed them and now it works
thanks



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum