SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Evangelist
    Join Date
    Mar 2011
    Location
    Bellingham, WA
    Posts
    450
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Select menus and forms

    Hello!

    I have a webpage with 3 different forms, each of which "shows itself" (via javascript) under different circumstances. Information is then submitted to my database via php. There's some information in common to all 3 forms that I'd like to pull off from a select menu and then submit as part of the form; instead of writing my select menu 3 times (which is actually much longer than the one provided), I'm wondering if I can use javascript to pull off the selected response in combination with some sort of "hidden" field on my form to make it work so that when I submit the form, the selected pull-down menu item goes along with the form in question. As an example, using "quasi-code":

    Code HTML4Strict:
    <select name="chapter" id="chapter">
      <option value="Linear_Functions">Linear Functions</option>
      <option value="Quadratic_functions">Quadratic Functions</option>
    </select>  //whichever is chosen here I'd like to submit with the form that the person responds to below
     
    <form1 multiple choice type question input stuff>
    <form2 free response type question input stuff>
    <form3 numeric type question input stuff>  //only one of the 3 forms will be seen by the user.

    Any help would be appreciated.

    Thank you,

    Eric
    Last edited by paul_wilkins; Mar 20, 2011 at 02:37. Reason: Fix code display problem

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Place a hidden value within each form, so that when the form is updated it can update that hidden value when submitted.

    Code javascript:
    <input type="hidden" name="chapter">

    Code javascript:
    function updateForm() {
        var select = document.getElementById('select');
        this.chapter.value = select.options[select.selectedIndex].value;
    }
    var form1 = document.getElementById('form1'),
        form2 = document.getElementById('form2'),
        form3 = document.getElementById('form3');
    form1.onsubmit = updateForm;
    form2.onsubmit = updateForm;
    form3.onsubmit = updateForm;
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Evangelist
    Join Date
    Mar 2011
    Location
    Bellingham, WA
    Posts
    450
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Very cool! Thanks so much....

    -Eric


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
  •