www.webdeveloper.com
Results 1 to 4 of 4

Thread: Hidden form field Create / Destroy

  1. #1
    Join Date
    Feb 2009
    Posts
    6

    Hidden form field Create / Destroy

    Hi,

    I have been busy developing my calender / booking system when I ran into a few PHP issues. I got some help with that but now im stuck on a javascript issue.
    My calender has cells that use onlick to change colour (to display that they have been selected) and also a function to toggle a value of 1 or 0 to a hidden form field for each day of the month. Each field would be assigned an id of a combination of the month and day so for example, on the 5th of feb if the cell was selected the value 1 would be put to the form field with the id february5.
    This was all well and good until I passed it to a php script and tried to do anything with it!
    So after some advice I have revised what im doing;
    I am creating the form field with a value of 1 if it is selected, if it is de-selected I am destroying it.
    The code I have so far is this;
    Code:
    function booking(id) { 
    
    var currentDay = document.getElementById(id);
    
          if (document.getElementById(id).value == null)
          { 
             var newNode = "<input type='hidden' name='" + id + "' id='" + id + "' value='1'>";
             var parent = document.getElementById("content");
             parent.insertBefore(newNode,document.getElementById(id));
          }
          else
          {
                  currentDay.parentNode.removeChild(currentDay);
          }
    }
    Within the TD for each cell I have;
    Code:
    onClick=\"booking('$titlelower$daynum')
    So it passes through,for example, february5.

    When I still had the form fields typed in for each cell the destroy part of the script was working, I just dont seem to be able to get the create part to function.
    Any ideas?

  2. #2
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    At least 98% of internet users' DNA is identical to that of chimpanzees

  3. #3
    Join Date
    Feb 2009
    Posts
    6
    Hi,

    Although ive only been testing this in firefox so far I had a read through and have adapted my script.
    Ive also been about and read up a bit more but i am still stuck fast. Firefox keeps throwing up an error on line 28 yet my script goes to 26 only?
    The way the function is called in the html has remained the same but my code is now this;

    Code:
    function booking(id)
    {
    
    var currentDay = document.getElementById(id);
    var hiddenField = null;
    var newField = null;
      newField = document.createElement("<input type='hidden' name='" + currentDay + "' id='" + currentDay + "' value=1>");
      hiddenField = document.getElementById("currentDay");
      document.body.insertBefore(newField, hiddenField);
    }
    I am just trying to create the input field first before worrying about destroying it now.
    Anyone have any ideas where im going wrong? Im beginning to think ive made a silly mistake somewhere as it really doesnt want to know whatever I change.

  4. #4
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    Code:
    function booking(elmID){
    var currentDay = document.getElementById(elmID).innerHTML; //must be a value not an object!
    var hiddenField = null;
    var newField = null;
    try { // IE
        newField = document.createElement('<input type="hidden" name="' + currentDay + '" id="' + currentDay + '" value="1">');
        } 
    catch (e) { // DOM
        newField=document.createElement('input');
        newField.setAttribute('type', 'hidden');
        newField.setAttribute('name', currentDay);
        newField.setAttribute('id', currentDay);
        newField.setAttribute('value', 1);
        }
    hiddenField = document.getElementById("currentDay");
    document.forms[0].insertBefore(newField, hiddenField);
    }
    Last edited by Fang; 02-08-2009 at 07:52 AM.
    At least 98% of internet users' DNA is identical to that of chimpanzees

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles