SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Evangelist
    Join Date
    Oct 2001
    Posts
    439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How can I write a hidden var in form based on select Option?

    Hey guys,
    I am having trouble with a hidden variable.......I need to include it in a form only if a certain choice is made:..........Or I need to write the hidden variable to the form, if a certain item is selected from a <select>

    Here is what I tried but it failed.....(I call this function on click of submit button on form)

    eg:
    <script language="JavaScript">
    function submit(option) {

    if (option == 1) {
    document.forms.form1.method = "post";
    document.forms.form1.action = "action.php"
    document.forms.form1.submit;
    }

    else if (option == 2){
    document.write('<input type="hidden" name="myvar">');
    document.forms.form1.method = "post";
    document.forms.form1.action = "action2.php"
    document.forms.form1.submit;
    }

    }
    </script>

    The above fails to write the hidden var as part of the form, instead it writes on the action page.....

    any help is greatly appreciated

  2. #2
    SitePoint Zealot
    Join Date
    Jul 2002
    Location
    Fort Lauderdale
    Posts
    123
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm. I was thinking that instead of using document.write, declare your hidden field inside the form
    Code:
    <input type=hidden name=h>
    Then in the javascript use this to replace document.write
    Code:
    document.forms.form1.value = "myvar";
    Last edited by Vasudevelopa; Mar 4, 2003 at 18:41.

    www.unpossiblemedia.com - A Fort Lauderdale based multimedia company.

  3. #3
    SitePoint Evangelist
    Join Date
    Oct 2001
    Posts
    439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes, I also tried that, but the problem is that I am working within a CMS framework, and if the hidden var even exists for any other option it will fail.......

    SO the hidden var can only exist when option 2 is selected........

    cheers

  4. #4
    SitePoint Zealot
    Join Date
    Jul 2002
    Location
    Fort Lauderdale
    Posts
    123
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I made a little test script and I don't think document.write is going to work the way you want unless its executed at run-time.

    What is a CMS framework? Is it possible to change the code on the action page so that rather than checking if the hidden variable exists, check if its value != "". In that case you would be able to use my previous idea...

    www.unpossiblemedia.com - A Fort Lauderdale based multimedia company.

  5. #5
    SitePoint Evangelist
    Join Date
    Oct 2001
    Posts
    439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am basically working in customisations in an existing off-the-shelf CMS product, so I cannot change certain things.....That is why I cannot change the check for the hidden var.....

    Ok, is it possible to write to the document based on what is selected in the <select>
    eg:
    if (form.select.value == 1){
    document.write("<input type=hidden......");
    }

    thanks again


Bookmarks

Posting Permissions

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