0

I'm trying to write a web application in which students enter their timetable. For example: First period of Monday is math, second period of Monday is English,... first period of Tuesday is history, second period of Tuesday is biology,... etc.

So I write a form like this:

<form method="post" action="timetable_handling.php">
    <?php
        for ($period=1;$period<=9;$period++)
        {
            echo "<tr>";
            for ($day=1;$day<=7;$day++)
                {
                    echo "<td><input name="the_subject_of_the_nth_$period_on_$day" type="text"></td></tr>";
                     //the problem is here
                }
        } 
    ?>
</form>

So my question is, are there any ways to pass the many variables to another php file to handle without having to manually write its name explicitly?

Edit 1: I mean is there anyway to encode the period and day information in the name, so that when it sends to timetable_handling.php I can just loop through it to save it into sql database. Something like an array $subject[day][period].

I would be grateful if someone could help me.

Tung Nguyen
  • 149
  • 1
  • 8
  • What you do is just fine, except that you have a closer look at different ways to create that variable name. I suggest to look at `sprintf()`. Check the documentation. Oh, and you have to fix those quot chars. You cannot use the same to delimit the string and inside the string. – arkascha Dec 06 '15 at 19:36
  • What do you mean by "many variables"? Can't you retrieve $_POST on the handler file? – Christian Bonato Dec 06 '15 at 19:39
  • @Bonatoc: It's hard to describe. The users enter 63 variables corresponding to the periods and days. I already wrote a loop, so I cant write the name of the input fields something like subject_of_third_period_monday. Are there any ways to make php regconize the $period and $day in the form ? – Tung Nguyen Dec 06 '15 at 19:49

4 Answers4

0

Yes. If you format a variable name something like

        for ($day=1;$day<=7;$day++)
            { ?>
                <td><input name="subject['<?= $period ?>'][<?= $day ?>]" type="text"></td></tr>
                 //the problem is here
            <?php }

PHP will turn $_POST['subject'] into a 2D array for you. (Note I make no promise this is free of syntax errors.)

Darwin von Corax
  • 5,201
  • 3
  • 17
  • 28
0

In the form handler you can loop through all the posted fields like this:

foreach($_POST as $field => $value)
{

}

Where $field would be the input-tag name and $value it's value. If you have other form elements you can check which ones you need with some kind of prefix, like if the fieldname starts with 'the_subject', you know it's one of the fields that were dynamically added.

Santy
  • 387
  • 2
  • 7
0

sure, you already did part of the answer :) first issue: you are not escaping your string properly: Here is 1 another way

echo '<td><input name="'.$period.'_on_'.$day.'" type="text"></td></tr>';

As for handling the post here is what you can do. You might need to tweak it around to get the exact desired result. But you are talking about multidimentional array.

if (isset($_POST)){

    $something=array();
    foreach ($_POST as $single=>$value)
    {
        array_push($something, array('period_'.substr($single,0,1) => array('day_'.substr($single,-1)=>$value)));
    }
}

echo '<pre>'.print_r($something,true).'</pre>';

Good Luck.

Waelio
  • 59
  • 1
  • 9
0

Start with the following on your timetable data entry page:

<form method="post" action="timetable_handling.php">
    <table>
        <?php
            for ($period=1; $period<=9; $period++)
            {
                echo '<tr>';
                for ($day=1; $day<=7; $day++)
                    {
                        echo '<td><input name="subject_of_period_'.$period.'_on_day_'.$day.'" type="text"></td>';
                        //the problem is here
                    }
                echo '</tr>';
            } 
        ?>
    </table>

<input type="submit" value="Submit Form" />
</form>

Then follow up with this script on your timetable_handling.php:

<?php

for ($day=1; $day<=7; $day++)
    {
        for ($period=1; $period<=9; $period++ )
            {
                ${'subject_of_period_'.$period.'_on_day_'.$day} = htmlspecialchars($_POST['subject_of_period_'.$period.'_on_day_'.$day],ENT_QUOTES);
                echo '<p>Subject of Period '.$period.' on Day '.$day.' is '.${'subject_of_period_'.$period.'_on_day_'.$day}.'</p>';
            }
    }

?>

It's secure and it works.

Rounin
  • 27,134
  • 9
  • 83
  • 108
  • Thank you very, very much, it works indeed. I love Stack Overflow. If possible, can you elaborate on the syntax ${something} you used ? I don't know much about the manipulation of variable name. Is this something programmers usually do or do they use alternative methods ? – Tung Nguyen Dec 07 '15 at 09:45
  • 1
    `${...}` is an unusual construct, not seen in many other scripting languages. It is called a `variable variable`. The syntax enables you to declare a variable name, which contains within it strings derived from the values of other variables. For more info, see: http://php.net/manual/en/language.variables.variable.php – Rounin Dec 07 '15 at 10:34
  • @Rounin - not so unusual, as it can be done in Javascript as well. – Christian Bonato Dec 07 '15 at 12:42
  • @Bonatoc - You might just have made my day. I'd always assumed that `variable variables` were not possible in `.js`. Do you have a link? Thanks. – Rounin Dec 07 '15 at 15:43
  • Don't get too excited :-) Sorry, but only the classic methods : http://stackoverflow.com/questions/5187530/variable-variables-in-javascript — I was reffering to angularJS, and also JQuery, where you can concatenate like ${"prefix_"+my_variable_variable} – Christian Bonato Dec 07 '15 at 17:16