Here is my problem. I have a HTML form which has around 20 buttons each one with a unique value. Now if the user presses a button say "2D-ECHO" then the user will be redirected to a scheduling page where the value "2D-ECHO" should be displayed. Likewise if the user presses any other button among the 20 buttons the value of that button should be displayed in the next page. FYI all the buttons redirect to the same scheduling page.
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;?>">
I understand that the above PHP code works for one button. How do I make it work for the other buttons. Pls advice.
You could store all the $_POST[''];
arays into another array and use foreach()
loop to go through all of them.
<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>
The below code will allow you to know which one was clicked
<?php
if(isset($_POST['foo'])) {
print_r($_POST);
}
Your form will only submit the button if it was clicked. You could do something with an array:
$buttons = array("button1id", "button2id");
foreach($buttons as $button){
if(isset($_POST[$button])) echo $_POST[$button];
}
or you could do something that requires a little less maintenance over time:
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;
}
As a first step I would look at using 'array' type IDs on the buttons:
<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]">
Then in code you can look under $_POST['m']
You'll of course need to be able to associate those IDs back to the answers when receiving the post.
I may be having a hard time grasping what you are doing. but if you are just clicking buttons and sending their vbalues to the same page, and echoing their values, you would do something like thi:
<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;?>">