pdhharris Posted December 9, 2008 Share Posted December 9, 2008 I need to call a javascript function to add dates (the user can add as many dates as they want to the form). It doesn't seem to be working. The link <a href > onclick works, just not the part within the foreach loop. Here is the section of code: <p>Program Date(s): <!--- If dates are set, re-add them ---> <font color="#FF0000"><strong><?php echo $dateErr ?></strong></font> <div id="area"></div> <br /> <?php if($DATES_SET == 1) { foreach($_POST["date"] as $theDate) { printf("<script language='javascript'>addArea(\"%s\")</script>", $theDate); } } ?> <a onclick='addArea("01-02-2009")' href='#'>add a date</a> <br /> <input type='hidden' value='0' id='intVal'/> </fieldset> <br /> <fieldset> Quote Link to comment Share on other sites More sharing options...
Psycho Posted December 9, 2008 Share Posted December 9, 2008 Need more info here. What is the JS function addArea() supposed to do? Can't provide any reason why it should/should not work within the loop if we have no clue what it is doing. Quote Link to comment Share on other sites More sharing options...
pdhharris Posted December 9, 2008 Author Share Posted December 9, 2008 the addArea() function adds an additional date field to a form. It stores them in an array, which is then processed for checking the dates addArea() function: <script type='text/javascript'> function addArea(myDefault) { var newArea = addElement(); //var area = document.getElementById('area').innerHTML; var area = "<input type='text' name='date[]' value='" + myDefault +" '/>"; document.getElementById(newArea).innerHTML = area; } function addElement() { var ni = document.getElementById('area'); var numi = document.getElementById('intVal'); var num = (document.getElementById('intVal').value -1)+ 2; numi.value = num; var newdiv = document.createElement('div'); var divIdName = 'my'+num+'Div'; newdiv.setAttribute('id',divIdName); ni.appendChild(newdiv); return divIdName; } </script> Quote Link to comment Share on other sites More sharing options...
Psycho Posted December 9, 2008 Share Posted December 9, 2008 The probelem is probably due tot he fact that the page isn't fully loaded and you are trying to add elements. But, there's no need to use JavaScript. Just use the PHP code to create those elements. You're making your page way to dependant on JavaScript when it doesn't need to be and shouldn't be. <p>Program Date(s): <!--- If dates are set, re-add them ---> <font color="#FF0000"><strong><?php echo $dateErr ?></strong></font> <div id="area"></div> <br /> <?php $date_count = 0; if($DATES_SET == 1) { foreach($_POST["date"] as $theDate) { $date_count++; echo "<div id=\"my{$date_count}Div\">"; printf("<input type='text' name='date[]' value='%s'/>", $theDate); echo "</div>\n"; } } ?> <a onclick='addArea("01-02-2009")' href='#'>add a date</a> <br /> <input type='text' value='<?php echo $date_count; ?>' id='intVal'/> </fieldset> <br /> <fieldset> Quote Link to comment Share on other sites More sharing options...
pdhharris Posted December 10, 2008 Author Share Posted December 10, 2008 Thanks very much! It worked well. I am rather new to the developing scene when it comes to Javascript, but I like the features that it can provide. Thanks for helping me actually decode what it was doing as well. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.