Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7

Thread: Checkbox Toggle

  1. #1
    New to the CF scene
    Join Date
    Apr 2013
    Posts
    8
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Exclamation Checkbox Toggle

    Hello guys I was wondering if the checkbox would get check when the date on $row['Due'] is reached for example 14/04/2013 is the date on Srow['Due']. The checkbox should automatically get the date and check the checkbox permanently.

    PHP Code:
    <?php 
     $con
    =mysqli_connect("localhost","root","","User_db"); 
     
    // Check connection 
     
    if (mysqli_connect_errno()) 
       { 
       echo 
    "Failed to connect to MySQL: " mysqli_connect_error(); 
       } 

     
    $result mysqli_query($con,"SELECT * FROM Assessments"); 

     echo 
    "<table class='sortable' border='0' cellpadding='40' 
    cellspacing='0'> 
    <thead> 
     <tr> 
     <th>Title</th> 
     <th>Lecturer</th> 
     <th>Start</th> 
     <th>Due</th> 
     <th>Completed</th> 
     </tr> 
     </thead>"


     while(
    $row mysqli_fetch_array($result)) 
       { 
       echo 
    "<tr>"
       echo 
    "<td>" $row['Title'] . "</td>"
       echo 
    "<td>" $row['Lecturer'] . "</td>"
       echo 
    "<td>" $row['Start'] . "</td>"
       echo 
    "<td id='Due'>" $row['Due'] . "</td>"
       echo 
    "<td>" ."<input type='checkbox' id='mycheckbox' name='Completed' value='Completed' />"
       echo 
    "</tr>"
       } 
     echo 
    "</table>"

     
    mysqli_close($con); 
     
    ?>

  • #2
    Regular Coder
    Join Date
    Sep 2011
    Posts
    213
    Thanks
    15
    Thanked 5 Times in 5 Posts
    Try something like this:
    PHP Code:
    echo "<td>" ."<input type='checkbox' id='mycheckbox' name='Completed' value='Completed' ".(date("d/m/Y") == $row['Due'] ? "checked='checked' " ""."/>"
    As for the disabling of the checkbox, you could use the disable tag to simply display the box is checked, but you would need to add a hidden input of the same name so the value will go through (disabled tags don't send on submit).

  • Users who have thanked Dubz for this post:

    D4rk_H34rt (04-14-2013)

  • #3
    New to the CF scene
    Join Date
    Apr 2013
    Posts
    8
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Dubz View Post
    Try something like this:
    PHP Code:
    echo "<td>" ."<input type='checkbox' id='mycheckbox' name='Completed' value='Completed' ".(date("d/m/Y") == $row['Due'] ? "checked='checked' " ""."/>"
    As for the disabling of the checkbox, you could use the disable tag to simply display the box is checked, but you would need to add a hidden input of the same name so the value will go through (disabled tags don't send on submit).
    Ok I think the code would work but it says Parse error: syntax error, unexpected '>' when I copy and paste ur code on the line where the checkbox is

    PHP Code:
     while($row mysqli_fetch_array($result))
       {
       echo 
    "<tr>";
       echo 
    "<td>" $row['Title'] . "</td>";
       echo 
    "<td>" $row['Lecturer'] . "</td>";
       echo 
    "<td>" $row['Start'] . "</td>";
       echo 
    "<td>" $row['Due'] . "</td>";
       echo 
    "<td>"  $row['Completed'] . "
       echo "
    <td>" ."<input type='checkbox' id='mycheckbox' name='Completed' value='Completed' ".(date("d/m/Y") == $row['Due'] ? "checked='checked' " : ""."/>";  

     
       echo "
    </tr>";
       }
     echo "
    </table>"; 

  • #4
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    709
    Thanks
    20
    Thanked 84 Times in 84 Posts
    you have a bit missing at the end of this row

    PHP Code:
       echo "<td>"  $row['Completed'] . 
    PHP Code:
       echo "<td>"  $row['Completed'] . " </td>"
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month

  • Users who have thanked Arcticwarrio for this post:

    D4rk_H34rt (04-15-2013)

  • #5
    New to the CF scene
    Join Date
    Apr 2013
    Posts
    8
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Arcticwarrio View Post
    you have a bit missing at the end of this row

    PHP Code:
       echo "<td>"  $row['Completed'] . 
    PHP Code:
       echo "<td>"  $row['Completed'] . " </td>"
    It still says Parse error: syntax error, unexpected ';' on line 30

    PHP Code:
    <?php
     $con
    =mysqli_connect("localhost","root","","User_db");
     
    // Check connection
     
    if (mysqli_connect_errno())
       {
       echo 
    "Failed to connect to MySQL: " mysqli_connect_error();
       }

     
    $result mysqli_query($con,"SELECT * FROM Assessments");

     echo 
    "<table class='sortable' border='0' cellpadding='40'
    cellspacing='0'>
    <thead>
     <tr>
     <th>Title</th>
     <th>Lecturer</th>
     <th>Start</th>
     <th>Due</th>
     <th>Completed</th>
     </tr>
     </thead>"
    ;

     while(
    $row mysqli_fetch_array($result))
       {
       echo 
    "<tr>";
       echo 
    "<td>" $row['Title'] . "</td>";
       echo 
    "<td>" $row['Lecturer'] . "</td>";
       echo 
    "<td>" $row['Start'] . "</td>";
       echo 
    "<td>" $row['Due'] . "</td>";
       echo 
    "<td>" "<input type='checkbox' id='mycheckbox' name='Completed' value='Completed' ".(date("d/m/Y") == $row['Due'] ? "checked='checked' " "".">  
       echo "
    </tr>";
       }
     echo "
    </table>";

     mysqli_close($con);
     ?>
    Even when I change it to, same error

    PHP Code:
    echo "<td>" "<input type='checkbox' id='mycheckbox' name='Completed' value='Completed' ".(date("d/m/Y") == $row['Due'] ? "checked='checked' " ""."> ."</td>"; 

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    You haven't assembled the new string properly: echo "<td><input type='checkbox' id='mycheckbox' name='Completed' value='Completed'" . (date("d/m/Y") == $row['Due'] ? " checked='checked' " : "") . "></td>";. That should work. Note that you were missing the ending quote and semi-colon as well as a closing bracket on the ternary. Also note that since PHP ternaries are ltr in precedence, that you *must* always block them in brackets when you are assembling them as a part of a string.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    D4rk_H34rt (04-15-2013)

  • #7
    New to the CF scene
    Join Date
    Apr 2013
    Posts
    8
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Smile

    Quote Originally Posted by Fou-Lu View Post
    You haven't assembled the new string properly: echo "<td><input type='checkbox' id='mycheckbox' name='Completed' value='Completed'" . (date("d/m/Y") == $row['Due'] ? " checked='checked' " : "") . "></td>";. That should work. Note that you were missing the ending quote and semi-colon as well as a closing bracket on the ternary. Also note that since PHP ternaries are ltr in precedence, that you *must* always block them in brackets when you are assembling them as a part of a string.
    Thanks a lot..it worked


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •