using a check box

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
phpPain
Forum Newbie
Posts: 10
Joined: Mon Nov 24, 2008 6:14 am

using a check box

Post by phpPain »

Hi,

I am still a newbie here :oops: . I have the following, which works at the moment and displays the data ok.

However I need the file to print out a check box after each line of the database. This way when the form is submitted which ever check box is ticked I can display on the next page ready for editing. I think I would need to send a variable with the item selected, but I am not sure, I think the only line I need to change will be the one in bold. Any help gratefully appreciated.

Hope this makes sense. Thank you.

Code: Select all

 
 
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
    <html>
    <head>
    <title>STW</title>
    <META NAME="Generator" CONTENT="">
    <META NAME="Author" CONTENT="">
    <META NAME="Keywords" CONTENT="?">
    <META NAME="Description" CONTENT="?">
 
    <link rel="stylesheet" href="style.css" type="text/css">
 
    </head>
 
    <body bgcolor="white">
    <h1>Your programs</h1>
 
    <?php
      include("connection.php");
    ?>
 
    <?php
    //connect to db 
    if(!($dblink=mysql_connect($server,$user,$password)))  {
      print "Fail: mysql_connect failed" ;
      exit;
    }
 
    //error if can't select db
    if(!mysql_select_db($db,$dblink))  {
      print "Fail: can't select database" ;
      exit;
    }
 
    // quuery db to get data
    $sql = "select * from programmes";
    if(!($result = mysql_query($sql,$dblink)))  {
      print "Fail: invalid query";
      exit;
    }
 
    //get values post data to update.php
    printf("<form action=\"%s\" method=\"%s\">\n", 'addComment.php', 'post');
    while ($row = mysql_fetch_row($result))
    {
        [b]printf("id: %s\n Channel: %s\n  Program: %s\n Time: %s\n <br/> <br/>", $row[0], $row[1], $row[2], $row[3]);[/b]  }
    print "<input type=\"submit\" name=\"mysubmit\" value=\"Add Comment\" />";
    print "</form>";  
 
    ?>
 
    <p><a href="unit6.html">BACK</a>
    </body>
    </html>
 
maneetpuri
Forum Commoner
Posts: 60
Joined: Tue Oct 07, 2008 6:32 am

Re: using a check box

Post by maneetpuri »

Hi,

Yes that’s correct, you will have to make changes in the bold line plus you will have to add a if() there to check if value passed from the previous form matches with the one you are displaying then show it checked (ready to be edited) otherwise do not show it checked. Also I think if you are making check boxes then in the while loop you should add line to generate the checkboxes.

Hope this makes sense to you!

Cheers,
phpPain
Forum Newbie
Posts: 10
Joined: Mon Nov 24, 2008 6:14 am

Re: using a check box

Post by phpPain »

I have got this so far, its sticks the check box on the end, I am a little stuck as to what I do next.

Code: Select all

 
 
    //get values post data to update.php
    printf("<form action=\"%s\" method=\"%s\">\n", 'addComment.php', 'post');
    while ($row = mysql_fetch_row($result))
    {
        printf("id: %s\n Channel: %s\n  Program: %s\n Time: %s\n ", $row[0], $row[1], $row[2], $row[3]);
        printf("<input type=\"checkbox\" name=box[] > <br/> <br/> ");
    }
    print "<input type=\"submit\" name=\"mysubmit\" value=\"View Details\" />";
    print "</form>";  
 
    ?>
 
 
What will the IF statement look like, how or where do I include the variable. thank you.
Post Reply