0

I have an HTML form that I need to then reference to in PHP, so that I can eventually filter through data. Right now it is just echoing a bit of text for testing purposes.

I use the $_GET variable to get which values are equal to what, then use an if/else statement to tell me if each is checked.

If I say it only equals one value (== .25) it returns false.

However if I add one more or both more values (== .25 or .375 or .5) it will return the value I need.

How can I get it to return true with only one value?

    <table stlye="width:100%">
        <tr>
            <td style="width:50%">
            <form method="GET">
                    Tool Diameter: <br>
                        <input type="checkbox" name="Tool Diameter" value=.25 checked> .25<br>
                        <input type="checkbox" name="Tool Diameter" value=.375 checked> 3/8<br>
                        <input type="checkbox" name="Tool Diameter" value=.5 checked> 1/2<br><br>
                    Brand: <br>
                        <input type="checkbox" name="Brand" value="Lakeshore Carbide " checked> Lakeshore Carbide<br>
                        <input type="checkbox" name="Brand" value="AB Tools" checked> AB Tools<br>
                        <input type="checkbox" name="Brand" value="Helical Tools" checked> Helical Tools<br><br>
                    Flutes: <br>
                        <input type="checkbox" name="Flutes" value="2" checked> 2<br>
                        <input type="checkbox" name="Flutes" value="3" checked> 3<br>
                        <input type="checkbox" name="Flutes" value="4" checked> 4<br><br>
                    Tool Material: <br>
                        <input type="checkbox" name="Material" value="HSS" checked> HSS<br>
                        <input type="checkbox" name="Material" value="Carbide" checked> Carbide<br>
                        <input type="checkbox" name="Material" value="Cobalt" checked> Cobalt<br><br>
                    Coating: <br>
                        <input type="checkbox" name="Coating" value="Uncoated" checked> Uncoated<br>
                        <input type="checkbox" name="Coating" value="ZrN" checked> ZrN<br>
                        <input type="checkbox" name="Coating" value="TiCN" checked> TiCN<br><br>
                    Tool Type: <br>
                        <input type="checkbox" name="Type" value="Face Mill" checked> Face Mill<br>
                        <input type="checkbox" name="Type" value="Flat Endmill" checked> Flat Endmill<br>
                        <input type="checkbox" name="Type" value="Ball Endmill" checked> Ball Endmill<br> 
                    <br><button>Filter</button><br>
                </form>
            </td>
            <td style="width:50%">
                <style type="text/css">
                    td
                    {
                    padding:0 50px 0 50px;
                    }
                </style>
        <?php
            //while (true){     
            if ($_GET['Tool Diameter'] == .375) {
              echo 'test = true';
            }
            else {
              echo "false";
            }
    ?>
            </td>
        </tr>

    </table>
mplungjan
  • 169,008
  • 28
  • 173
  • 236
Alex
  • 7
  • 4
  • 1
    You need to convert to number or test strings: `if ($_GET['Tool Diameter'] == ".375") {` - also please quote the values in the form: `name="Tool Diameter" value=".375"` – mplungjan May 17 '18 at 12:47
  • Voting to close as _This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting._ – mplungjan May 17 '18 at 12:49
  • so many comments (sorry) - avoid inline css / js - it's bad practice. Place all cs / js in external files where possible. Place – treyBake May 17 '18 at 12:50
  • @mplungjan this actually didn't work, it still only posts true with two values with an or – Alex May 17 '18 at 13:02
  • There is not enough information in your question to tell why exactly you are trying to do. My comments are valid and is perhaps the first two things you need to fix out of many – mplungjan May 17 '18 at 13:03
  • 1
    You really should change `name="Tool Diameter"` to `name="ToolDiameter[]"` and read as an array on the server – mplungjan May 17 '18 at 13:06

3 Answers3

0

Convert the values to strings e.g.

<input type="checkbox" name="Tool Diameter" value=".375" checked> .375<br>

then check

if ($_GET['Tool Diameter'] == ".375"){
enter code here
}
AJFMEDIA
  • 2,093
  • 6
  • 29
  • 52
0

1st mistake

values have to be wrapped in parenthesise value=".25"

2nd mistake

names have to be unique, as a result you have only one value in the array $_GET['Tool Diameter'] you should have a slot in $_GET that contains i.e. array of your results, so lets say

$_GET['Dimensions'] = [
  'Dimension 1' => '.25',
  'Dimension 2' => '.375',
  'Dimension 3' => '.5'
];

and then refer to each of them separately

David C.
  • 201
  • 2
  • 11
  • @mplungjan you're wrong. I'm sending you to get close with this answer https://stackoverflow.com/questions/2203430/posting-form-fields-with-same-name-attribute – David C. May 17 '18 at 13:06
0

try this

<input type="checkbox" name="Tool Diameter[]" value=".25" checked> .25<br>
<input type="checkbox" name="Tool Diameter[]" value=".375" checked> 3/8<br>
<input type="checkbox" name="Tool Diameter[]" value=".5" checked> 1/2<br><br>

..... 
<?php 
// for checking the condition 'atleast 1 ' should be checked 
if(sizeof($_GET['Tool Diameter']) >=1){
 echo 'test = true';
}
else {
  echo "test = false";
}

?>
Jayakrishnan
  • 1,295
  • 2
  • 13
  • 27