Cant get $_POST values from multiple forms

2019-09-20 14:56发布

i got a bit of a problem, as you guys can see in my code, i got a foreach looping through my img folder, showing all the images, well i left out the proper styling of the images, since the problem is the form inside every image.

i need to get the value from the radio input, so i can tell my other script to select all the info from the DB where the ID has the same value as the input.

and i needed a if statement so that if nothing is set i can show something saying you need to select a radio before you can listen to any stations.

but my if statement keeps showing 'error', even tho i click on the buttons

i hope anyone could help me out :)

if(isset($_POST['submitRadio')):
    $test = $_POST['radio'];
    echo $test;
else:
    echo 'error';
endif;

foreach(glob('img/radios/big/*.png') as path):
    printf('
        <form method="post">
            <input class="hidden" type="text" name="radio"  value="1">
            <input type="submit" name="submitRadio" value="Go!">
        </form>
    ');
endforeach;

after doing what GolezTrol told me to do:

<div class="row text-center">
              <?php
                $sql = "SELECT * FROM radio";

                $stmt = $Db->dbh->prepare($sql);
                $stmt->execute();

                $result = $stmt->fetchAll();

                if(isset($_POST['submitRadio'])):
                  $test = $_POST['submitRadio'];
                  echo $test;
                else:
                  echo 'error';
                endif;

              ?>

              <form method="post">

              <?
                foreach($result as $radio):

                  printf('

                  <button type="submit"
                          name="submitRadio"
                          value="'.$radio['id'].'">
                     Go!
                  </button>

                  ');
              ?>

              </form>

2条回答
smile是对你的礼貌
2楼-- · 2019-09-20 15:42

I've removed my previous solution. I only just saw that you have no actual radio button but hidden elements, so basically the buttons are the radios. That changes it a little bit because you actually have the alternative suggestion I proposed earlier: The buttons themselves work as radiobuttons that immediately post the chosen option.

To solve your issue, you can use a button instead of an input for submitting the form. In a button, you can make the caption and the value differ from each other, so you can just put the id (or filename, or whatever you need) in the value of the button and don't need the hidden input at all. The content of the button tag is the caption it gets, so the code can look like this:

if(isset($_POST['selectedFile')):
    $test = $_POST['selectedFile'];
    echo $test;
else:
    echo 'error';
endif;

?><form method="post"><? // Open the form outside of the loop

foreach(glob('img/radios/big/*.png') as path):
    // Not sure what you want to do here. Put the filename instead of 
    // "1" in the value?
    $filename = basename($path, '.png');
    ?>
      <button type="submit" 
              name="selectedFile" 
              value="<?=$filename?>">
         Go!
      </button>
    <?
endforeach;

?></form><? // Close the form

In this example I've pulled the form tags out of the loop, because strictly you only need one form. But since there are now no radiobuttons at all, you could also have one form per button. It would still work.

By the way, I saw you mentioned a database in your comment. Of course I didn't take that into account, since you didn't mention that in the question. Anyway, it shouldn't change the basic concept, only the way the 'value' is determined.

查看更多
淡お忘
3楼-- · 2019-09-20 15:43

First, the value code in radio tag is always 1. So if any radio button is selected, you will always gets 1. Instead the value clause should have the filename. So you get to know which image (radio button) is selected.

For none of the image (radio button) selected, you may use

if (!isset($_POST['radio']))<br>{<br>//php code }                    
查看更多
登录 后发表回答