0

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

sally
  • 29
  • 7
  • Point 1: You should only be able to select 1 Radio Button. Thats the whole reason for their existance. They should be Checkboxes if you want to be able to select more than one – RiggsFolly Mar 18 '22 at 11:40
  • 1
    With some `if isset` statements, I'd suggest. – ADyson Mar 18 '22 at 11:40
  • P.S. Using radio buttons here makes no semantic sense if the user can select more than one option. Use checkboxes instead. Radio buttons are to be used when you want to force the user to choose a single option from the list. – ADyson Mar 18 '22 at 11:40
  • 1
    What actually happens with the code you've shown, and what do you want to happen instead? – IMSoP Mar 18 '22 at 11:45

1 Answers1

1

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;
AlexioVay
  • 4,338
  • 2
  • 31
  • 49
  • Of course this assumes that nothing else was POSTed in the same request, except this one list. As per what the OP showed, that's true, but it may be a reduced sample. (There's nothing wrong with your answer, it's just worth mentioning this as a caveat.) – ADyson Mar 18 '22 at 11:45
  • That checkbox HTML isn't valid, technically - https://jsfiddle.net/zdptvx72/. – ADyson Mar 18 '22 at 11:47
  • Still editing, sorry – AlexioVay Mar 18 '22 at 11:51
  • yes am still a beginner tying to learn, i will really appreciate the solution as well – sally Mar 18 '22 at 12:26
  • @sally I just made an edit to my post. I have put important information in comments starting with `//`. I hope it helps you. If it does, please checkmark this question as answered. Feel free to ask any questions if you still have some. – AlexioVay Mar 18 '22 at 15:48
  • @AlexioVay Looks like am getting there with your solution but the only problem is on next.php its displaying the value as "on" .... thats where hit the wall now. – sally Mar 23 '22 at 19:12
  • Do you mean that all checkboxes you selected are showing but not the other (unchecked) checkboxes? You can edit your current post and show me the code you have right now or create a new question here on StackOverflow and inform me again. @sally – AlexioVay Mar 23 '22 at 19:55
  • @AlexioVay I edited my post you can check it out please. – sally Mar 26 '22 at 16:59
  • @sally Hmm, it's strange that "on" is displayed since we didn't put that in the code. But I noticed that you are missing an apostrophe ( ' ) and also the dot after your input tag is closed in `form.php`. In PHP, the string operator dot (.) is used to concatenate strings: https://stackoverflow.com/questions/4266799/why-is-the-php-string-concatenation-operator-a-dot#:~:text=In%20PHP%2C%20the%20string%20operator,%3D%20%22Hello%20there%2C%20%22%20. – AlexioVay Mar 28 '22 at 23:36
  • @sally Please see my edit, I totally missed that you didn't have a `value` tag inside your input which tells your form what data to actually send since your array values aren't included automatically. I'm sorry. But I hope you learned a bit by that? Now you wouldn't need to debug in `next.php` but it is super helpful to find bugs generally. When you use a popular php framework like symfony or Laravel you would use a lot of `dump($variable)` or `dd($variable)` which is an advanced version of `var_dump($variable); exit;` to find out what data you receive like `var_dump($_POST);`. – AlexioVay Mar 28 '22 at 23:42