2

Hi I am trying to make a form sticky. It creates a grid, and the user can then select a value for each row.

$subjectListArray has values of subject names like 'english', 'maths','speaking and listening' and $gradeSetArray carries a selection of grades like a,b,c etc

When I press submit I want the values that were selected to stay selected.

It works fine for subjects with NO SPACES in their names. As soon as I introduce an array value like 'Speaking and listening' its not sticky.

SO the space is causing me a problem.

<form>
<?php
foreach ($subjectListArray as $subject){
    echo "<tr><td>$subject</td>";
    foreach ($gradeSetArray as $grade){
        echo '<td><input type="radio" name="'.$subject.'" value="'.$grade.'" ';
            if ( isset($_POST[$subject])and $_POST[$subject]==$grade){
                echo 'checked="checked"';
            }
        echo ' /></td>';
    }
    echo "</tr>";
}
?>

<input type="submit" name="submitPupilData" value="Input data" />
</form>

Here's a little of the html produced:

<td>
<input type="radio" value="p2iic" name="Speaking and Listening">
</td>

So for some reason my if ( isset($_POST[$subject])and $_POST[$subject]==$grade) isn't working if there's a space

What can I do in order to keep the spaces in the names and keep the from sticky?

Thanks

As per Jakar I printed out the $_POST and I noticed that the spaces were being replaced by _ characters thus Array ( [Health_and_well_being] => p2iic

maxelcat
  • 1,333
  • 2
  • 18
  • 31
  • I don't see anything obvious causing the problem. Try doing `print_r($subjectListArray); print_r($_POST); print_r($gradeSetArray)`. Are the array keys/values what you would expect them to be? – Reed Nov 03 '15 at 15:56
  • Also, you can do `inline code` by using \` (to the left of the 1) like: \`inline code\` – Reed Nov 03 '15 at 15:58
  • 1
    good tip - see above – maxelcat Nov 03 '15 at 16:12

1 Answers1

3

It's not possible to use spaces in HTML name attributes. For more information checkout Can I use white spaces in the name attribute of an HTML element?

Community
  • 1
  • 1
Erik Theoboldt
  • 2,168
  • 2
  • 16
  • 21