Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Javascript, passing a form variable to URL

  • 22-06-2009 04:23PM
    #1
    Registered Users, Registered Users 2 Posts: 5,425 ✭✭✭


    Hi All,

    Wondering if anybody can help me out, I am looking to pass a variable from my form to a php script, I believe it has to be done by passing the variable to a new URL and using a get script to pull it from the URL into php...

    here's my problem, I have this script created, to create the drop down boxes and have the drop down boxes linked, but how do I get the form to submit the variable to open a new url with the variable in the url....

    Hope I am making myself clear, anyway here's the full script.
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    <script language="JavaScript">
    function setOptions(chosen) {
    var selbox = document.myform.opttwo;

    selbox.options.length = 0;
    if (chosen == " ") {
    selbox.options[selbox.options.length] = new Option('Please select one of the options above first',' ');

    }
    if (chosen == "1") {
    selbox.options[selbox.options.length] = new
    Option('145','oneone');
    selbox.options[selbox.options.length] = new
    Option('146','onetwo');
    selbox.options[selbox.options.length] = new
    Option('147','onethree');
    }
    if (chosen == "2") {
    selbox.options[selbox.options.length] = new
    Option('AMV8','twoone');
    selbox.options[selbox.options.length] = new
    Option('DB7','twotwo');
    selbox.options[selbox.options.length] = new
    Option('DB9','twothree');
    }
    if (chosen == "3") {
    selbox.options[selbox.options.length] = new
    Option('1 series','threeone');
    selbox.options[selbox.options.length] = new
    Option('2000','threetwo');
    selbox.options[selbox.options.length] = new
    Option('2002','threethree');
    selbox.options[selbox.options.length] = new
    Option('3 series','threefour');
    }
    }
    </script>
    </head>

    <body>
    <form name="myform"><div align="center">


    <select name="optone" size="1"
    onchange="setOptions(document.myform.optone.options
    [document.myform.optone.selectedIndex].value);">
    <option value="0" selected="selected">Make</option>
    <option value="1">Alfa Romeo</option>
    <option value="2">Aston Martin</option>
    <option value="3">BMW</option>
    </select><br> <br>
    <select name="opttwo" size="1">
    <option value=" " selected="selected">Please select one of the options above first</option>
    </select>
    <form name="myform" method="POST" action="records.php">
    <input type="hidden" name="myform">
    </div></form>
    </body>
    </html>


Comments

  • Registered Users, Registered Users 2 Posts: 21,278 ✭✭✭✭Eoin


    I'm not sure if this is what you're asking, but you can either set the value of a hidden form field and submit the form from your JavaScript, or just redirect the page:

    var myval = document.getElementById("txtField").value;
    document.location.href = 'newpage.php?param=" + myval;


  • Registered Users, Registered Users 2 Posts: 5,425 ✭✭✭robtri


    eoin wrote: »
    I'm not sure if this is what you're asking, but you can either set the value of a hidden form field and submit the form from your JavaScript, or just redirect the page:

    var myval = document.getElementById("txtField").value;
    document.location.href = 'newpage.php?param=" + myval;

    yep, I want to redirect the page to a new page called records.php and bring the variable from the drop down boxes with me in the URL,

    so I need to assign to the submit button the above href???? so when the two drop downs boxes have a selection and the user hits the submit button... it goes to the new URL record.php(with the var attached)


  • Registered Users, Registered Users 2 Posts: 21,278 ✭✭✭✭Eoin


    If you're submitting the form, then you don't need JavaScript - the value of the 3rd dropdown list will be in the post information

    [php]echo $_POST; [/php]


  • Registered Users, Registered Users 2 Posts: 5,425 ✭✭✭robtri


    eoin wrote: »
    If you're submitting the form, then you don't need JavaScript - the value of the 3rd dropdown list will be in the post information

    [php]echo $_POST; [/php]

    ok you lost me a bit.. I only have two drop down boxes...


  • Registered Users, Registered Users 2 Posts: 21,278 ✭✭✭✭Eoin


    Sorry, wasn't concentrating. If you submit the form like any other one than the selected values from the dropdown lists should be in the post values collection.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 5,425 ✭✭✭robtri


    eoin wrote: »
    Sorry, wasn't concentrating. If you submit the form like any other one than the selected values from the dropdown lists should be in the post values collection.

    cheers, sounds good, will give that a go..


  • Registered Users, Registered Users 2 Posts: 5,425 ✭✭✭robtri


    eoin wrote: »
    Sorry, wasn't concentrating. If you submit the form like any other one than the selected values from the dropdown lists should be in the post values collection.

    ok gave that a go...

    put in a standard php form action at start and submit button line..

    see new code
    </head>
    <body>
    <form name="myform"><div align="center">
    <form action="records.php" method="post">
    <select name="optone" size="1" onchange="setOptions(document.myform.optone.options [document.myform.optone.selectedIndex].value);">
    <option value="0" selected="selected">Make</option>
    <option value="1">Alfa Romeo</option>
    <option value="2">Aston Martin</option>
    <option value="3">BMW</option>
    </select>
    <br> <br>
    <select name="opttwo" size="1">
    <option value=" " selected="selected">Please select one of the options above first</option> <input type="submit" />
    </div></form>
    </body>
    </html>

    when I hit the submit button, nothing is happening, the drop down boxes remain on page, it does not go to new page "records.php" but the URL changes to
    XXXXXX.com/selentry8.php?optone=2&opttwo=twoone

    any thoughts on where I need to go


  • Registered Users, Registered Users 2 Posts: 21,278 ✭✭✭✭Eoin


    You've got two form tags there


  • Registered Users, Registered Users 2 Posts: 5,425 ✭✭✭robtri


    eoin wrote: »
    You've got two form tags there

    ohhhh man :o:o:o
    how stupid was that... dude.... it works a treat... thanks a million....


  • Registered Users, Registered Users 2 Posts: 21,278 ✭✭✭✭Eoin


    No worries, glad to be of use :)


  • Advertisement
Advertisement