i have been trying to add javascipt in my php script to implement dynamic form filling but it seems not to work. the javascript code works well without the php script what could br wrong?

var alive1 = new Array("Yes","No");
var alive2 = new Array("");
var loss = new Array("");
var dead = new Array("");
function set_ycontact(){
  var select_status= document.form1.status;
  var select_ycontact= document.form1.ycontact;
  var selected_status= select_status.options[select_status.selectedIndex].value;
  if (selected_status == "alive1"){
    for(var i=0; i < alive1.length; i++)
   select_ycontact.options[select_ycontact.options.length] = new Option(alive1[i],alive1[i]);
  if (selected_status == "alive2"){
    for(var i=0; i < alive2.length; i++)
   select_ycontact.options[select_ycontact.options.length] = new Option(alive2[i],alive2[i]);
  if (selected_status == "loss"){
    for(var i=0; i < loss.length; i++)
   select_ycontact.options[select_ycontact.options.length] = new Option(loss[i],loss[i]);
  if (selected_status == "dead"){
    for(var i=0; i < dead.length; i++)
   select_ycontact.options[select_ycontact.options.length] = new Option(dead[i],dead[i]);

the corresponding php script is :

<td width="21%" class="invisible">Patients Status </td>
		<select name="status" id="status" tabindex="6" onChange="set_ycontact();">
			<option value="" selected="selected">Select status</option>
			  <option value="alive1" <?php  if ($status=='alive1') echo "selected" ?>>Alive&nbsp;and&nbsp;promised&nbsp;to&nbsp;come&nbsp;to&nbsp;clinic</option>
			  <option value="alive2" <?php  if ($status=='alive2') echo "selected" ?>>Alive-drop&nbsp;outs</option>
			  <option value="loss" <?php  if ($status=='loss') echo "selected" ?>>Loss&nbsp;to&nbsp;follow&nbsp;up</option>
			  <option value="dead" <?php  if ($status=='dead') echo "selected" ?>>Dead</option>
            <td width="18%" class="invisible">Agreed&nbsp;To&nbsp;Yearly&nbsp;Contact</td>
            <td width="1%" class="invisible">:</td>
            <td width="25%" class="invisible">
  			<select name="ycontact">
    		<option value="" selected="selected"></option>


First, if you have Register Globals = Off in your php.ini (which is recommended), than you have to extract your $status variable from your post data, with a line like:
$status = $_POST; (or $_GET, if your form uses the Get method)
Second, change the Body tag of your html to:
<body onLoad="set_ycontact();">
so you trigger the form fields update on page load.

That's all ;)

This article has been dead for over six months. Start a new discussion instead.