Hi, I am generating a table for use as a revision form. The following line:

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);

has the php function showDisplayName() embedded in it. This function will not work. If I write the identical line in a pure HTML page it works fine.

Attachments: reviseTbl.png Generated output table
generatedHTML.png Generated code
ContactRevise.php Source code

Would appreciate any help in making the changes so it works.

Thanks in advance, RP

Recommended Answers

All 6 Replies

Strings in javascript need single quotes around them. Add \' before and after your variables.

If you mean:

...onfocus='showDisplayName(\'$v[1]\', \'$v[2]\');'
id='%s'...

it doesn't work. This is generated code:

...name="display_name" onfocus="showDisplayName(\" frank="" &="" doady\',="" \'paddon1009\');'="" id="display_name"...


Can you show me code please?

Thanks, RP

printf(
  '<td><input type="text" name="%s" onfocus="showDisplayName(\'%s\', \'%s\');" id="%s" value="%s"></input></td></tr>\n', 
  strtolower($key), $v[1], $v[2], strtolower($key), $value);

If there are quotes in $v they might be messing it up.

Thanks again,

After looking at the results of incorporating your change to my code I realize I am making a major logic error.

printf(
    '<td><input type="text" name="%s" onfocus="showDisplayName(\'%s\', \'%s\');" id="%s" value="%s"></input></td></tr>\n',
    strtolower($key), $v[1], $v[2], strtolower($key), $value);

in place of \'%s\', \'%s\' I need to put something like $(#last_name.val())+", "+$(#first_name.val()) which means I have to mingle jquery with PHP. What my original code does not do is insert the changed values of first_name and last_name to create display_name.

Is this possible??

RP

You are using jQuery. I would leave out the parameters for showDisplayName(), and just get them in that function. Is that what you mean ?

:):) SOLVED;

I have used this function in another application.
Can it somehow be called from within my PHP program? I just answered my own question.

I put the following function in printf() statements and it works just fine.

Thanks for your time in helping me.

RP,

<script type="text/javascript" >

           function showDisplayName() {
     	       	var first = document.getElementById('fname');
    		var second = document.getElementById('lname');
    		 var third = document.getElementById('dname');
		third.value=second.value+", "+first.value;
	}

</script>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.