Results 1 to 5 of 5

Thread: how to pass div values to form

  1. #1
    Join Date
    Aug 2013
    Posts
    79
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default how to pass div values to form

    i have a div values that i want to hide and then pass its value into form
    I tried this below. any help


    Code:
    <div id="records" type="hidden">
    <input type="text" name="records" value="records">
    </div>

  2. #2
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    2,027
    Thanks
    16
    Thanked 315 Times in 313 Posts
    Blog Entries
    13

    Default

    Unfortunately you can't pass div values (there isn't such a thing) - you'd pass the input as a hidden field instead;
    Code:
    <input type="hidden" name="records" value="records">
    Here you are only passing the word "records" though, which may be correct, but it's also likely that you'd want to pass a dynamic variable via php for example;
    Code:
    <input type="hidden" name="records" value="<?php echo $records;?>">
    Focus on Function Web Design | Latest News RSS | Facebook | Twitter |
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps |
    The only limit to creativity is imagination: JemCon.org

  3. #3
    Join Date
    Jun 2014
    Location
    USA
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I think I know what you are trying to do. This should work for you. I hope this helps. What this does is JavaScript will grab whatever is inside of the HTML div tag with the ID queal to "records" and place it into the value attribute of your input html element that has the ID equal to "form_input_box" when the webpage loads completely.


    Code:
    <script
    var rec=document.getElementById("records");
    var form=document.getElementById("form_input_box");
    
    window.onload=function()// This runs the code once your browser has finished loading the webpage.
    {
       form.value=rec.innerHTML; // This takes the  value from Records and places it into the form_input_box.
    }
    </script>
    
    <div id="records">
    Content goes in here!
    </div>
    
    <input type="text" name="records" value="records" id="form_input_box">

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,138
    Thanks
    44
    Thanked 3,231 Times in 3,192 Posts
    Blog Entries
    12

    Default

    Element div cannot technically have value or type properties. And you cannot pass the value of any of its valid properties without javascript or perhaps server side code (only by copying). You have posted (or someone else has moved the post) to the HTML section, so it's hard to understand what language you intend to do this in. Could you be more specific about what you're trying to accomplish and using what language?
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Jul 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    If you have the need to submit content rendered inside a <div> element along with other form elements try this.
    For every <div> a <textarea> is created and filled with the inner HTML of the div. The name attribute for the <div> is id of the form+'div'. The value of the id attribute of the above mentioned <div> will be used as the name attribute of the <textarea>

    Code:
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title></title>
        </head>
        <body>
            <form id="form" name="formname" action="index.html" method="get" onsubmit="div2form('form')">
                <input id="i1" name="i1name" type="text" value="10">
                <div id=t1 name="formdiv">sdajda;lkdja;ljdal;dj</div>
                <div id=t2 name="formdiv">aaaaaaaaaaaaaaaaaaaaaa</div>           
                <input type="submit" name="submit" value="submit">
            </form>
     </body>
        <script type="text/javascript" >
        function div2form(id){
            var form=document.getElementById(id);
            if(!form){
                return;
            }
            var divs=document.getElementsByName(id+'div')
            var i, ndivs=divs.length;
            for(i=0;i<ndivs;i++){
                if(document.getElementById('textarea'+divs[i].id)){
                   document.getElementById('textarea'+divs[i].id).value=divs[i].innerHTML; 
                } else {
                    var texta=document.createElement('TEXTAREA');
                    texta.name=divs[i].id;
                    texta.id='textarea'+divs[i].id;
                    texta.value=divs[i].innerHTML;      
                    texta.style.display='none';
                    form.appendChild(texta);                
                }                  
            }          
        }
        </script>

Similar Threads

  1. jQuery ajax pass two values
    By keyboard in forum JavaScript
    Replies: 3
    Last Post: 02-29-2012, 10:27 PM
  2. Resolved How to pass several values from radio buttons
    By bluewalrus in forum PHP
    Replies: 5
    Last Post: 05-12-2009, 04:13 AM
  3. How to pass values??
    By karchana in forum Other
    Replies: 6
    Last Post: 10-15-2007, 05:09 AM
  4. pass 2 different values in a form?
    By munkynpunky in forum HTML
    Replies: 5
    Last Post: 10-05-2006, 01:02 AM
  5. Replies: 6
    Last Post: 06-08-2006, 05:21 PM

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
  •