Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Puzzled!! why won't this javascript work when I write it with PHP 1

Status
Not open for further replies.

sd0t1

IS-IT--Management
Mar 14, 2007
131
US
I wrote a simple piece of java that for a radio button that will show another set of radio buttons if you select "yes" or it will hide them if you select "no".

Then I put it in a PHP loop that writes the java per row.

However, when PHP writes it, it doesn't work. If I create it in HTML with Javascript it works fine.



Here is the html/javascript (that works)

<div id='showbutton[1]' >
YES<input type="radio" name="press1" value="1" />NO<input type="radio" name="press1" value="2"/>
</div>

<p>YES
<input type="radio" name="press" value="1" onclick="MM_showHideLayers('showbutton[1]','','show')" />
NO
<input type="radio" name="press" value="2" onclick="MM_showHideLayers('showbutton[1]','','hide')" />
</p>




<p>
<div id='showbutton[2]' >
YES<input type="radio" name="press1" value="1" />NO<input type="radio" name="press1" value="2"/>
</div>
</p>
<p>

Yes<input type='radio' name='press' value='1' onclick="MM_showHideLayers('showbutton[2]','','show')">
No<input type='radio' name='press' value='2' onclick="MM_showHideLayers('showbutton[2]','','hide')">


Here is the same code in a PHP echo statement. it echo's just fine onto the screen, however the java doesn't work.

<?php
echo "


<div id='showbutton[1]' >
YES<input type='radio' name='press1' value='1' />NO<input type='radio' name='press1' value='2'/>
</div>

<p>YES
<input type='radio' name='press' value='1' onclick='MM_showHideLayers('showbutton[1]','','show')' />
NO
<input type='radio' name='press' value='2' onclick='MM_showHideLayers('showbutton[1]','','hide')' />
</p>




<p>
<div id='showbutton[2]' >
YES<input type='radio' name='press1' value='1' />NO<input type='radio' name='press1' value='2'/>
</div>
</p>
<p>

Yes<input type='radio' name='press' value='1' onclick='MM_showHideLayers('showbutton[2]','','show')'>
No<input type='radio' name='press' value='2' onclick='MM_showHideLayers('showbutton[2]','','hide')'>



";
?>
 
PHP is a server-side scripting language. It can not directly interact with users, so to do this with PHP you would have to submit the page to the server so that it can be re-processed and the new page be re-displayed to the user.
 
Or not. I didn't read it closely enough. I'll have to come back to this when I'm not as tired.
 
First its Javascript, not Java, Java is a completely different language to Javascript.

Its probably a quoting issue, since you have everything inside in single quotes, Once it gets displayed out into the browser it doesn't know where the onclick event is supposed to end.

For Example:
Code:
<p>YES
   <input type='radio' name='press' value='1' onclick=[red]'[/red]MM_showHideLayers([red]'[/red]showbutton[1]','','show')' />

Your onclick event starts with a single quote, but then in the middle of your Javascript MM_showHideLayers(' there's a another single quote. So it technically thinks the Onclick ends there, which means its a malformed javascript. so it can't run.

What you can do is use heredoc syntax with your echo statement, instead of your double quotes. That way you can still use your double quotes in the html and javascript to correctly surround the events and the inner parameters.

Heredoc syntax allows you to use straight html with double and single quotes without interfering in anything else.


Code:
echo [red]<<<EOS[/red]

<div id='showbutton[1]' >
YES<input type="radio" name="press1" value="1"  />NO<input type="radio" name="press1" value="2"/>
</div>

<p>YES
   <input type="radio" name="press" value="1" onclick="MM_showHideLayers('showbutton[1]','','show')" />
  NO
  <input type="radio" name="press" value="2" onclick="MM_showHideLayers('showbutton[1]','','hide')" />
</p>


<p>
<div id='showbutton[2]' >
YES<input type="radio" name="press1" value="1"  />NO<input type="radio" name="press1" value="2"/>
</div>
</p>
<p>

Yes<input type='radio' name='press' value='1' onclick="MM_showHideLayers('showbutton[2]','','show')">  
No<input type='radio' name='press' value='2' onclick="MM_showHideLayers('showbutton[2]','','hide')"> 

[red]EOS;[/red]



----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Thanks Vacunita, the heredoc worked.

Although it wasn't the quotes (single vs double) I had already tried that, but somehow when I run it through the herodoc it works.
 
Its not the type of quote, but how they are laid out.

As I said above you start your event as:

Code:
onclick='MM_showHideLayers('showbutton[1]'

having the the same type of quote inside itself will do that.

so your onclick event basically consisted of [red]MM_showHideLayers([/red]

nothing after that was getting used, since the single quote there effectively terminates the event.

Anyway glad you got it working.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top