combine text box and option box

Discussion in 'HTML & Website Design' started by xbat, Feb 6, 2012.

  1. #1
    <input type="text" name="box1"  value="" size="30" />
        
       
       <select  name="box1" >
            
            <option value="bob2"> bob2</option>
            <option value="bob3">bob3</option>
      
          </select>
    HTML:
    how would I combine this? When i click on submit I would like it to be entered into the database as - textbob2

    so whatever would be entered into the text box would combine with the option..


    thanks
     
    xbat, Feb 6, 2012 IP
  2. tolra

    tolra Active Member

    Messages:
    515
    Likes Received:
    36
    Best Answers:
    1
    Trophy Points:
    80
    #2
    You have to do it in the scripting language that you are using on the server, look up string concatenation for your chosen language.
     
    tolra, Feb 6, 2012 IP
  3. xbat

    xbat Well-Known Member

    Messages:
    326
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    105
    #3
    ok heres what i have.....




    var tmpStr = "";
    var tick = 0;

    function updateTxt(isForm,isData){

    tmpStr += isData;
    isForm.box2.value = tmpStr;
    tick++;
    if (tick == 2){tmpStr = ""; tick =0}
    }



    var tmpStr = "";
    var tick = 0;

    function updateTxt(isForm,isData){

    tmpStr += isData;
    isForm.box1.value = tmpStr;
    tick++;
    if (tick == 2){tmpStr = ""; tick =0}
    }




    <select name="box1" value="box1">

    <input type="text" name="box1" value="box1" size="30"/>



    for some reason i cannot get my box1's to combine....


    when i hit submit I only get either the dropdown selction option or just the text input...
     
    xbat, Feb 29, 2012 IP
  4. tolra

    tolra Active Member

    Messages:
    515
    Likes Received:
    36
    Best Answers:
    1
    Trophy Points:
    80
    #4
    Send it to the server as 2 elements, they both need different names, then on the server combine the 2 values before storing e.g. in PHP $_POST['select1'] . $_POST['box1'], once you have the combined string write it to the database.

    NOTE: Don't use the raw data without validation & escaping especially as it's going in a database or you will get hacked.

    Or if you want to do it in JavaScript create a hidden field, then on updates to the form copy the value of the selection and text field into the hidden form, then allow the form submission to continue. Honestly unless you have some specific reason to want to combine the values on the client it's not worth the trouble as if the user has JavaScript turned off you'll get the 2 form fields separately with the what should be combined field blank, then you'll have to combine them on the server anyway.

    Either way give your form elements different names.
     
    tolra, Feb 29, 2012 IP
  5. xbat

    xbat Well-Known Member

    Messages:
    326
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    105
    #5
    i agree on the submission.. but with what i am working with thats not a option.. this is a add on script... And i need something that will work with just javascript...


    in other words I am trying to find a solutions where I can only use javascript for combining them.
     
    xbat, Feb 29, 2012 IP
  6. tolra

    tolra Active Member

    Messages:
    515
    Likes Received:
    36
    Best Answers:
    1
    Trophy Points:
    80
    #6
    Then read the 2 values, combine them and store them in the appropriate field e.g. in the onsubmit handler document.getElementById('name').value += document.getElementById('sel').value;
     
    tolra, Feb 29, 2012 IP