SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Guru
    Join Date
    Oct 2011
    Posts
    658
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Radio Button value save to database

    Hi..

    I'm new in radio button.

    Now I have to radio button one for H value means half and W for Whole.

    Now I don't know how can I get the value to save in to my database.

    here is my code:

    Code:
    <input type="radio" name="CHK_LEAVE" id="H" value="H">
    <input type="radio" name="CHK_LEAVE" id="W"value="W">
    <script>
    function ApproveLeaveOP(){   
        var CHK_LEAVE = document.getElementByName('CHK_LEAVE').value;
        
        alert (CHK_LEAVE);
    }
    </script>
    I got an error Object Required.

    I tried this test:

    Code:
    function chk(){
          if(document.getElementById('H').checked) {
             alert ('H')
          }else if(document.getElementById('W').checked) {  
           alert ('W')
          }
    }
    And it works.

    Now How can I save it to my database

    I have this code and I need to insert here the code to get the value of radio button that I choose:

    Code:
    function ApproveLeaveOP(){  
    var EMP_NO = document.getElementById('EMP_NO').value;
        var DATE_LEAVE_FROM = document.getElementById('DATE_LEAVE_FROM').value;
        var DATE_LEAVE_TO = document.getElementById('DATE_LEAVE_TO').value;
        var NAME = document.getElementById('NAME').value;
        var HOURS_LEAVE = document.getElementById('HOURS_LEAVE').value;
        var Approve = document.getElementById('Approve').value;
        var TYPE = document.getElementById('TYPE').value;
        var dateprocess = document.getElementById('dateprocess').value;
       
        document.sampleform.action="AddLeave.php?EMP_NO="+EMP_NO+"&DATE_LEAVE_FROM="+DATE_LEAVE_FROM+"&DATE_LEAVE_TO="+DATE_LEAVE_TO+"&NAME="+NAME+"&HOURS_LEAVE="+HOURS_LEAVE+"&Approve="+Approve+"&TYPE="+TYPE+"&dateprocess="+dateprocess;
           document.sampleform.submit(); 
    }
    Code:
    <input type="text" name="EMP_NO" id="EMP_NO" value="{$empno}" size="8" style="background: #e2e2e2" readonly="readonly">
    <input type="text" name="NAME" id="NAME" value="{$fullname}" size="35" style="background: #e2e2e2" readonly="readonly">
    <input type="text" name="DATE_LEAVE_FROM" id="DATE_LEAVE_FROM" value="{$DATE_LEAVE_FROM}" size="9">
    <input type="text" name="DATE_LEAVE_TO" id="DATE_LEAVE_TO" value="{$DATE_LEAVE_TO}" size="9">
    
    {html_options id=TYPE name=TYPE options=$LeaveStatus selected=$TYPE style="width:140px;"}
    <input type="radio" name="CHK_LEAVE" id="H" value="H">
    <input type="radio" name="CHK_LEAVE" id="W"value="W">    
    <!--<input type="text" name="STATUS" id="STATUS" value="{$STATUS}" size="10">-->
    <input type="button" name="Approve" value="Approve" id="Approve" onclick="ApproveLeaveOP()">
    I need to save in to my database what I choose.

    Thank you

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,678
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by newphpcoder View Post
    Now I don't know how can I get the value to save in to my database.

    here is my code:

    Code:
    <input type="radio" name="CHK_LEAVE" id="H" value="H">
    <input type="radio" name="CHK_LEAVE" id="W"value="W">
    <script>
    function ApproveLeaveOP(){   
        var CHK_LEAVE = document.getElementByName('CHK_LEAVE').value;
        
        alert (CHK_LEAVE);
    }
    </script>
    I got an error Object Required.
    This occurs because there is more than one element with the same name, so getElementsByName returns you with an array-like item list, instead of a single element.

    Normally the best way to handle what you're doing here is to submit the form to a server-side page, so that only the selected values are submitted through to the server, from where the form can be processed using server-side code.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Enthusiast
    Join Date
    Apr 2008
    Location
    The Netherlands
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What do you want to create? JS validation the form? Serverside validation with AJAX call? Or just submit form and insert data into database?


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
  •