Search code examples
phphtmlhtml-tableradio-button

How to post data from radio buttons to table in the next page


Okay @AlexioVay this is I what have on the first page form.php

<?php
$fruits = [
   'Orange' => 10, 
   'Banana' => 12, 
   'Apple' => 15, 
   'Lemon' => 8
];
?>
<form method="post" action="next.php">
    <?php
    // With this syntax we say "$key equals $value" ($fruit => $price)
    foreach($fruits as $fruit => $price) {
        // We use '. $variable .' to merge our variables into HTML
        // Since we are in a loop here all the items from the array will be displayed:
        echo '<input type="checkbox" name="'.$fruit.'" /> 
             $'.$price.' - '.$fruit;
    }
    ?>
    <button type="submit">Submit</button>
</form>

and on my second page next.php,

<table style="width:100%">
  <tr>
<?php
foreach($_POST as $fruit) {
      echo "<td>".$fruit."</td>";
}
?>
  </tr>
</table>

So when i select options on the first page then press submit the data am getting on the next page is just the word "on"

here is example, i dont know if you can open this link https://test.ftgclothing.net then you can see what am talking about


Solution

  • The best way would be a foreach loop that displays what items have been selected in the $_POST variable:

    <table style="width:100%">
      <tr>
    <?php
    foreach($_POST as $fruit) {
          echo "<td>".$fruit."</td>";
    }
    ?>
      </tr>
    </table>
    

    Also you should use checkboxes in the file before, not radio buttons, because with radio buttons you can only select one item of a group of items:

    <form method="post" action="next.php">
        <input type="checkbox" name="orange" /> $10 - Orange
        <input type="checkbox" name="banana" /> $12 - Banana
        <input type="checkbox" name="apple" /> $15 - Apple
        <input type="checkbox" name="lemon" /> $8 - Lemon
        <button type="submit">Submit</button>
    </form>
    

    So, this solution will show you only selected items you checkmarked on the page before, just like you asked in your question. If you also want to display all other items you should create an array for both pages. I assume you are still learning PHP and HTML? Do you want that solution also or try it by yourself?

    Edit: Here comes the array solution:

    form.php

    // We define the array $fruits and assign the price 
    // to each item as Integer value (therefore without quotation marks):
    $fruits = [
       'Orange' => 10, 
       'Banana' => 12, 
       'Apple' => 15, 
       'Lemon' => 8
    ];
    
    <form method="post" action="next.php">
        <?php
        // With this syntax we say "$key equals $value" ($fruit => $price)
        foreach($fruits as $fruit => $price) {
            // We use '. $variable .' to merge our variables into HTML
            // Since we are in a loop here all the items from the array will be displayed:
            echo '<input type="checkbox" name="'.$fruit.'" /> 
                 $'.$price.' - '.$fruit;
        }
        ?>
        <button type="submit">Submit</button>
    </form>
    

    next.php

    <table style="width:100%">
      <tr>
    <?php
    foreach($_POST as $fruit) {
          echo "<td>".$fruit."</td>";
    }
    ?>
      </tr>
    </table>
    

    So, we have created an array that holds all the items. This is much more comfortable if you imagine you would list all the available fruits on earth for example or other long lists. With arrays you can also do things like array_sort to maybe sort them by price, etc. So that's super useful.

    EDIT 29/03:

    This should be in one line and please be aware of the apostroph that is missing:

    echo '<input type="checkbox" name="'.$fruit.'" value="'.$fruit.'" />' . $'.$price.' - '.$fruit;