0

I've got a loop posting images from database. I tried to give each photo div separate id. When i tried to send it to script by onclick function It shows that "$photoid is not defined " I tried to print this variable in this first php script but it shows me all IDs, so shouldnt be empty or undefined...

$allphotos = mysql_query("SELECT * FROM photos ORDER BY id DESC");

while ($numphotos = mysql_fetch_assoc($allphotos)){

$photoinfo = mysql_query('SELECT * FROM photos WHERE link="'.$numphotos['link'].'" ');
$fetchinfo = mysql_fetch_assoc($photoinfo);

$photoid = $fetchinfo['id']; 
echo '<div class="thisphotobox" id="'.$photoid.'"><img src="'.$numphotos['link'].'" alt="photo" class="photolink" onclick=clicked($photoid)></div>';
}

Here goes script:

  <script>
    function clicked(photoid){
    document.getElementById('photoid').style.backgroundColor = 'red'
    }
    </script>
  • Did you change the function to what @vlzvl wrote? Or is your background not red even with the change? – The One and Only ChemistryBlob Sep 10 '14 at 02:01
  • I did everything like he said and it shows no errors now but background still do not change at all after all updates –  Sep 10 '14 at 02:02
  • Do you know jQuery? If so you can try replacing the document.get etc. line with $("#" + photoid).css({"backgroundColor" : "#FF0000"}); Another possibility is the correct photoid is not being passed...try adding alert(photoid); to your function to make sure the correct id is being passed – The One and Only ChemistryBlob Sep 10 '14 at 02:07
  • Oh no, i am fresh programmer with html css php knowledge which is also pretty elemental :D But thank you :) Hmm alert dont even show, thats strange, i hate js so much –  Sep 10 '14 at 02:12
  • Wait...alert box doesn't even appear or it does appear but it is empty? Which one? – The One and Only ChemistryBlob Sep 10 '14 at 02:15
  • It doesnt appear at all. Maybe im just stupid and this is the case XD –  Sep 10 '14 at 02:19
  • if the alert box does not appear there is an error in the code...in this case right click (in Chrome) and choose "Inspect Element" and see if on the bottom of the popup window under "Console" tab if there is red print that says the nature of the error – The One and Only ChemistryBlob Sep 10 '14 at 02:19
  • It didnt show any error i was checking it. But after adding background-color inside of div line all started to work! even alert poped up. I have no idea how that works but it does :D –  Sep 10 '14 at 02:26
  • These are vanilla javascript thingies etc. plain javascript. One needs to verify first if the `.style` exist before modifying it. I had some code for this years, years before switching to jquery. –  Sep 10 '14 at 02:30
  • I would love to start with jquery but I really believe that i need some js basics first. Uhm all that programming seems to be so hard after this trashy php language XD –  Sep 10 '14 at 02:34

3 Answers3

1

Change this:

echo '<div class="thisphotobox" id="'.$photoid.'"><img src="'.$numphotos['link'].'" alt="photo" class="photolink" onclick=clicked($photoid)></div>';

to this:

echo '<div class="thisphotobox" id="'.$photoid.'"><img src="'.$numphotos['link'].'" alt="photo" class="photolink" onclick="clicked('.$photoid.');"></div>';

otherwise, the output is just clicked($photoid), all text.

Also change that:

function clicked(photoid){
document.getElementById('photoid').style.backgroundColor = 'red'
}

to that:

function clicked(photoid){
document.getElementById( photoid.toString() ).style.backgroundColor = 'red';
}
  • I propose a name change to this site....DVO (Down Vote Overflow)...by the way I did not downvote, I'm simply baffled – The One and Only ChemistryBlob Sep 10 '14 at 01:54
  • Thank you, it actually seems to finish all my errors but the background color still not changing. Any idea? A lot of trolls here who are downvoting :( They should get banned. Cant you guys vote for each other to make it up :D? –  Sep 10 '14 at 01:58
  • I upvoted your answer to undo the downvote, plus your answer is useful – The One and Only ChemistryBlob Sep 10 '14 at 01:59
  • I actually upvoted yours as well, just because i don't like silent downvoters –  Sep 10 '14 at 02:00
  • @Terrorizer, do you mean the `div` is not getting _red_? –  Sep 10 '14 at 02:02
  • Yes thats exactly what i mean –  Sep 10 '14 at 02:04
  • @Terrorizer, i must be dizzy.. just pass the photoid converted as string –  Sep 10 '14 at 02:07
  • @vlzvl Thank you so much for your patience! Thats strange how this stupid background still do not want to change XD Java script is a language out of this universe XD –  Sep 10 '14 at 02:13
  • @Terrorizer, i don't know really, code is simple. You said alert isn't even shown? do you have any errors in console? –  Sep 10 '14 at 02:13
  • Additionally, can you add `background-color:red` to `thisphotobox` class (in css, not inline) to verify it is shown red as you want it, regardless the click? remove it after, just for verification. –  Sep 10 '14 at 02:16
  • Have no idea why but in css stylesheet it didnt work but in line it did :P –  Sep 10 '14 at 02:24
0

The answer above is working, but they forgot to add something,

instead of

echo '<div class="thisphotobox" id="'.$photoid.'"><img src="'.$numphotos['link'].'" alt="photo" class="photolink" onclick="clicked('.$photoid.');"></div>';

copy this code

echo '<div class="thisphotobox" id="'.$photoid.'"><img src="'.$numphotos['link'].'" alt="photo" class="photolink" style="background-color:white" onclick="clicked('.$photoid.');"></div>';

add the style="background-color:white;" i think you can't access the .style in js if you didn't initialize it in your div. hope this will help

CodeSlayer
  • 1,318
  • 1
  • 12
  • 34
  • Are you sure background-color:none; is valid? See this link: http://stackoverflow.com/questions/8739665/is-background-colornone-valid-css – The One and Only ChemistryBlob Sep 10 '14 at 02:22
  • Yes that's right!! I actually set the color in CSS sheet but it had to be done here as we can see hmmm Thank you, works perfectly now. I love u all guys –  Sep 10 '14 at 02:22
  • i edited my answer minute ago but my net was so slow.. i put white cause it is the default color. Sorry for the none, i'm visual C++ and C# develeper before after a month going back to php, and i think i got confused – CodeSlayer Sep 10 '14 at 02:25
-1

This is simply an escaping issue. The argument $photoid in your echoed function has to be escaped if it is a string. In your code above this:

onclick=clicked($photoid)

should be

onclick="clicked($photoid)"

if $photoid is a number or

onclick="clicked(\'$photoid\')"

if it is alphanumeric or a string