Results 1 to 8 of 8
  1. #1
    Join Date
    Feb 2014
    Posts
    35

    Unanswered: different fields to appear when a choice is made

    First, I'm sorry if this is the wrong forum or that I can't choose a good title!

    I have a php/mysql system recording data about firms. At one stage the country the firm is based in is asked via a drop down list. If they choose the UK from the list then I want a new question to appear on the form asking for their VAT number.

    Can anyone give me some pointers how I go about this?

    Many thanks

  2. #2
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    Air code:
    PHP Code:
    <form action="" method="post">
        <select name="country">
            <option>UK</option>
            <option>US</option>
        </select>
        <?php if ($_POST["country"] == "UK") { ?>
            <input type="text" name="vat_number" />
        <?php ?>
        <input type="submit" />
    </form>
    George
    Home | Blog

  3. #3
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    or perhaps better yet
    have a table which identifies countries (you will probably need it in any case for your combo/list box
    in that table define if a VAT number is required. depending on the size of company you may need the VAT registration details for customers from other EU countries.
    I'd rather be riding on the Tiger 800 or the Norton

  4. #4
    Join Date
    Feb 2014
    Posts
    35
    Thank you. I was thinking about something like gvee's answer but without needing the submit ie as soon as UK was selected the vat box would appear.

    Is that possible?

  5. #5
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    yes
    if you delve into the black arts of Javascript
    I'd rather be riding on the Tiger 800 or the Norton

  6. #6
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    With JavaScript, yes. Remember though: not all your users will have JS enabled!

    HTML
    Code:
    <form action="" method="post">
        <select name="country">
            <option>UK</option>
            <option>US</option>
        </select>
        <input type="text" name="vat_number" />
    </form>
    CSS
    Code:
    input[name=vat_number] {
        display: none;
    }
    JQuery (I chose to use this library for ease)
    Code:
    $("select[name=country]").change(function () {
        if ($(this).val() == "UK") {
            $("input[name=vat_number]").show();
        } else {
            $("input[name=vat_number]").hide();
        }
    });
    DEMO: http://jsfiddle.net/0s3shdzc/
    George
    Home | Blog

  7. #7
    Join Date
    Feb 2014
    Posts
    35
    Thanks gvee.
    Looking at the demo this seems to be what I want. I'm not sure how to use this though. I would put the CSS part in the css file, the HTML in the main code. How do I put the JQuery stuff in there? (Sorry to be a bit dom about this - I've not used this before.)

  8. #8
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10

Posting Permissions

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