Submit button and 'id' values problem for a form

Hi,
I have a form of checkboxes, which has been created using two foreach() loops, with one nested inside the other.
Basically there are courses and learners.
I would like one submit button for the entire form.
But if I put it inside the loops it will be created multiple times, and if I put it outside the loops the ‘id’ of each learner will not be passed to $_Post[‘id’].
How can I get around this problem?
form.html.php

<?php include_once $_SERVER['DOCUMENT_ROOT'] . '/artgibney/includes/helpers.inc.php';  
      include_once $_SERVER['DOCUMENT_ROOT'] . '/artgibney/includes/func.inc.php'; 
      include $_SERVER['DOCUMENT_ROOT'] . '/artgibney/includes/buildcourses.inc.php';
      include $_SERVER['DOCUMENT_ROOT'] . '/artgibney/includes/buildlearners.inc.php';?>
<!DOCTYPE html>
<html lang="en">
  <head>
  <style>
table,th,td
{
border-collapse:collapse;
}
th, td
{
padding:6px;
}
        td { text-align: center; width: 40px; overflow: hidden;  white-space: nowrap;}
      
</style>
    <meta charset="utf-8">
    <title>Manage Daily Notes</title>
  </head>
  <body>
      <p><a href="..">physCMS home</a></p>
      <?php include $_SERVER['DOCUMENT_ROOT'] . '/artgibney/includes/logout.inc.html.php'; ?>
    <h1>Manage Daily Notes</h1>
    <p><a href="?addcourseform">Add Course</a>  &#8658;  
	<? if(countRows('courses')): ?>
 	<a href="?addlearnerform">Add Student</a>
	<? endif; ?>  
	<? if(countRows('learners')): ?>
 	  &#8658;  <a href="?courses">Manage Courses and Students</a></p>
	<? endif; ?>
	<table>
	     <form action="?addnotes" method="post">
			<tr>
			<th>Course</th>
			<th>Student</th>
			<th>Absence</th>
			<th>Late</th>
			<th>Disruptive</th>
			<th>Equipment</th>
			<th>Laziness</th>
			<th>Phone</th>
			<th>Rudeness</th>
			<th>Excellent Effort</th>
			</tr>
			<?php foreach (array_reverse($courses) as $course): ?>
			<tr>
				<td>
				<?php htmlout($course['course']);?>
				<!-- <?php htmlout($course['id']);?> -->
				</td>
					<?php foreach (array_reverse($learners) as $learner): ?>
						<? if($course['id']==$learner['courseid']): ?>
						<td>
						<?php htmlout($learner['learner']);?>	
						<!-- <?php htmlout($learner['courseid']);?>	-->							
						</td>
								<td>
									<input type="checkbox" name="absence" id="absence" value="-1">
								</td>
								<td>
									<input type="checkbox" name="late" id="late" value="1">
								</td>
								<td>
									<input type="checkbox" name="disrupt" id="disrupt" value="-1">
								</td>
								<td>
									<input type="checkbox" name="equip" id="equip" value="-1">
								</td>
								<td>
									<input type="checkbox" name="lazy" id="lazy" value="-1">
								</td>
								<td>
									<input type="checkbox" name="phone" id="phone" value="-1">
								</td>
								<td>
									<input type="checkbox" name="rude" id="rude" value="-1">
								</td>
								<td>
									<input type="checkbox" name="effort" id="effort" value="1">
								</td>
						</tr>
					<td></td>
					<? endif; ?> 
                                                <tr>
						<input type="hidden" name="id" value="<?php echo $learner['id']; ?>">
    						<input type="submit" value="Submit">
                                                </tr>
				</form>
			<?php endforeach; ?>	
		<?php endforeach; ?>
	</table>
  </body>
</html>

index.php

if (isset($_GET['addnotes']))
{
  include $_SERVER['DOCUMENT_ROOT'] . '/artgibney/includes/db.inc.php';

  try
  {
    $sql = 'INSERT INTO notes SET
        absence = :absence,
        late = :late,
        disrupt = :disrupt,
        equip = :equip,
        lazy = :lazy,
        phone = :phone,
        rude = :rude,
        effort = :effort,
        id = :id';
    $s = $pdo->prepare($sql);
    $s->bindValue(':absence', $_POST['absence']);
    $s->bindValue(':late', $_POST['late']);
    $s->bindValue(':disrupt', $_POST['disrupt']);
    $s->bindValue(':equip', $_POST['equip']);
    $s->bindValue(':lazy', $_POST['lazy']);
    $s->bindValue(':phone', $_POST['phone']);
    $s->bindValue(':rude', $_POST['rude']);
    $s->bindValue(':effort', $_POST['effort']);
    $s->bindValue(':id', $_POST['id']);
    $s->execute();
  }
  catch (PDOException $e)
  {
    $error = 'Error adding submitted daily notes data.';
    include 'error.html.php';
    exit();
  }

  header('Location: .');
  exit();
}

Thanks,
Shane

first problem is that you have invalid HTML markup (one opening <form> tag, multiple closing </form> tags, as well as duplicate IDs).

second (related to that) problem, your input elements would overwrite each other if multiple ones with the same name are selected (which would include the hidden inputs as well giving you only the last ID).

since you wanted to post everything at once, you need to make groups of each set of input elements by (e.g.) passing the learner ID into the names (which would make the hidden fields superfluous)

printf('<input type="checkbox" name="absence[%1$d]" id="absence_%1$d" value="-1">', $learner['id']);

Hi Dormilich,
Thanks for your reply. Regarding the HTML, I can only see one closing form tag </form>.

input elements would overwrite each other if multiple ones with the same name are selected

I do understand what you mean by this.
I am not familiar with printf() but I see that it outputs a formatted string.
I have deleted the line

<input type="hidden" name="id" value="<?php echo $learner['id']; ?>">

Then should I proceed like this?

<table>
	     <form action="?addnotes" method="post">
			<tr>
			<th>Course</th>
			<th>Student</th>
			<th>Absence</th>
			<th>Late</th>
			<th>Disruptive</th>
			<th>Equipment</th>
			<th>Laziness</th>
			<th>Phone</th>
			<th>Rudeness</th>
			<th>Excellent Effort</th>
			</tr>
			<?php foreach (array_reverse($courses) as $course): ?>
			<tr>
				<td>
				<?php htmlout($course['course']);?>
				<!-- <?php htmlout($course['id']);?> -->
				</td>
					<?php foreach (array_reverse($learners) as $learner): ?>
						<? if($course['id']==$learner['courseid']): ?>
						<td>
						<?php htmlout($learner['learner']);?>	
						<!-- <?php htmlout($learner['courseid']);?>	-->							
						</td>
								<td>
									<input type="checkbox" name="absence" id="absence" value="-1">
									printf('<input type="checkbox" name="absence[%1$d]" id="absence_%1$d" value="-1">', $learner['id']); 
								</td>
								<td>
									<input type="checkbox" name="late" id="late" value="1">
									printf('<input type="checkbox" name="late[%1$d]" id="late_%1$d" value="-1">', $learner['id']); 
								</td>
								<td>
									<input type="checkbox" name="disrupt" id="disrupt" value="-1">
									printf('<input type="checkbox" name="disrupt[%1$d]" id="disrupt_%1$d" value="-1">', $learner['id']);
								</td>
								<td>
									<input type="checkbox" name="equip" id="disrupt" value="-1">
									printf('<input type="checkbox" name="disrupt[%1$d]" id="disrupt_%1$d" value="-1">', $learner['id']);
								</td>
								<td>
									<input type="checkbox" name="lazy" id="lazy" value="-1">
									printf('<input type="checkbox" name="lazy[%1$d]" id="lazy_%1$d" value="-1">', $learner['id']);
								</td>
								<td>
									<input type="checkbox" name="phone" id="phone" value="-1">
									printf('<input type="checkbox" name="phone[%1$d]" id="phone_%1$d" value="-1">', $learner['id']);
								</td>
								<td>
									<input type="checkbox" name="rude" id="rude" value="-1">
									printf('<input type="checkbox" name="rude[%1$d]" id="rude_%1$d" value="-1">', $learner['id']);
								</td>
								<td>
									<input type="checkbox" name="effort" id="effort" value="1">
									printf('<input type="checkbox" name="effort[%1$d]" id="effort_%1$d" value="-1">', $learner['id']);
								</td>
						</tr>
					<td></td>
					<? endif; ?> 
                                                <tr>
						<!-- <input type="hidden" name="id" value="<?php echo $learner['id']; ?>"> -->
    						<input type="submit" value="Submit">
                                                </tr>
				</form>
			<?php endforeach; ?>	
		<?php endforeach; ?>
	</table>

Thanks for your help,
Shane

Hi,
I should have had the code like this instead,

<?php include_once $_SERVER['DOCUMENT_ROOT'] . '/artgibney/includes/helpers.inc.php';  
      include_once $_SERVER['DOCUMENT_ROOT'] . '/artgibney/includes/func.inc.php'; 
      include $_SERVER['DOCUMENT_ROOT'] . '/artgibney/includes/buildcourses.inc.php';
      include $_SERVER['DOCUMENT_ROOT'] . '/artgibney/includes/buildlearners.inc.php';?>
<!DOCTYPE html>
<html lang="en">
  <head>
  <style>
table,th,td
{
border-collapse:collapse;
}
th, td
{
padding:6px;
}
        td { text-align: center; width: 40px; overflow: hidden;  white-space: nowrap;}
      
</style>
    <meta charset="utf-8">
    <title>Manage Daily Notes</title>
  </head>
  <body>
      <p><a href="..">physCMS home</a></p>
      <?php include $_SERVER['DOCUMENT_ROOT'] . '/artgibney/includes/logout.inc.html.php'; ?>
    <h1>Manage Daily Notes</h1>
    <p><a href="?addcourseform">Add Course</a>  &#8658;  
	<? if(countRows('courses')): ?>
 	<a href="?addlearnerform">Add Student</a>
	<? endif; ?>  
	<? if(countRows('learners')): ?>
 	  &#8658;  <a href="?courses">Manage Courses and Students</a></p>
	<? endif; ?>
<table>
         <form action="?addnotes" method="post">
            <tr>
            <th>Course</th>
            <th>Student</th>
            <th>Absence</th>
            <th>Late</th>
            <th>Disruptive</th>
            <th>Equipment</th>
            <th>Laziness</th>
            <th>Phone</th>
            <th>Rudeness</th>
            <th>Excellent Effort</th>
            </tr>
            <?php foreach (array_reverse($courses) as $course): ?>
            <tr>
                <td>
                <?php htmlout($course['course']);?>
                <!-- <?php htmlout($course['id']);?> -->
                </td>
                    <?php foreach (array_reverse($learners) as $learner): ?>
                        <? if($course['id']==$learner['courseid']): ?>
                        <td>
                        <?php htmlout($learner['learner']);?>    
                        <!-- <?php htmlout($learner['courseid']);?>    -->                            
                        </td>
                                <td>
                                    <!-- <input type="checkbox" name="absence" id="absence" value="-1"> -->
                                    printf('<input type="checkbox" name="absence[%1$d]" id="absence_%1$d" value="-1">', $learner['id']); 
                                </td>
                                <td>
                                    <!-- <input type="checkbox" name="late" id="late" value="1"> -->
                                    printf('<input type="checkbox" name="late[%1$d]" id="late_%1$d" value="-1">', $learner['id']); 
                                </td>
                                <td>
                                    <!-- <input type="checkbox" name="disrupt" id="disrupt" value="-1"> -->
                                    printf('<input type="checkbox" name="disrupt[%1$d]" id="disrupt_%1$d" value="-1">', $learner['id']);
                                </td>
                                <td>
                                    <!-- <input type="checkbox" name="equip" id="disrupt" value="-1"> -->
                                    printf('<input type="checkbox" name="disrupt[%1$d]" id="disrupt_%1$d" value="-1">', $learner['id']);
                                </td>
                                <td>
                                    <!-- <input type="checkbox" name="lazy" id="lazy" value="-1"> -->
                                    printf('<input type="checkbox" name="lazy[%1$d]" id="lazy_%1$d" value="-1">', $learner['id']);
                                </td>
                                <td>
                                    <!-- <input type="checkbox" name="phone" id="phone" value="-1"> -->
                                    printf('<input type="checkbox" name="phone[%1$d]" id="phone_%1$d" value="-1">', $learner['id']);
                                </td>
                                <td>
                                    <!-- <input type="checkbox" name="rude" id="rude" value="-1"> -->
                                    printf('<input type="checkbox" name="rude[%1$d]" id="rude_%1$d" value="-1">', $learner['id']);
                                </td>
                                <td>
                                    <!-- <input type="checkbox" name="effort" id="effort" value="1"> -->
                                    printf('<input type="checkbox" name="effort[%1$d]" id="effort_%1$d" value="-1">', $learner['id']);
                                </td>
                        </tr>
                    <td></td>
                    <? endif; ?> 
                                                <tr>
                        <!-- <input type="hidden" name="id" value="<?php echo $learner['id']; ?>"> -->
                                                </tr>
                </form>
            <?php endforeach; ?>    
        <?php endforeach; ?>
    </table>
    						<input type="submit" value="Submit">
  </body>
</html>

Thanks,
Shane

Right, I guess the printf, has to be inside php tags, <?php ?>
So now the html is as follows,

<?php include_once $_SERVER['DOCUMENT_ROOT'] . '/artgibney/includes/helpers.inc.php';  
      include_once $_SERVER['DOCUMENT_ROOT'] . '/artgibney/includes/func.inc.php'; 
      include $_SERVER['DOCUMENT_ROOT'] . '/artgibney/includes/buildcourses.inc.php';
      include $_SERVER['DOCUMENT_ROOT'] . '/artgibney/includes/buildlearners.inc.php';?>
<!DOCTYPE html>
<html lang="en">
  <head>
  <style>
table,th,td
{
border-collapse:collapse;
}
th, td
{
padding:6px;
}
        td { text-align: center; width: 40px; overflow: hidden;  white-space: nowrap;}
      
</style>
    <meta charset="utf-8">
    <title>Manage Daily Notes</title>
  </head>
  <body>
      <p><a href="..">physCMS home</a></p>
      <?php include $_SERVER['DOCUMENT_ROOT'] . '/artgibney/includes/logout.inc.html.php'; ?>
    <h1>Manage Daily Notes</h1>
    <p><a href="?addcourseform">Add Course</a>  &#8658;  
	<? if(countRows('courses')): ?>
 	<a href="?addlearnerform">Add Student</a>
	<? endif; ?>  
	<? if(countRows('learners')): ?>
 	  &#8658;  <a href="?courses">Manage Courses and Students</a></p>
	<? endif; ?>
<table>
         <form action="?addnotes" method="post">
            <tr>
            <th>Course</th>
            <th>Student</th>
            <th>Absence</th>
            <th>Late</th>
            <th>Disruptive</th>
            <th>Equipment</th>
            <th>Laziness</th>
            <th>Phone</th>
            <th>Rudeness</th>
            <th>Excellent Effort</th>
            </tr>
            <?php foreach (array_reverse($courses) as $course): ?>
            <tr>
                <td>
                <?php htmlout($course['course']);?>
                <!-- <?php htmlout($course['id']);?> -->
                </td>
                    <?php foreach (array_reverse($learners) as $learner): ?>
                        <? if($course['id']==$learner['courseid']): ?>
                        <td>
                        <?php htmlout($learner['learner']);?>    
                        <!-- <?php htmlout($learner['courseid']);?>    -->                            
                        </td>
                                <td>
                                    <!-- <input type="checkbox" name="absence" id="absence" value="-1"> -->
                                    <?php printf('<input type="checkbox" name="absence[%1$d]" id="absence_%1$d" value="-1">', $learner['id']); ?>
                                </td>
                                <td>
                                    <!-- <input type="checkbox" name="late" id="late" value="1"> -->
                                    <?php printf('<input type="checkbox" name="late[%1$d]" id="late_%1$d" value="-1">', $learner['id']); ?>
                                </td>
                                <td>
                                    <!-- <input type="checkbox" name="disrupt" id="disrupt" value="-1"> -->
                                    <?php printf('<input type="checkbox" name="disrupt[%1$d]" id="disrupt_%1$d" value="-1">', $learner['id']); ?>
                                </td>
                                <td>
                                    <!-- <input type="checkbox" name="equip" id="disrupt" value="-1"> -->
                                    <?php printf('<input type="checkbox" name="disrupt[%1$d]" id="disrupt_%1$d" value="-1">', $learner['id']); ?>
                                </td>
                                <td>
                                    <!-- <input type="checkbox" name="lazy" id="lazy" value="-1"> -->
                                    <?php printf('<input type="checkbox" name="lazy[%1$d]" id="lazy_%1$d" value="-1">', $learner['id']); ?>
                                </td>
                                <td>
                                    <!-- <input type="checkbox" name="phone" id="phone" value="-1"> -->
                                    <?php printf('<input type="checkbox" name="phone[%1$d]" id="phone_%1$d" value="-1">', $learner['id']); ?>
                                </td>
                                <td>
                                    <!-- <input type="checkbox" name="rude" id="rude" value="-1"> -->
                                    <?php printf('<input type="checkbox" name="rude[%1$d]" id="rude_%1$d" value="-1">', $learner['id']); ?>
                                </td>
                                <td>
                                    <!-- <input type="checkbox" name="effort" id="effort" value="1"> -->
                                    <?php printf('<input type="checkbox" name="effort[%1$d]" id="effort_%1$d" value="-1">', $learner['id']); ?>
                                </td>
                        </tr>
                    <td></td>
                    <? endif; ?> 
                                                
                        <!-- <input type="hidden" name="id" value="<?php echo $learner['id']; ?>"> -->
                                                
                </form>
            <?php endforeach; ?>    
        <?php endforeach; ?>
    </table>
    						<input type="submit" value="Submit">
  </body>
</html>

And the table looks fine now, but just not submitting the values to the DB.
Thanks,
Shane

Hi,
I presume I still need to make some changes to the index.php file.
Perhaps something like this, but i am not sure,


  try
  {
    $sql = 'INSERT INTO notes SET
        absence = :absence,
        late = :late,
        .........................';
    $s = $pdo->prepare($sql);
    $s->bindValue(':absence', $_POST['absence[]']);
    $s->bindValue(':late', $_POST['late[]']);
    .......................................................................
    $s->execute();

Or maybe,

    $s->bindValue(':absence', $_POST[$absence[]]);
    $s->bindValue(':late', $_POST[$late[]]);

Thanks,
Shane

then check again in the HTML output (as found in the browser when you do ‘view source code’) how many you see.

Hi Dormilich,
I see what you mean now about the </form> tag. Because it is inside the foreach() loops it is being closed multiple times. I think i have fixed that now.
I have simplified the code to only deal with one field of input now and if I can get that to work it should be easy enough to add more later.
Also I have taken a different approach, but also trying to create an array for each absence value for each student. This idea came from looking at code in Kevin Yank’s book ‘PHP and MYSQL Novice to Ninja’ p.222 5th Ed.
Here is my next attempt. I know it isn’t correct but hopefully I can fix it.
the HTML form,

   <table>
         <form action="?addnotes" method="post">
            <tr>
            <th>Course</th>
            <th>Student</th>
            <th>Absence</th>
            </tr>
            <?php foreach (array_reverse($courses) as $course): ?>
            <tr>
                <td>
                <?php htmlout($course['course']);?>
                </td>
                    <?php foreach (array_reverse($learners) as $learner): ?>
                        <? if($course['id']==$learner['courseid']): ?>
                        <td>
                        	<?php htmlout($learner['learner']);?>             
                        	<?php htmlout($learner['id']);?>          
                        </td>
                        <td>
                   		<input type="checkbox" name="absence[]" id="<?php htmlout($learner['id']);?> " value="-1">
                        </td>
                        </tr>
                    	<td></td>
                    	<? endif; ?>                    
            	<?php endforeach; ?>    
        <?php endforeach; ?>
        </form>
    </table>
    	<input type="submit" value="Submit">

index.php (excerpt)

if (isset($_GET['addnotes']))
{
  include $_SERVER['DOCUMENT_ROOT'] . '/artgibney/includes/db.inc.php';

  try
  {
    $sql = 'INSERT INTO notes SET
        absence = :absences';
    $s = $pdo->prepare($sql);
    foreach($_POST['absence'] as $absence)
    {
    $s->bindValue(':absences', $absence);
    $s->execute();
    }
  }
  catch (PDOException $e)
  {
    $error = 'Error adding submitted daily notes data.';
    include 'error.html.php';
    exit();
  }

  header('Location: .');
  exit();
}

Thanks,
Shane

Obviously, I made a mistake in putting the

    	<input type="submit" value="Submit">

outside the </form> tag. I have corrected that now and it seems to be working.

   <table>
         <form action="?addnotes" method="post">
            <tr>
            <th>Course</th>
            <th>Student</th>
            <th>Absence</th>
            </tr>
            <?php foreach (array_reverse($courses) as $course): ?>
            <tr>
                <td>
                <?php htmlout($course['course']);?>
                </td>
                    <?php foreach (array_reverse($learners) as $learner): ?>
                        <? if($course['id']==$learner['courseid']): ?>
                        <td>
                        	<?php htmlout($learner['learner']);?>             
                        	<?php htmlout($learner['id']);?>          
                        </td>
                        <td>
                   		<input type="checkbox" name="absence[]" id="<?php htmlout($learner['id']);?> " value="-1">
                        </td>
                        </tr>
                    	<td></td>
                    	<? endif; ?>                    
            	<?php endforeach; ?>    
        <?php endforeach; ?>
        <input type="submit" value="Submit">
        </form>
    </table>

Now to add some more fields and see if it still works :slight_smile:
Thanks,
Shane

Hi,
I am trying to post the learner’s id with the absence field, so that when a student is marked absent, their id is also recorded, as i need to know who is absent. But I can’t get it to work! :confused: The ‘notes’ table looks like,

| id | absence | userid |

| | | |

The id in the first column is irrelevant.

   <table>
         <form action="?addnotes" method="post">
            <tr>
            <th>Course</th>
            <th>Student</th>
            <th>Absence</th>
            </tr>
            <?php foreach (array_reverse($courses) as $course): ?>
            <tr>
                <td>
                <?php htmlout($course['course']);?>
                </td>
                    <?php foreach (array_reverse($learners) as $learner): ?>
                        <? if($course['id']==$learner['courseid']): ?>
                        <td>
                        	<?php htmlout($learner['learner']);?>             
                        	<?php htmlout($learner['id']);?>          
                        </td>
                        <td>
                        	<input type="hidden" name="id[]" id="<?php htmlout($learner['id']);?>" value="<?php htmlout($learner['id']);?>">
                   		<input type="checkbox" name="absence[]" id="<?php htmlout($learner['id']);?> " value="-1">
                        </td>
                        </tr>
                    	<td></td>
                    	<? endif; ?>                    
            	<?php endforeach; ?>    
        <?php endforeach; ?>
        <input type="submit" value="Submit">
        </form>
    </table>

index.php (excerpt)

if (isset($_GET['addnotes']))
{
  include $_SERVER['DOCUMENT_ROOT'] . '/artgibney/includes/db.inc.php';

  try
  {
    $sql = 'INSERT INTO notes SET
        absence = :absences,
        userid = :id';
    $s = $pdo->prepare($sql);
    foreach($_POST['absence'] as $absence)
    {
    $s->bindValue(':absences', $absence);
    $s->bindValue(':id', $_POST['id']);
    $s->execute();
    }
  }
  catch (PDOException $e)
  {
    $error = 'Error adding submitted daily notes data. Click the back button to continue.';
    include 'error.html.php';
    exit();
  }

  header('Location: .');
  exit();
}

This enters a 0 in the userid column each time.
I tried changing a line in form to,

<input type="hidden" name="id" id="<?php htmlout($learner['id']);?>" value="<?php htmlout($learner['id']);?>">

so that instead of

name="id[]"

I now have

name="id"

This only inserts the id of the last user into the field ‘userid’, which is always 25.
What should I be doing?
Thanks,
Shane

you have still HTML errors (though new ones): <form> and <input> are not allowed as child elements of <table> as well as your </tr>s not matching up.

This only inserts the id of the last user into the field ‘userid’, which is always 25.

see post #2 paragraph 3.

This enters a 0 in the userid column each time.

because $_POST[‘id’] is an array and not a number.

Hi Dormilich,
Regarding the HTML, I know that there is a <td></td> which is outside of an <tr> </tr> tags, but when i played around that was the only way that i could get the table to do what i wanted. Attached are two images of the table. The first image 001.jpg is where the <td></td> tags outside of the row tags, have been taken out and for the second one they have been put in.

<form> and <input> are not allowed as child elements of <table>

Does that mean they cannot be used in a table at all?
If I use,

printf('<input type="checkbox" name="absence[%1$d]" id="absence_%1$d" value="-1">', $learner['id']);

as you recommend, how should I handle the variable $learner[‘id’] in the inde.php file? And is it correct to use the following method to get the absence values into the table ‘notes’?

    $sql = 'INSERT INTO notes SET
        absence = :absences';
    $s = $pdo->prepare($sql);
    foreach($_POST['absence'] as $absence)
    {
    $s->bindValue(':absences', $absence);
    $s->execute();
    } 

Thanks for your help,
Shane

Does that mean they cannot be used in a table at all?

you’re not restricted as to what you put outside of the table and inside of the <td>, but despite that the allowed tags and where to place them is fixed.

if you find that a table does not play well with your form code, consider using something other than a table. you may for instance consider to use a list of tables, or even simpler, put a table inside a form and repeat that (the forms).

If I use […] as you recommend, how should I handle the variable $learner[‘id’] in the inde.php file?

the learner ID is the array key in your absence array.

You’re not really doing the table html properly - looping around for each course, then looping around all the students and displaying the remainder of the row when the students match the course-id, you’re not giving yourself a way to display the cell in the first column for the second and subsequent student on each course. You either need to count the students on each course first and add a “rowspan” value to the course information cell, or display the course information for the first row and a space for subsequent ones. Your workaround probably won’t work consistently on other browers, and it will lead to messing around to figure out why not.


         <form action="?addnotes" method="post">
            <table>
            <tr>
            <th>Course</th>
            <th>Student</th>
            <th>Absence</th>
            </tr>
            <?php foreach (array_reverse($courses) as $course): 
            firstcourse = true; // set a flag each time we have a new course id
            foreach (array_reverse($learners) as $learner): ?>
                       <tr>
                        <? if($course['id']==$learner['courseid']): 
                        if (firstcourse) {
                            firstcourse = false; // so we only display the first column once
                            echo '<td>' . htmlout($course['course']) . '</td>';
                            }
                        else {
                            echo '<td>&nbsp;</td>'; } // and display an empty field in the first column for second and subsequent rows
                            ?>
                        <td>
                        	<?php htmlout($learner['learner']);?>             
                        	<?php htmlout($learner['id']);?>          
                        </td>
                        <td>
                        	<input type="hidden" name="id[]" id="<?php htmlout($learner['id']);?>" value="<?php htmlout($learner['id']);?>">
                   		<input type="checkbox" name="absence[]" id="<?php htmlout($learner['id']);?> " value="-1">
                        </td>
                        </tr>
                    	<? endif; ?>                    
            	<?php endforeach; ?>    
        <?php endforeach; ?>
        </table>
        <input type="submit" value="Submit"> // this either needs to be in a table cell, or completely outside the table
        </form>

For some reason I can’t edit the post above (despite having edited it a couple of times already) - but in the code, obviously where I’ve written “firstcourse” it should read “$firstcourse”:


$firstcourse = true;
...
if ($firstcourse)  {
  $firstcourse = false;

Sorry for the typo.

Hi,

Droopsnot: thanks for the code. I see how it should be now. I would not have thought of putting a php if() condition around a set of <td> cells, but of course it makes sense. Thank you very much for this.

Dormilich:

the learner ID is the array key in your absence array.

which is id. So then I should use,

$sql = 'INSERT INTO notes SET
        absence = :absences,
        userid = :id';
    $s = $pdo->prepare($sql);
    foreach($_POST['absence'] as $absence)
    {
    $s->bindValue(':absences', $absence);
    $s->bindValue(':id', $id);
    $s->execute();
    } 

or maybe

    foreach($_POST['absence'] as $absence)
    {
    $s->bindValue(':absences', $absence);
    $s->bindValue(':id', $absence['id']);

I will try these anyway and see how it goes,
Thanks,
Shane

Hi,
In the table, using,

                        <td>
                   		<?php printf('<input type="checkbox" name="absence[%1$d]" id="absence_%1$d" value="-1">', $learner['id']); ?>  
                        </td>

This in index.php gives an error,

        foreach($_POST['absence'] as $absence)
    {
    $s->bindValue(':absences', $absence);
    $s->bindValue(':id', $id);
    $s->execute();

the error is,

Error adding submitted daily notes data…

So then I tried,

    foreach($_POST['absence'] as $absence)
    {
    $s->bindValue(':absences', $absence);
    $s->bindValue(':id', $absence['id']);
    $s->execute();

This puts a zero in the ‘userid’ field of the table. Which I presume is because this is a number and not string! What about passing users name instead of their id?
By the way the -1 value is appearing in the DB table fine.
Thanks,
Shane

Hi droopsnoot,

For some reason the courses are all displaying horizontally in a row above the headings.
I will include an image.
Here is the source page,

</form>
    <h1>Manage Daily Notes</h1>
    <p><a href="?addcourseform">Add Course</a>  &#8658;  
	 	<a href="?addlearnerform">Add Student</a>
	  
	 	  &#8658;  <a href="?courses">Manage Courses and Students</a></p>
	   <table>
                 <form action="?addnotes" method="post">
            <table>
            <tr>
            <th>Course</th>
            <th>Student</th>
            <th>Absence</th>
            </tr>
                                   <tr>
                         
                        </tr>
                                           
                                       <tr>
                         
                        </tr>
                                           
                                       <tr>
                         
                        </tr>
                                           
                                       <tr>
                        Course1<td></td>                        <td>
                            Student4             
                            30          
                        </td>
                        <td>
                           <!-- <input type="hidden" name="id[]" id="30" value="30"> -->
                           <!-- <input type="checkbox" name="absence[]" id="30 " value="-1"> -->
                           <input type="checkbox" name="absence[30]" id="absence_30" value="-1"> 
                        </td>
                         
                        </tr>
                                           
                                       <tr>
                         
                        </tr>
                                           
                                       <tr>
                        <td>&nbsp;</td>                        <td>
                            Student6             
                            28          
                        </td>

For some reason there are many more <tr> rows than there should be, I think.
Thanks,
Shane

Avoid use of short tags, e.g.

 <? if($course['id']==$learner['courseid']): 

Use full start tags.

 <?php if($course['id']==$learner['courseid']):