Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Nov 2005
    Posts
    23
    Thanks
    0
    Thanked 1 Time in 1 Post

    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

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    New Coder
    Join Date
    Nov 2005
    Posts
    23
    Thanks
    0
    Thanked 1 Time in 1 Post
    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.

  • #4
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #5
    New Coder
    Join Date
    Nov 2005
    Posts
    23
    Thanks
    0
    Thanked 1 Time in 1 Post
    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

  • #6
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #7
    New Coder
    Join Date
    Nov 2005
    Posts
    23
    Thanks
    0
    Thanked 1 Time in 1 Post
    aha!
    quite right
    thanks kor
    the checkboxes were called something else.

    renamed them and now it works
    thanks


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •