1

这是我的问题。我有一个 HTML 表单,它有大约 20 个按钮,每个按钮都有一个唯一的值。现在,如果用户按下按钮说“2D-ECHO”,那么用户将被重定向到应该显示值“2D-ECHO”的调度页面。同样,如果用户按下 20 个按钮中的任何其他按钮,则该按钮的值应显示在下一页中。仅供参考,所有按钮都重定向到同一个调度页面。

HTML:

<form name="Left" method="post" action="Schedule_Devp.php">
 <div id="men" class="grid_4 alpha" style="margin:1.5 em; text-align:center">
    <label class="label">Men</label><br>
    <span class="grid_2 alpha"><input type="submit" class="buttons-men" value="BELOW 30 YRS WELLNESS CHECK" name="m_below30"></span>
    <span class="grid_2 omega"><input type="submit" class="buttons-men" value="35-40 YRS WELLNESS CHECK" name="m_below40"></span><br>
    <span class="grid_2 alpha"><input type="submit" class="buttons-men" value="CANCER CHECK" name="m_cancer"></span>
    <span class="grid_2 omega"><input type="submit" class="buttons-men" value="45 AND ABOVE WELLNESS CHECK" name= "m_above45"></span><br> 
    <span class="grid_2 alpha" style="visibility:hidden"><input type="button" class="buttons-men" ></span>
    <span class="grid_2 omega"><input type="submit" class="buttons-men" value="2D ECHO" name="m_2d"></span><br>      
    </div>
    <div class="clear"></div>
</form>

PHP:

<?php
$m_below40 = $_POST['m_below40'];
?>
<input type="button" class="buttons-specific-screenings" value="<?php echo     $m_below40;?>">

我知道上面的 PHP 代码适用于一个按钮。我如何使它适用于其他按钮。请建议。

4

4 回答 4

2

您可以将所有数组存储$_POST[''];到另一个数组中并使用foreach()循环遍历所有数组。

<form action="" method="POST"> 
    <?php
    $m_below40 = "one";
    $m_below30 = "two";
    $m_below20 = "four";
    $m_below10 = "three";

    $all = array($m_below40,$m_below30,$m_below20,$m_below10);
    ?>

    <?php 
    foreach ($all as $single ) { ?>
    <input type="submit" class="buttons-specific-screenings" name="foo" value="<?php echo $single;?>"> 

    <?php } ?>



    </form>

下面的代码将让您知道点击了哪一个

<?php

if(isset($_POST['foo'])) {
print_r($_POST);



}
于 2013-02-05T17:42:29.637 回答
1

您的表单仅在单击按钮时才会提交该按钮。你可以用数组做一些事情:

$buttons = array("button1id", "button2id");
foreach($buttons as $button){
   if(isset($_POST[$button])) echo $_POST[$button];
}

或者您可以做一些随着时间的推移需要较少维护的事情:

foreach($_POST as $key => $value){
        //check if the key (button name) has the word button in it. If so, echo the value
        if(strstr($key, 'button') !== false) echo $value;
}
于 2013-02-05T17:41:38.857 回答
1

作为第一步,我将考虑在按钮上使用“数组”类型 ID:

<input type="submit" class="buttons-men" value="BELOW 30 YRS WELLNESS CHECK" id="m[0]">
<input type="submit" class="buttons-men" value="35-40 YRS WELLNESS CHECK" id="m[1]">
<input type="submit" class="buttons-men" value="CANCER CHECK" id="m[2]">

然后在代码中你可以查看$_POST['m']

您当然需要能够在收到帖子时将这些 ID 关联回答案。

于 2013-02-05T17:42:01.913 回答
1

我可能很难理解你在做什么。但是,如果您只是单击按钮并将它们的 vbalues 发送到同一页面,并回显它们的值,那么您将执行以下操作:

<form name="Left" method="post" action="Schedule_Devp.php">
<div id="men" class="grid_4 alpha" style="margin:1.5 em; text-align:center">
<label class="label">Men</label><br>
<span class="grid_2 alpha"><input type="submit" class="buttons-men" value="BELOW 30 YRS WELLNESS CHECK" name="m_below30"></span>
<span class="grid_2 omega"><input type="submit" class="buttons-men" value="35-40 YRS WELLNESS CHECK" name="m_below40"></span><br>
<span class="grid_2 alpha"><input type="submit" class="buttons-men" value="CANCER CHECK" name="m_cancer"></span>
<span class="grid_2 omega"><input type="submit" class="buttons-men" value="45 AND ABOVE WELLNESS CHECK" name= "m_above45"></span><br> 
<span class="grid_2 alpha" style="visibility:hidden"><input type="button" class="buttons-men" ></span>
<span class="grid_2 omega"><input type="submit" class="buttons-men" value="2D ECHO" name="m_2d"></span><br>      
</div>
<div class="clear"></div>
</form>

php

<?php
foreach ($_REQUEST as $this) { ?>
<input type="button" class="buttons-specific-screenings" value="<?php echo     $this;?>">
于 2013-02-05T17:45:25.123 回答