Again syntax problems...

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
jotae
Forum Commoner
Posts: 25
Joined: Fri Jul 02, 2010 5:49 pm

Again syntax problems...

Post by jotae »

Code: Select all

<?php
echo "<div id='menu'><dl><dt onmouseover='javascript:mostrar();'><a href='index.php'>Home</a></dt></dl>\n";
echo "<dl><dt onmouseover='javascript:mostrar();'><a href='catalogo.php'>Catálogo</a></dt></dl>\n";
echo "<dl><dt onmouseover='javascript:mostrar();'><a href='buscar.php'>Buscar</a></dt></dl>\n";
echo "<dl><dt onmouseover='javascript:mostrar();'><a href='tutor.php'>Su 1er Midi</a></dt></dl>\n";
echo "<dl><dt onmouseover='javascript:mostrar('smenu1');'>Midi Tutor</dt>\n"; //THIS LINE PRODUCE A SYNTAX ERROR....
echo "<dd id='smenu1'>\n";
echo "<ul>\n";
echo "<li><a href='miditut.php'>Inicio</a></li>\n";
echo "<li><a href='midicos.php'>Midi Cosas</a></li>\n";
echo "<li><a href='midigm.php'>General Midi</a></li>\n";
echo "<li><a href='midique.php'>Qu&eacute; Hacer</a></li>\n";
echo "<li><a href='midicom1.php'>Compu 1</a></li>\n";
echo "<li><a href='midicom2.php'>Compu 2</a></li>\n";
echo "</ul></dd></dl>\n";
echo "<dl><dt onmouseover='javascript:mostrar();'><a href='mailto:prolatin@cwpanama.net'>Cont&aacute;ctenos</a></dt></dl>\n
echo "<dl><dt onmouseover='javascript:mostrar();'><a href='http://www.prolatin.com'>Prolatin</a></dt></dl></div>";
?>
The menu work but submenu (smenu1) don't work. Can help me, please?
The original code is ...javascript:mostrar .....
Thx.
oscardog
Forum Contributor
Posts: 245
Joined: Thu Oct 23, 2008 4:43 pm

Re: Again syntax problems...

Post by oscardog »

What error does it print on the screen?

Also you're using invalid HTML. You should be using doubles quotes(") not single quotes('). So when outputting your first line you use:

Code: Select all

echo "<div id='menu'><dl><dt onmouseover='javascript&#058;mostrar();'><a href='index.php'>Home</a></dt></dl>\n";
It should be:

Code: Select all

echo "<div id=\"menu\"><dl><dt onmouseover='javascript&#058;mostrar();'><a href=\"index.php\">Home</a></dt></dl>\n";
That probably isn't causing the syntax error but it's probably not helping either.
DaiLaughing
Forum Commoner
Posts: 76
Joined: Thu Jul 16, 2009 8:03 am

Re: Again syntax problems...

Post by DaiLaughing »

I'm not sure you should be using definition lists either as these don't seem to be definitions!

The dog called oscar said you should use escaped double quotes and he's right because this line is wrong because the single quotes are confusing you and the browser:

Code: Select all

echo "<dl><dt onmouseover='javascript&#058;mostrar('smenu1');'>Midi Tutor</dt>\n"; //THIS LINE PRODUCE A SYNTAX ERROR....
To the browser this will look like:

[text]<dl><dt onmouseover='javascript:mostrar('smenu1');'>Midi Tutor</dt>[/text]

So the onmouseover attribute's value will be:

[text]javascript:mostrar([/text]

because it ends at the next single quote. The rest of what you intended to be in that value is now garbage as far as the browser is concerned. If you had used this:

Code: Select all

echo "<dl><dt onmouseover=\"javascript:mostrar('smenu1');\">Midi Tutor</dt>\n";

There would have been no problem.
jotae
Forum Commoner
Posts: 25
Joined: Fri Jul 02, 2010 5:49 pm

Re: Again syntax problems...

Post by jotae »

Definitively doesn't work neither with double quotation marks neither with simple quotation marks. The menu appears but when I am about running the submenu, the options disappear and it is blocked. This is the complete code (changing quotation)

Code: Select all

<?php
echo '<div id="menu"><dl><dt onmouseover="javascript:mostrar();"><a href="index.php">Home</a></dt></dl><br />';
echo '<dl><dt onmouseover="javascript:mostrar();"><a href="catalogo.php">Catálogo</a></dt></dl><br />';
echo '<dl><dt onmouseover="javascript:mostrar();"><a href="buscar.php">Buscar</a></dt></dl><br />';
echo '<dl><dt onmouseover="javascript:mostrar();"><a href="tutor.php">Su 1er Midi</a></dt></dl><br />';
echo '<dl><dt onmouseover="javascript:mostrar("smenu1");">Midi Tutor</dt><br />';
echo '<dd id="smenu1"><br /><ul><br />';
echo '<li><a href="miditut.php">Inicio</a></li><br />';
echo '<li><a href="midicos.php">Midi Cosas</a></li><br />';
echo '<li><a href="midigm.php">General Midi</a></li><br />';
echo '<li><a href="midique.php">Qu&eacute; Hacer</a></li><br />';
echo '<li><a href="midicom1.php">Compu 1</a></li><br />';
echo '<li><a href="midicom2.php">Compu 2</a></li><br />';
echo '</ul></dd></dl><br />';
echo '<dl><dt onmouseover="javascript:mostrar();"><a href="mailto:prolatin@cwpanama.net">Cont&aacute;ctenos</a></dt></dl><br />';
echo '<dl><dt onmouseover="javascript:mostrar();"><a href="http://www.prolatin.com">Prolatin</a></dt></dl></div>';
?>
This is the function

function mostrar(id) {
var d = document.getElementById(id);
for (var i = 1; i<=10; i++) {
if (document.getElementById('smenu'+i)) {document.getElementById('smenu'+i).style.display='none';}
}
if (d) {d.style.display='block';}
}

thx
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: Again syntax problems...

Post by Jade »

Code: Select all

function mostrar(id) {
var d = document.getElementById(id);
for (var i = 1; i<=10; i++) {
if (document.getElementById('smenu'+i)) {document.getElementById('smenu'+i).style.display='none';}
}
if (d) {d.style.display='';} //sometimes if you use block it doesnt remove the display=none status 
}
jotae
Forum Commoner
Posts: 25
Joined: Fri Jul 02, 2010 5:49 pm

Re: Again syntax problems...

Post by jotae »

I think the function don't have problem because this is the original code in HTML and work perfectly:
<div id="menu">
<dl>
<dt onmouseover="javascript:mostrar();"><a href="index.php">Home</a></dt>
</dl>
<dl>
<dt onmouseover="javascript:mostrar();"><a href="catalogo.php">Catálogo</a></dt>
</dl>
<dl>
<dt onmouseover="javascript:mostrar();"><a href="buscar.php">Buscar</a></dt>
</dl>
<dl>
<dt onmouseover="javascript:mostrar();"><a href="tutor.php">Su 1er Midi</a></dt>
</dl>
<dl>
<dt onmouseover="javascript:mostrar('smenu1');">Midi Tutor</dt>
<dd id="smenu1">
<ul>
<li><a href="miditut.php">Inicio</a></li>
<li><a href="midicos.php">Midi Cosas</a></li>
<li><a href="midigm.php">General Midi</a></li>
<li><a href="midique.php">Qu&eacute; Hacer</a></li>
<li><a href="midicom1.php">Compu 1</a></li>
<li><a href="midicom2.php">Compu 2</a></li>
</ul>
</dd>
</dl>
<dl>
<dt onmouseover="javascript:mostrar();"><a href="mailto:prolatin@cwpanama.net">Cont&aacute;ctenos</a></dt>
</dl>
<dl>
<dt onmouseover="javascript:mostrar();"><a href="http://www.prolatin.com">Prolatin</a></dt>
</dl>
</div>
But when I translate the code to php, don't work. That's my problem.
DaiLaughing
Forum Commoner
Posts: 76
Joined: Thu Jul 16, 2009 8:03 am

Re: Again syntax problems...

Post by DaiLaughing »

You haven't changed the quotes as oscar dog explained. That's the problem. Note the double quotes in his example and the extra slashes as well.
oscardog
Forum Contributor
Posts: 245
Joined: Thu Oct 23, 2008 4:43 pm

Re: Again syntax problems...

Post by oscardog »

Yeh, in your most recent code post you have added in double quotes but unfortunately you have not escaped them. You must add a \ before a " otherwise it will break the echo.
jotae
Forum Commoner
Posts: 25
Joined: Fri Jul 02, 2010 5:49 pm

Re: Again syntax problems...

Post by jotae »

Well, I change the line as you say oscardog:

Code: Select all

echo "<dl><dt onmouseover=\"javascript:mostrar(\"smenu1\");\">Midi Tutor</dt>\n";
But see what happen here, please:
httP://sml.prolatin.com/menu.php

The sub-menu dissapears. Can't access them. The menu don't have any Css treatment yet.
DaiLaughing
Forum Commoner
Posts: 76
Joined: Thu Jul 16, 2009 8:03 am

Re: Again syntax problems...

Post by DaiLaughing »

I get a 404 error
jotae
Forum Commoner
Posts: 25
Joined: Fri Jul 02, 2010 5:49 pm

Re: Again syntax problems...

Post by jotae »

Ready now, sorry...
DaiLaughing
Forum Commoner
Posts: 76
Joined: Thu Jul 16, 2009 8:03 am

Re: Again syntax problems...

Post by DaiLaughing »

The HTML shows you still don't have your PHP right. Don't worry about that though as quotes are probably the hardest single bit to get right in PHP!

<dl><dt onmouseover="javascript:mostrar("smenu1");">Midi Tutor</dt>

Can you see that the browser will treat the two sets of quotes I have turned red as the start and finish of the attribute value for onmouseover? What you meant to have was:

<dl><dt onmouseover="javascript:mostrar('smenu1');">Midi Tutor</dt>

That is possible by using single quotes in the Javascript and escaped double quotes in the HTML all with double quotes around the ECHO content! Try this:

echo "<dl><dt onmouseover=\"javascript&#058;mostrar('smenu1');\">Midi Tutor</dt>\n";

As I said knowing what to use where is a nightmare. My brain still hurts trying to explain this now. If you want to understand this more try this page:

http://www.yourwebskills.com/phpquotes.html

then do the same structure for all your other code - double quotes in PHP, escaped double quotes in ECHOed HTML and single quotes in Javascript. There are other ways but that was the one I found least difficult in the end.
jotae
Forum Commoner
Posts: 25
Joined: Fri Jul 02, 2010 5:49 pm

Re: Again syntax problems...

Post by jotae »

Wow!! Thanks very much!! Working perfect. Really I'm very confussed about syntax in Php because all my life I work with ASP. Regards.

You can see working in http://sml.prolatin.com/demos.php.

Now I go to change all my pages. Thx!!!
Post Reply