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)";
      }
}
3
  • You are mixing javascript and php but the php has already finished processing the time that javascript starts. Are you using ajax? Commented Aug 22, 2016 at 11:24
  • Possible duplicate of What is the difference between client-side and server-side programming? Commented Aug 22, 2016 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. Commented Aug 22, 2016 at 11:42

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

thanks for the kind reminder... forgot about that a little. I will give that thingy with the CSS class a try, thank you.
@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.
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.
@MisterM I'm glad you figured it out

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.