Jump to content

Radio Buttons on a php search page


MargateSteve

Recommended Posts

Sorry if this should be in the HTML forum but as it is relating to something I am trying to do with php I thought this was the right place!

 

I am creating a search page and one of the columns has only two potential values, 'H' for Home Games and 'A' for Away Games. Using radio buttons I can get the search page to filter correctly but there will also be times, once there are a few more search options on there, when a search would want to show All Games.

The current form I have is

<form id="form1" name="form1" method="post" action="fixrestestsearch.php">
    <p>
      <label for="OPP">OPP</label>
      <input type="text" name="OPP" id="OPP" />
      <br />
           <label>
        <input type="radio" name="HA" value="H" id="HA_0" />
        Home</label>
      <br />
      <label>
        <input type="radio" name="HA" value="A" id="HA_1" />
        Away</label>
      <br />
      <label>
        <input type="radio" name="HA" value="" id="HA_2" />
        All</label>
      <br />
      <br />
      SUB
<input type="submit" name="SUB" id="SUB" value="Submit" />
    </p>
</form>

 

and the query in the results page contains

 WHERE team_name='$OPP' AND h_a='$HA'

 

If either the 'Home' or 'Away' buttons are checked everything works fine. If I check the 'All' button nothing is returned as it is looking for blank cells. I have also tried using 'value="*"' but that did not work.

 

Is there anything that I can put as the value of a radio button that would select all records or would it have to be something amended in the query on the results page?

 

Thanks in advance

Steve

Link to comment
https://forums.phpfreaks.com/topic/218058-radio-buttons-on-a-php-search-page/
Share on other sites

Test for the $_POST['HA'] value and only add it if they have chosen home or away.  Make your HA value for the all games selection equal all.

 

<label><input type="radio" name="HA" value="all" id="HA_2" />All</label>

 

<?php
$sql = "SELECT * FROM your_db WHERE team_name = '$OPP'";

if ($_POST['HA'] != 'all')
    $sql .= " AND h_a = '$HA'";

mysql_query($sql);
?>

 

This is untested but you can add in the proper query using your query with this logic.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.