Results 1 to 10 of 10

Thread: php checkbox

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    261

    php checkbox

    i have a website and i would like to add a checkbox for each record. so if i select the checkbox the page updates the database .

    how can i do this please, not sure were to start


    regards craig
    programming pc: laptop. running xp pro and vb.net..
    media centre xp pc: dual core 6000,4gb memory, 1tb harddrive
    mainserver: server 2008, 8gb memory, amd e-350 dual core, 6tb harddrive and local web host
    atomvault 1: server 2008, atom 330 with 4 gb memory, 35TB hardrive space for videos and music..
    backup pc : xp, 4gb, atom 330, 10tb harddrive(web, pictures, music, databases)
    2 x video backup: atom 330, 2gb, 18tb harddrive
    everything on remote login..
    check out my builds http://AtomVaults.yolasite.com

    learning vb.net and php + mysql - got most of the basics now.

    I WISH THERE WAS MORE TIME IN THE DAY

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: php checkbox

    you'll have to be a bit more descriptive about what you already have done and what you're actually trying to do. post some of your code.

    also -- step 1: make a checkbox.
    HTML Code:
    <input type="checkbox" name="myCheckbox" value="myCheckboxValue" />

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    261

    Re: php checkbox

    hi kows
    i used your posted and now i have a checkbox on my page There is now text next to it and its not getting the value from my DB.
    i also need the checkbox if selected to change the value in the db please

    thanks for your help in advance
    Code:
    </tr>
    <tr>
      <td width="20%" rowspan="1" height="120" BORDERCOLOR=white><input type="checkbox" name="WantToWatch" value=<?php echo $row['WantToWatch']; ?></td>
       <td width="80%" height="38" colspan="4" BORDERCOLOR=white><?php echo $row['filmplot1']; ?></td>
      </tr>
    <tr>
    programming pc: laptop. running xp pro and vb.net..
    media centre xp pc: dual core 6000,4gb memory, 1tb harddrive
    mainserver: server 2008, 8gb memory, amd e-350 dual core, 6tb harddrive and local web host
    atomvault 1: server 2008, atom 330 with 4 gb memory, 35TB hardrive space for videos and music..
    backup pc : xp, 4gb, atom 330, 10tb harddrive(web, pictures, music, databases)
    2 x video backup: atom 330, 2gb, 18tb harddrive
    everything on remote login..
    check out my builds http://AtomVaults.yolasite.com

    learning vb.net and php + mysql - got most of the basics now.

    I WISH THERE WAS MORE TIME IN THE DAY

  4. #4
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: php checkbox

    well, your syntax is wrong -- you need to fix your <input> so that it looks like this:
    HTML Code:
    <input type="checkbox" name="WantToWatch" value="<?php echo $row['WantToWatch']; ?>" />
    where is the code you're using to update your database now?

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    261

    Re: php checkbox

    hi kows.
    i corrected the syntax but im still not getting the check box checked. in the db "wanttowatch has the value of "true" or "false"
    programming pc: laptop. running xp pro and vb.net..
    media centre xp pc: dual core 6000,4gb memory, 1tb harddrive
    mainserver: server 2008, 8gb memory, amd e-350 dual core, 6tb harddrive and local web host
    atomvault 1: server 2008, atom 330 with 4 gb memory, 35TB hardrive space for videos and music..
    backup pc : xp, 4gb, atom 330, 10tb harddrive(web, pictures, music, databases)
    2 x video backup: atom 330, 2gb, 18tb harddrive
    everything on remote login..
    check out my builds http://AtomVaults.yolasite.com

    learning vb.net and php + mysql - got most of the basics now.

    I WISH THERE WAS MORE TIME IN THE DAY

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    261

    Re: php checkbox

    hi kows i have had a play around with checkboxs and come up with this. but im stuck with it i cant get the true value from the wanttowatch colom of the db to change the checkbox to checked

    i have posted the full table to try and show my working

    Code:
         $sql = "SELECT vidnumber, titleid, hd, year,starring1,videotitle,starring2, directedby, filmplot1,dirid,starrid1,starrid2, trilogy,age, Runningtime,WantToWatch FROM videolist WHERE WantToWatch LIKE '".mysql_real_escape_string("True")."' LIMIT $offset, $rowsperpage";
         $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
    
         // while there are rows to be fetched...
         while ($row = mysql_fetch_assoc($result)) {
    
       //  <td><?php echo $row['vidnubmer']; ?></td>
    
     <td width="20&#37;" rowspan="3" height="120" BORDERCOLOR=white><p align="center"><img border=0 src="/vidpics/<?php echo $row['videotitle'];?>web.jpg" <?php</p></td>
    
        <td width="20%" height="19" BORDERCOLOR=white><p align="center">ID:<?php echo $row['vidnumber']; ?></td>
        <td width="60%" colspan="3" height="19" BORDERCOLOR=white><p align="center"><b><?php echo $row['videotitle']; ?></b></td>
    </tr>
    <tr>
      <td width="20%" height="14" BORDERCOLOR=white><p align="center"><a href="http://imdb.com/title/<?php echo $row['titleid']; ?>" target="_top"><?php echo $row['titleid']; ?></a></td>
        <td width="20%" height="14" BORDERCOLOR=white><p align="center">Age <?php echo $row['age']; ?></td>
        <td width="20%" height="14" BORDERCOLOR=white><p align="center"><?php echo $row['year']; ?></td>
        <td width="20%" height="14" BORDERCOLOR=white><p align="center"><?php echo $row['Runningtime']; ?>mins</td>
    </tr>
    <tr>
      <td width="20%" height="25" BORDERCOLOR=white><p align="center"><a href="http://imdb.com/name/<?php echo $row['dirid']; ?>" target="_top"><?php echo $row['directedby']; ?></td>
        <td width="20%" height="25" BORDERCOLOR=white><p align="center"><a href="http://imdb.com/name/<?php echo $row['starrid1']; ?>" target="_top"><?php echo $row['starring1']; ?></td>
        <td width="20%" height="25" BORDERCOLOR=white><p align="center"><a href="http://imdb.com/name/<?php echo $row['starrid2']; ?>" target="_top"><?php echo $row['starring2']; ?></td>
        <td width="20%" height="25" BORDERCOLOR=white><p align="center"><?php echo $row['hd']; ?></td>
    </tr>
    <tr>
          if <?php echo $row['WantToWatch']; ?>="true" {
          <td width="20%" rowspan="1" height="120" BORDERCOLOR=white><input type="checkbox" name="WantToWatch" value="<?php echo $row['WantToWatch']; ?>" checked/>Watch</td>
                } else {
    
             <td width="20%" rowspan="1" height="120" BORDERCOLOR=white><input type="checkbox" name="WantToWatch" value="<?php echo $row['WantToWatch']; ?>" />Watch</td>
    
       }
      <td width="80%" height="38" colspan="4" BORDERCOLOR=white><?php echo $row['filmplot1']; ?></td>
      </tr>
    <tr>
      <td width="80%" height="38" colspan="5" BORDERCOLOR=white><p align="center">~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~</p></td>
    
     </tr>
          <?php
    
      }
    ?>
    
    </table>
    Last edited by c_owl; Jul 23rd, 2010 at 12:01 PM.
    programming pc: laptop. running xp pro and vb.net..
    media centre xp pc: dual core 6000,4gb memory, 1tb harddrive
    mainserver: server 2008, 8gb memory, amd e-350 dual core, 6tb harddrive and local web host
    atomvault 1: server 2008, atom 330 with 4 gb memory, 35TB hardrive space for videos and music..
    backup pc : xp, 4gb, atom 330, 10tb harddrive(web, pictures, music, databases)
    2 x video backup: atom 330, 2gb, 18tb harddrive
    everything on remote login..
    check out my builds http://AtomVaults.yolasite.com

    learning vb.net and php + mysql - got most of the basics now.

    I WISH THERE WAS MORE TIME IN THE DAY

  7. #7
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: php checkbox

    you have a random IF statement there which won't do anything; you haven't actually opened a PHP tag, so you're literally just printing an IF statement. change it to something like this:
    PHP Code:
    <?php if($row['WantToWatch'] == "true"){ ?>
      <!-- something if the value is true -->
    <?php }else{ ?>
      <!-- something is the value is not true -->
    <?php ?>
    you don't have to put the entire <input> again, however. you could shorten this immensely (this would replace your entire if statement, and produce the same result):
    PHP Code:
    <input type="checkbox" value="<?php echo $row['WantToWatch']; ?>"<?php if($row['WantToWatch'] == 'true') echo ' checked="checked"'?> />

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    261

    Re: php checkbox

    hi kows.
    thanks for your help i used the longer version so i can better understand what im doing while im learnng

    Code:
    <?php if($row['WantToWatch'] == "true"){ ?>
      <!-- something if the value is true -->
    <?php }else{ ?>
      <!-- something is the value is not true -->
    <?php } ?>
    i now want tobe able to update my db if a checkbox is selected.. ????
    but i dont know were to start
    programming pc: laptop. running xp pro and vb.net..
    media centre xp pc: dual core 6000,4gb memory, 1tb harddrive
    mainserver: server 2008, 8gb memory, amd e-350 dual core, 6tb harddrive and local web host
    atomvault 1: server 2008, atom 330 with 4 gb memory, 35TB hardrive space for videos and music..
    backup pc : xp, 4gb, atom 330, 10tb harddrive(web, pictures, music, databases)
    2 x video backup: atom 330, 2gb, 18tb harddrive
    everything on remote login..
    check out my builds http://AtomVaults.yolasite.com

    learning vb.net and php + mysql - got most of the basics now.

    I WISH THERE WAS MORE TIME IN THE DAY

  9. #9
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: php checkbox

    you need to have a form, and then you need a button that will submit the form.
    HTML Code:
    <form method="post">
      <!-- your entire table should go here -->
    
      <input type="submit" value="Submit" />
    </form>
    then, you'll need to modify your checkbox, because you need to keep track of whatever ID you're on (all of your checkboxes can not be named "WantToWatch"). I would suggest an array:
    HTML Code:
    <input type="checkbox" name="WantToWatch[<?php echo $row['vidnumber']; ?>]" value="<?php echo $row['WantToWatch']; ?>" />
    then, at the very top of your script you should put the following:
    PHP Code:
    <?php
      
    if($_SERVER['REQUEST_METHOD'] == "POST"){

        echo 
    '<pre>';
        
    print_r($_POST);
        echo 
    '</pre>';

      }
    ?>
    then, whenever you submit your form, you should see a big array holding all of the values that you'll need to update. keep in mind: checkboxes are NOT SET when they are submitted on a form and unchecked! this means that when you submit this form, you will have a list of all the checkboxes that were checked, but not a list of which ones were not checked. this shouldn't be a problem for updating your database table, though.

    let me know when you get all of that working properly.

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    261

    Re: php checkbox

    hi kows
    thanks. for the advice and help. im off to work now.

    im away for 3 days so will get straight on it when im back and let you know

    thanks again for your help

    craig
    programming pc: laptop. running xp pro and vb.net..
    media centre xp pc: dual core 6000,4gb memory, 1tb harddrive
    mainserver: server 2008, 8gb memory, amd e-350 dual core, 6tb harddrive and local web host
    atomvault 1: server 2008, atom 330 with 4 gb memory, 35TB hardrive space for videos and music..
    backup pc : xp, 4gb, atom 330, 10tb harddrive(web, pictures, music, databases)
    2 x video backup: atom 330, 2gb, 18tb harddrive
    everything on remote login..
    check out my builds http://AtomVaults.yolasite.com

    learning vb.net and php + mysql - got most of the basics now.

    I WISH THERE WAS MORE TIME IN THE DAY

Posting Permissions

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



Click Here to Expand Forum to Full Width