1

I'm creating some checkboxes via php like this:

$query = mysql_query("SELECT user FROM login");
while ($row = mysql_fetch_assoc($query)) {
    $readUser = $row['user'];
    if($readUser == "mod"){}
    else {
        $checkboxUserId = $readUser;
        echo "<p><input class='filled-in item' type='checkbox' id='$checkboxUserId' checked='checked' /><label for='$checkboxUserId'>Team: $checkboxUserId</label></p>";
    }

some code after this, I do:

I'm drawing some polygones via a Javascript function based on some values I stored in a database.

$query = mysql_query("SELECT * FROM questionAnswers");
while ($row = mysql_fetch_assoc($query)) {
    $readUser =  $row['user'];
    $someMoreVars = $row['var']; //like ten more or that
    if ($user == "mod"){
        if ($readUser == "mod"){}
        else{
             echo "drawUserPoly($someMoreVars, $iDontWantToListThemAll, $thatsJustForTheContext)";
             //Some More Code here
        }

Now the problem: I need to check for the checkboxes which one is checked so i don't draw them and this needs to be updated live (like: checking the checkbox again and the polygon will be drawn, uncheck the checkbox and the polygon is not drawn).

my attempt:

else {
      if(isset($_POST['$readUser'])){
      echo "drawUserPoly($someMoreVars, $iDontWantToListThemAll, $thatsJustForTheContext)";
      }
}

my second attempt:

else {
      if($_POST['$readuser'] == 'checked'){
      echo "drawUserPoly($someMoreVars, $iDontWantToListThemAll, $thatsJustForTheContext)";
      }
}
  • You are mixing javascript and php but the php has already finished processing the time that javascript starts. Are you using ajax? – jeroen Aug 22 '16 at 11:24
  • Possible duplicate of [What is the difference between client-side and server-side programming?](http://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – Epodax Aug 22 '16 at 11:28
  • @jeroen well after 7 weeks working on that website, my brain is a bit of a mush. But yes I think my coworker implemented ajax some time earlier. – Dosendusche Aug 22 '16 at 11:42

1 Answers1

1

Remember that all PHP code is executed before the page is sent to the browser, and that PHP cannot see whatever happens on the page after that. As a result, PHP and the HTML do not interact live.

Your solution is to use Javascript which does see what's happening in the HTML, and CSS styles. A simple approach would be to register an event listener on the checkbox checked event in JavaScript. When the box is unchecked, just hide the polygon by applying a CSS class that has display:none style. When checkbox is checked, remove that class and the polygon will reappear.

BeetleJuice
  • 39,516
  • 19
  • 105
  • 165
  • thanks for the kind reminder... forgot about that a little. I will give that thingy with the CSS class a try, thank you. – Dosendusche Aug 22 '16 at 11:58
  • @MisterM you never came back with your results. If you used this suggestion please select and upvote. If it didn't work for you, let me know why so I can learn from something from this too. – BeetleJuice Sep 06 '16 at 16:16
  • sry for not respodning @BeetleJuice that trick with the css class worked. i just create them with an dynamically given ID and later in the code where i got the checkboxes I give them a class with either visibility: visible or hidden. Thank you for your help, i would like to upvote but stackoverflow tells me: no you can't you need more points to upvote. – Dosendusche Sep 08 '16 at 12:45
  • @MisterM I'm glad you figured it out – BeetleJuice Sep 08 '16 at 14:14