'onfocus' won't work inside dynamic generated <input> line

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pizzipie
Forum Commoner
Posts: 87
Joined: Wed Feb 10, 2010 10:59 pm
Location: Hayden. ID

'onfocus' won't work inside dynamic generated <input> line

Post by pizzipie »

I am generating a table to be used as a revision form. One <input> line contains: onfocus='showDisplayName(x,y)'. The purpose of this is to produce a standardized input of 'last_name, first_name'. This works fine in a regular HTML form context, but does not work in the dynamically created form. I would appreciate any help in making this work. Thanks in advance, RP.

Generated Table Output:
reviseTbl.png

Generated HTML Code:
generatedHTML.png
My Source Code:

Code: Select all

foreach($connectData as $key => $value) {
$k[]=$key;
$v[]=$value;
		
	if($key=='Name_ID') {
		continue;
	};
		printf("<td><label for='%s'>%s</label></td>\n",strtolower($key), preg_replace("/_/"," ",$key));
		
		if($key=="Display_Name") {
			
           printf("<td><input type='text' name='%s' onfocus='showDisplayName($v[1], $v[2]);' 
           		id='%s' value='%s'></input></td></tr>\n", strtolower($key), strtolower($key), $value);
			continue;
		};		
				
		if (contains($key, "Notes")) {
			printf("<td><textarea name='%s' id='%s'  rows=5 cols=81> '%s' </textarea></td></tr>\n",strtolower($key),
			 strtolower($key), $value);
			continue;
		};
	
		printf("<td><input type='text' name='%s' id='%s' value='%s'></input></td></tr>\n", strtolower($key), strtolower($key), $value);
		
	}; // if key	
	
printf("</table></form></fieldset></div>\n");
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: 'onfocus' won't work inside dynamic generated <input> li

Post by social_experiment »

Code: Select all

onfocus="showDisplayName(Frank & Doady, Paddon3256);"
Probably the & that is causing the script not to work, if you use & instead, does it work?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: 'onfocus' won't work inside dynamic generated <input> li

Post by Celauran »

Could also be that the string arguments to your JS function aren't enclosed in quotes.
pizzipie
Forum Commoner
Posts: 87
Joined: Wed Feb 10, 2010 10:59 pm
Location: Hayden. ID

Re: 'onfocus' won't work inside dynamic generated <input> li

Post by pizzipie »

Thank you, social_experiment and Celauram,

I tried the suggestions you each gave but no-go. I input names that didn't contain & and I also tried to quote the variables to showDisplayName().

I'll continue to experiment.

Again thanks to you both,

RP
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: 'onfocus' won't work inside dynamic generated <input> li

Post by social_experiment »

Could you paste the code for the javascript function
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply