Referencing a javascrip variable in html within PHP

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
jak82
Forum Newbie
Posts: 11
Joined: Mon Oct 15, 2007 4:21 am

Referencing a javascrip variable in html within PHP

Post by jak82 »

Hi Guys,

Help !

I am trying to pass a variable in a function from php/html to javascript,

$loginsummary .= "
<table>
<tr>
<td><input name='user' type='text' id='user' value='' onChange='doit('hello')' /></td>


The problem seems to be with how many apostrophes is before 'hello' the function works if I remove the hello part so it is fine calling it, i just need to be able to set a variable.

Kind Regards,

C
gethinw
Forum Newbie
Posts: 16
Joined: Tue Sep 23, 2008 4:02 am

Re: Referencing a javascrip variable in html within PHP

Post by gethinw »

You need double quotes around the string inside the doit() function, otherwise the single quote before the hello closes the onChange statement:

Code: Select all

onChange='doit("hello")'
Also, if that is within a php string (I can't quite work out what you're trying to do from your code) you'll need to escape the double quotes with a backslash, to stop them closing the php string:

Code: Select all

onChange='doit(\"hello\")'
Post Reply