1 Replies - 1010 Views - Last Post: 15 September 2010 - 12:47 PM

#1 denewey  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 18-May 09

function in onload event not working

Posted 14 September 2010 - 09:02 PM

I've been developing an application that uses a combination of Javascript and PHP. It's a sizeable form that includes the option for the user, on certain form objects, to add additional text boxes for multiple answers. I created a Javascript function that uses the insertRow() method. The user can click on a link which adds a row with a text box for an additional response.

My function works great and, after some work around I got the values to post correctly, but where I'm running into trouble is re-populating those additional fields if I have to bring the user back to edit input that was not put in correctly.

My solution was to have create a PHP varible which is a javascript function call, e.g.:

 $var = "js_function('varName1','varName2', 'varName3', 1 , '" . $post_val . "')";



using the posted variables that apply and then echoing that into the onload event handler in the body tag. This calls the same function that was originally used to add the row to have it add the row again and populate it with the already submitted data.

It all looks great, like it should work, but it doesn't and I'm wondering if there is something about the onload event handler which I don't know about. It was my understanding that the event handler executes when the page is fully loaded. If that is the case, my solution should work.

I don't know if there's some error I'm not seeing or if I'm trying to make the onload event handler do something it's not supposed to. I used a similar solution on another application and it works there.

To give a better understanding, here is the function:

function appendIaRow(tblId, valID, oName, Nbr, populate1, populate2)
{
	var tbl = document.getElementById(tblId);
	var newRow = tbl.insertRow(tbl.rows.length);	
	var numi = document.getElementById(valID);
  	var num = (document.getElementById(valID).value -1)+ 2;
  	numi.value = num;
	var rowID = 'Row'+num;
	newRow.setAttribute('id',rowID);
	var newCell = newRow.insertCell(0);
	newCell.innerHTML = "<input type=\"text\" name=\""+oName+"Name["+num+"]\" value=\""+populate1+"\" size=\"20\"  maxlength=\"150\"/>&nbsp;<a href=\"Javascript:;\" onclick=\"deleteLastRow('"+tblId+"','"+rowID+"');\" style=\"text-decoration:none\">X</a><br /><br />";
	var newCell2 = newRow.insertCell(1);
	if (Nbr == 1){
		newCell2.innerHTML = "";
		}
	else{
	    newCell2.innerHTML = "# <input type=\"text\" name=\""+oName+"Number["+num+"]\" value=\""+populate2+"\" size=\"17\" maxlength=\"17\"/><br /><br />";
		}
}



Here is the coding for the event handler('initialize()' is a different function altogether - that works):

<body onload="initialize(),<?php echo $init ?>;">



And here is what it looks like from the source code when the page is displaying in a browser:

<body onload="initialize(),appendIaRow('AdmTbl','AdmValue','Administrator', 1, 'Joan'),;">



Is This A Good Question/Topic? 0
  • +

Replies To: function in onload event not working

#2 neit  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 187
  • Joined: 13-February 09

Re: function in onload event not working

Posted 15 September 2010 - 12:47 PM

I'm no expert on javascript but is the last comma between the ) and ; supposed to be there, it does'nt look right to me
and are you not supposed to separate calling functions with ; instead of a comma.
As I have said no expert on this but every example I have seen does.

This post has been edited by neit: 15 September 2010 - 12:47 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1