0

I have a little Problem with PHP and JavaScript. I am making a homepage with a gallery and if you click on a picture(PHP get the src address from a database) it should become greater.

Here my code:

PHP:

    echo '<h1>'."Galerie".'</h1>';
                    $r = esql("SELECT * FROM Bilder");
                    $anzahlBilder = count($r);
                    $counterForBilderInReihe = 0;
                    $counterForID = 0;
                    echo '<table>';

                    foreach($r as $bild){
                       $bildpfad = $bild['Bilderpfad']; 
                       if($counterForBilderInReihe === 0){
                           echo '<tr>';
                           echo '<td class="td"><img src="'.$bild['Bilderpfad'].'" alt="'.$bild['Bildtitel'].'" class="GalerieBilder" onclick="anzeigen('.$bildpfad.')" onMouseOver="cursorChange('.$counterForID.')" id="'.$counterForID.'"></img></td>';
                           $counterForBilderInReihe = $counterForBilderInReihe + 1;
                       }
                       else if($counterForBilderInReihe === 1){
                           echo '<td class="td"><img src="'.$bild['Bilderpfad'].'" alt="'.$bild['Bildtitel'].'" class="GalerieBilder" onclick="anzeigen('.$bildpfad.')" onMouseOver="cursorChange('.$counterForID.')" onclick="anzeigen('.$bild['Bilderpfad'].')" id="'.$counterForID.'"></img></td>';
                           $counterForBilderInReihe = $counterForBilderInReihe + 1;
                       }else if($counterForBilderInReihe === 2){
                           echo '<td class="td"><img src="'.$bild['Bilderpfad'].'" alt="'.$bild['Bildtitel'].'" class="GalerieBilder" onMouseOver="cursorChange('.$counterForID.')" onclick="anzeigen('.$bildpfad.')" id="'.$counterForID.'"></img></td>';
                           $counterForBilderInReihe = $counterForBilderInReihe + 1;
                       }else if($counterForBilderInReihe === 3){
                           echo '<td class="td"><img src="'.$bild['Bilderpfad'].'" alt="'.$bild['Bildtitel'].'" class="GalerieBilder" onMouseOver="cursorChange('.$counterForID.')" onclick="anzeigen('.$bildpfad.')" id="'.$counterForID.'"></img></td>';
                           $counterForBilderInReihe = $counterForBilderInReihe + 1;
                       }else{
                           echo '</tr>';
                           $counterForBilderInReihe = 1;
                           echo '<tr>';
                           echo '<td class="td"><img src="'.$bild['Bilderpfad'].'" alt="'.$bild['Bildtitel'].'" class="GalerieBilder" onMouseOver="cursorChange('.$counterForID.')" onclick="anzeigen('.$bildpfad.')" id="'.$counterForID.'"></img></td>';
                       }
                       $counterForID = $counterForID + 1;
                   }
                   if($counterForBilderInReihe === 0){
                       echo '</tr>';
                   }else{

                   }
                   echo '</table>';

JavaScript:

    function cursorChange(id){
console.log("cursorchange!");
document.getElementById(id).style.cursor = "pointer";}


    function anzeigen(pfad){
console.log("anzeigen");
window.alert(pfad);}

My Problem here is that the function anzeigen(pfad) doesnt work but the function cursorChange(id) works fine. The Problem specificly is that if I click on a picture it doesnt call the function anzeigen(). I can see this because of the console.log(). And the variable pfad or $bildpfad is a string value. So what is the Problem?

Thanks for helping

Rene

5
  • 2
    Yep, what's the problem? What does "doesn't work" mean? Notice also, that img tag should not have ending tag. Commented Jun 16, 2014 at 9:40
  • what is coming in your 'bildpfad' variable in anzeigen function..Is it giving string value.. Commented Jun 16, 2014 at 9:43
  • The problem is that I cant call the function anzeigen(). So if I click on a picture it doesnt do the console.log Commented Jun 16, 2014 at 9:47
  • I strongly suggest you use JQuery to do this - onclick events hard coded into the HTML doc are not the way to do things. If you really need to do pure Javascript try using addEventListener instead. Commented Jun 16, 2014 at 9:48
  • Ok, I will try to do it with JQuery Commented Jun 16, 2014 at 9:57

2 Answers 2

1

Try this....

Use escape character for passing string value in javascript function call on click

echo '<td class="td"><img src="'.$bild['Bilderpfad'].'" alt="'.$bild['Bildtitel'].'" class="GalerieBilder" onclick="anzeigen(\''.$bildpfad.'\')" onMouseOver="cursorChange('.$counterForID.')" id="'.$counterForID.'"></img></td>';
Sign up to request clarification or add additional context in comments.

1 Comment

YES!! That´s it! Thank you
0

Typical German programming, never use one short word where 5 long ones will do. :-)

There is a lot of duplicated code (I've removed most but not all) and some of your code has 2 onclick events instead of just one, and in any event they should not really be in the code (see my earlier comment).

echo '<h1>'."Galerie".'</h1>';
$r = esql("SELECT * FROM Bilder");
$anzahlBilder = count($r);
$counterForBilderInReihe = 0;
$counterForID = 0;
echo '<table>';

foreach($r as $bild) {
    $events = '';
    $bildpfad = $bild['Bilderpfad'];
    switch( $counterForBilderInReihe ) {
    case 0:
        echo '<tr>';
        $events = <<<EOF
onclick="anzeigen({$bildpfad})" 
onMouseOver="cursorChange({$counterForID})"
EOF;

        $counterForBilderInReihe++;
        break;
    case 1:
    case 2:
    case 3:
        $events = <<<EOF
onclick="anzeigen({$bildpfad})" onMouseOver="cursorChange({$counterForID})"
EOF;
        $counterForBilderInReihe++;
        break;
    default:
        echo '</tr>';
        $counterForBilderInReihe = 1;
        echo '<tr>';
        $events =<<<EOF
onMouseOver="cursorChange({$counterForID})" onclick="anzeigen({$bildpfad})"
EOF;
        break;
    }
    echo <<<EOF
   <td class="td">
     <img src="{$bildpfad}" alt="{$bild['Bildtitel']}" 
        class="GalerieBilder" id="{$counterForID}"
        {$events}>
      </img>
    </td>
EOF;
    $counterForID++;

}
if($counterForBilderInReihe === 0){
    echo '</tr>';
} else {

}
 echo '</table>';

3 Comments

Yes you are right. And thank you for making me to notice that I have a duplicate onclick in
Finished editing - it is not complete but you should make an effort to cut down the amount of code you write. Part of the problem was that the code was more complicated than it needed to be.
Thank you. I will work on it to make simple code. The Problem is that I think in to complicated way. I dont know why :D But thank you :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.