phphtmlarraysmultidimensional-arraysubmission

Get form submission data from radio buttons generated by a 2d array


I created an HTML table containing radio fields using a multidimensional array.

How can I get the selected values from the form submission?

My current code:

$cakedata = array(
    array("shape" => "Heart",       "flavor" => "Chocolate",    "toppings" => "Cookies"),
    array("shape" => "Rectangle",   "flavor" =>  "Vanilla",     "toppings" =>  "Spun-sugar Flowers"),
    array("shape" => "Square",      "flavor" =>  "Lemon",       "toppings" => "Mini Chocolate Candies"),
    array("shape" => "Round",       "flavor" =>  "Cheesecake",  "toppings" => "Marshmallows")
);
echo "<form action = 'diycake.php' method = 'post'>";
echo "<table><table border = '1'>";
echo "<tr>";
    echo "<th> Cake Shape </th>";
    echo "<th> Cake Flavor </th>";
    echo "<th> Cake Toppings </th>";
$i = 0;
foreach($cakedata as $row => $innerArray){
    foreach($innerArray as $innerRow => $value){
        if($i==0){
            echo "<tr>";
        }
        echo "<td><input type ='radio' name ='radio".$i."'>".$value."</td>";
        if($i==2){  
            echo "</tr>"; 
            $i=-1;
        }
        $i++;
    }
}
echo "</tr>";
echo "</table><br>";
echo "<input type = 'submit' name = 'submit' value = 'Submit'>";
echo "</form>";
echo '<pre>'; print_r($cakedata); echo '</pre>';

How do I receive the user's selections?


Solution

  • <html>
        <head>
            <title>DIY Cake</title>
        </head>
        <body>
            <h1>D-I-Y Cake</h1>
            <?php
            $cakedata = array(
                            array("shape" => "Heart",       "flavor" => "Chocolate",    "toppings" => "Cookies"),
                            array("shape" => "Rectangle",   "flavor" =>  "Vanilla",     "toppings" =>  "Spun-sugar Flowers"),
                            array("shape" => "Square",      "flavor" =>  "Lemon",       "toppings" => "Mini Chocolate Candies"),
                            array("shape" => "Round",       "flavor" =>  "Cheesecake",  "toppings" => "Marshmallows")
                    );
            echo "<form action = 'diycake.php' method = 'post'>";
            echo "<table><table border = '1'>";
            echo "<tr>";
                echo "<th> Cake Shape </th>";
                echo "<th> Cake Flavor </th>";
                echo "<th> Cake Toppings </th>";
                    $i = 0;
                    foreach($cakedata as $row => $innerArray){
                        foreach($innerArray as $innerRow => $value){
                            if($i==0){
                                echo "<tr>";
                                }
                                echo "<td><input type ='radio' name ='radio".$i."' value='" . $value . "'>".$value."</td>";
                            if($i==2){  
                                echo "</tr>"; 
                                $i=-1;
                                }
                            $i++;
                        }
                    }
            echo "</tr>";
            echo "</table><br>";
            echo "<input type = 'submit' name = 'submit' value = 'Submit'>";
            echo "</form>";
    
            ?>
    
        </body>
    </html>
    

    diycake.php

    <?php
    
    // check, if form was sent
    if (isset($_POST['submit'])) {
      $outerNumberArray = 4; // actually the outer number of your array
      for($i = 0; $i < $outerNumberArray; $i++) {
        if (isset($_POST['radio' . $i])) {
          echo "Your value from radio" . $i . ": " . $_POST['radio' . $i] . PHP_EOL;
        }
      }
    }