无法从多个表单中获取$ _POST值 [英] Cant get $_POST values from multiple forms

查看:149
本文介绍了无法从多个表单中获取$ _POST值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一些问题,因为你们可以在我的代码中看到,我得到了一个foreach循环访问我的img文件夹,显示所有图像,以及我忽略了图像的正确样式,因为问题是每个图像内的形式。

我需要从收音机输入中获取值,所以我可以告诉我的其他脚本从DB中选择所有信息,其中ID具有与输入相同的值。



我需要一个if语句,这样如果没有设置,我可以显示一些信息,说明你需要选择一个收音机,然后才能收听到任何电台。

但我的if语句一直显示'错误',即使我点击按钮





  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 =hiddentype =textname =radiovalue =1>
< input type =submitname =submitRadiovalue =Go! >
< / form>
');
endforeach;

完成GolezTrol告诉我要做的事情:

 < 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>


解决方案

我删除了以前的解决方案。我只是看到你没有真正的单选按钮,但隐藏的元素,所以基本上按钮是无线电。
这改变了一点,因为你实际上有我早先提出的替代建议:按钮本身作为单选按钮,立即发布选定的选项。为解决您的问题,您可以使用按钮而不是输入 code>用于提交表单。在一个按钮中,您可以使标题和值彼此不同,因此您可以将 id (或文件名,或任何您需要的)放在值按钮并且根本不需要隐藏的输入。 button标签的内容就是它的标题,所以代码如下所示:

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

?>< form method =post>< ;? //在循环之外打开表单

foreach(glob('img / radios / big / *。png')as path:
//不知道你想做什么这里。把文件名而不是
//1的值?
$ filename = basename($ path,'.png');
?>

在这个例子中,我将表单标签拉出循环,因为严格地说你只需要一种形式。但由于目前没有单选按钮,所以每个按钮也可以有一个表单。它仍然有效。



顺便说一句,我看到你在评论中提到了一个数据库。当然,我没有考虑到这一点,因为你在问题中没有提到这一点。无论如何,它不应该改变基本概念,只有'价值'的确定方式。

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>

解决方案

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.

这篇关于无法从多个表单中获取$ _POST值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆