Jump to content

Calling Javascript function with variables in PHP


pdhharris

Recommended Posts

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>

 

 

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>

 

 

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>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.