SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Thread: Form handling

  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2001
    Location
    Coventry, England
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Form handling

    Hi,

    The problem is this,

    I have a form with a series of any number of checkboxes that are all in one array. The form is created from a database so I have no way of knowing how many items there are to be in the array!

    What I need to be able to do is scan through the array and see which ones they have selected.

    Also is there any way to be able to access and list the complete contents of what is being sent back from a form?

    Thanks
    Ian

    Code:
    <? $sessionsresult = @mysql_query("SELECT * FROM Sessions WHERE $page = CourseID");
    if (!$sessionsresult) {
       echo("<p>Error performing query: " . mysql_error() . "</p>");
       exit();
    }
    
    while ( $row = mysql_fetch_array($sessionsresult) ) {
    ?>
        <tr>
        <td align="right">
    	<input type="checkbox" name="Session[]" value="<? echo( $row["ID"] ); ?>">
        </td>
        <td align="left">
        	<strong><? echo( $row["Title"] ); ?></strong><br>
    		Date: <? echo formatdate( $row["SessionStartDate"] ); ?> - <? echo formatdate( $row["SessionEndDate"] ); ?>
        </td>
        </tr>
    <? } ?>

  2. #2
    SitePoint Enthusiast
    Join Date
    Nov 2001
    Location
    Coventry, England
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Sussed it!

    I have worked out a solution, here it is for anyone thats interested!

    Basically it reloads in the data from the table that the form was created from. Then it runs through the rows that create the checkbox tags and using the row ID it puts that as the array number and checks to see if that is set if so then that form item has been selected!

    Plus you have to put the name of the checkbox tag with a number in square brackets which is the Row ID as shown here
    Code:
    <input type="checkbox" name="Session[<? echo( $row["ID"] ); ?>]" value="ON">
    Code is as follows.

    Code:
    <? $sessionsresult = @mysql_query("SELECT * FROM Sessions WHERE $page = CourseID");
    if (!$sessionsresult) {
       echo("<p>Error performing query: " . mysql_error() . "</p>");
       exit();
    }
    
    while ( $row = mysql_fetch_array($sessionsresult) ) {
    	$n = $n + 1;
    	if ($Session[ $n ]){
    		?>
    		<tr>
    		<td colspan="2">
    		<strong><? echo( $row["Title"] ); ?></strong><br>
    		Date: <? echo formatdate( $row["SessionStartDate"] ); ?> - <? echo formatdate( $row["SessionEndDate"] ); ?></td></tr>
    	<? }
    	
    } ?>

  3. #3
    PHP warrior dkode's Avatar
    Join Date
    Sep 2001
    Location
    Planet Namek
    Posts
    329
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    instead of doing this:

    <input type="checkbox" name="Session[]" value="<? echo( $row["ID"] ); ?>">

    i would use an associative array instead:

    <input type="checkbox" name="Session[<?=$row["ID"]?>]" value="1">

    and then you could use a loop like this to see which ones have been set:

    foreach ($Session as $id => $val) {
    if ($val == 1) {
    do whatever here;
    }
    }
    "Mankind cannot define memory, yet it defines mankind"
    -- Project 2501, Ghost in the Shell

    Smarty | PEAR | PHP Manual | MySQL Manual


Bookmarks

Posting Permissions

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