Jump to content

[SOLVED] radio buttons


spooke2k

Recommended Posts

need some help i have a series of radio buttons which when are clicked i want to auto the php page to refresh and then exercute my sql quries and return information.

 

on my form i am using <form action= <?php echo $_SERVER["../PHP_SELF"]; ?> method="POST">

</div>

  <label></label>

  <div align="center">

    <table width="226" border="1" align="left">

      <tr>

        <td width="144">Boot Room</td>

        <td width="66"><div align="center">

            <input name="group1" type="radio" value="boot" >

        </div></td>

      </tr>

      <tr>

        <td height="30">Wildlife</td>

        <td><div align="center">

          <input type="radio" name="group1" value="Wildlife">

        </div></td>

      </tr>

      <tr>

        <td>Gardening</td>

        <td><div align="center">

            <input type="radio" name="group1" value="Gardening">

        </div></td>

      </tr>

      <tr>

        <td>Outdoor Life</td>

        <td><div align="center">

            <input type="radio" name="group1" value="outlife">

        </div></td>

      </tr>

      <tr>

        <td>Home</td>

        <td><div align="center">

            <input type="radio" name="group1" value="home">

        </div></td>

      </tr>

      <tr>

        <td>Gifts </td>

        <td><div align="center">

            <input type="radio" name="group1" value="gifts">

        </div></td>

      </tr>

      <tr>

        <td><input type="text" name="textfield"></td>

        <td><div align="center">

          <p>

            <input type="submit" value="Submit" name="submit_button"></p>

 

        </div></td>

      </tr>

    </table>

 

</form>

 

<?php

 

switch(group1){

    case 'boot':

      $cat="1";

    break;

    case 'wildlife':

$cat="2";

  break;

    case 'gardening':

  $cat="3";

  break;

  case 'outlife':

  $cat="4";

  break;

  case 'home':

  $cat="5";

  break;

  case 'gifts':

  $cat="6";

  break;

default:

$cat="1";

}

 

$cat is then used below as a unique number for my select query which happens.

 

but i need it to action as they switch between radio buttons automatically many thanks

Link to comment
https://forums.phpfreaks.com/topic/54429-solved-radio-buttons/
Share on other sites

PHP runs on the server - not the client (i.e. browser). Once the page is created and returned to the browser, you can no longer run PHP. You can either:

 

1. Query for all the possibile results for each radio button and then use DHTML to show/hide the appropriate content based upon the radio button selected

 

2. Have the page auto-submitted based upon a radio button selection using Javascript. Then do the query and resend the page with the updated content

 

3. Use AJAX - a combination of Javascript and PHP to externally run a PHP file to run the query and return the results and then display the results using DHTML.

 

The appropriate solution will most likely depend on how "big" your results are. If small enough I would go with option 1 as it would be the faster for the user and the least work for your server. For very big results I would go with option 2. Option 3 falls in the middle.

Link to comment
https://forums.phpfreaks.com/topic/54429-solved-radio-buttons/#findComment-269154
Share on other sites

 

 

 

 

   

try this


<?php
if( $_POST['submit_button']=='Submit' )
{
if(isset($_POST['group1']))
{


switch($_POST['group1']){
    case 'boot':
      $cat="1";
    break;
    case 'wildlife':
$cat="2";
  break;
    case 'gardening':
  $cat="3";
  break;
  case 'outlife':
  $cat="4";
  break;
  case 'home':
  $cat="5";
  break;
  case 'gifts':
  $cat="6";
  break;
default:
$cat="1";
}
}
else
{
echo "No radio button is selected";
//write code to display options again.
}
}
?>



[/Code]

Link to comment
https://forums.phpfreaks.com/topic/54429-solved-radio-buttons/#findComment-269159
Share on other sites

what have i done wrong the submit button isnt working keeps comming up with error 500

 

<html>

<head>

<title>Implementing Paging with next and prev</title>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<style type="text/css">

<!--

.style1 {

font-size: 16px;

font-weight: bold;

}

-->

</style></head>

 

<body>

<div align="center">PRESS IMAGE APPLICATION

 

<form action= <?php echo $_SERVER["../PHP_SELF"]; ?> method="POST">

</div>

  <label></label>

  <div align="center">

    <table width="226" border="1" align="left">

      <tr>

        <td width="144">Boot Room</td>

        <td width="66"><div align="center">

            <input name="group1" type="radio" value="boot" >

        </div></td>

      </tr>

      <tr>

        <td height="30">Wildlife</td>

        <td><div align="center">

          <input type="radio" name="group1" value="Wildlife">

        </div></td>

      </tr>

      <tr>

        <td>Gardening</td>

        <td><div align="center">

            <input type="radio" name="group1" value="Gardening">

        </div></td>

      </tr>

      <tr>

        <td>Outdoor Life</td>

        <td><div align="center">

            <input type="radio" name="group1" value="outlife">

        </div></td>

      </tr>

      <tr>

        <td>Home</td>

        <td><div align="center">

            <input type="radio" name="group1" value="home">

        </div></td>

      </tr>

      <tr>

        <td>Gifts </td>

        <td><div align="center">

            <input type="radio" name="group1" value="gifts">

        </div></td>

      </tr>

      <tr>

        <td><input type="text" name="textfield"></td>

        <td><div align="center">

          <p>

            <input type="submit" value="Submit" name="submit_button"></p>

 

        </div></td>

      </tr>

    </table>

 

</form>

 

 

<?php

 

if( $_POST['submit_button']=='Submit' )

{

if(isset($_POST['group1']))

{

switch($_POST['group1']){

    case 'boot':

    $cat="1";

    break;

    case 'wildlife':

$cat="2";

      break;

    case 'gardening':

  $cat="3";

  break;

  case 'outlife':

  $cat="4";

      break;

  case 'home':

  $cat="5";

  break;

    case 'gifts':

  $cat="6";

  break;

default:

$cat="1";}

}

else

{

 

echo "No radio button is selected";

//write code to display options again.

 

echo '<form action= <?php echo $_SERVER["../PHP_SELF"]; ?> method="POST">

</div>

  <label></label>

  <div align="center">

    <table width="226" border="1" align="left">

      <tr>

        <td width="144">Boot Room</td>

        <td width="66"><div align="center">

            <input name="group1" type="radio" value="boot" >

        </div></td>

      </tr>

      <tr>

        <td height="30">Wildlife</td>

        <td><div align="center">

          <input type="radio" name="group1" value="Wildlife">

        </div></td>

      </tr>

      <tr>

        <td>Gardening</td>

        <td><div align="center">

            <input type="radio" name="group1" value="Gardening">

        </div></td>

      </tr>

      <tr>

        <td>Outdoor Life</td>

        <td><div align="center">

            <input type="radio" name="group1" value="outlife">

        </div></td>

      </tr>

      <tr>

        <td>Home</td>

        <td><div align="center">

            <input type="radio" name="group1" value="home">

        </div></td>

      </tr>

      <tr>

        <td>Gifts </td>

        <td><div align="center">

            <input type="radio" name="group1" value="gifts">

        </div></td>

      </tr>

      <tr>

        <td><input type="text" name="textfield"></td>

        <td><div align="center">

          <p>

            <input type="submit" value="Submit" name="submit_button"></p>

 

        </div></td>

      </tr>

    </table>

 

</form>';

}

}

Link to comment
https://forums.phpfreaks.com/topic/54429-solved-radio-buttons/#findComment-269181
Share on other sites

Ok, here is a working prototype. However, you should make an additional change. If you want the values of 1, 2, 3,... instead of "boot", "wildlife", etc. Then change your values to that! No need to use that switch statement to transpose from one value to another! Persoanlly I would use the values you have instead of 1, 2, 3... for the category as they are more descriptive and you won't have to remember which number goes with what category.

 

I put the radio labels and values into an array to make the processing a bit easier. Hope it does not confuse the situation.

 

<html>
<head>
<title>Implementing Paging with next and prev</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
.style1 {
   font-size: 16px;
   font-weight: bold;
}
-->
</style></head>

<body>

<div align="center">

PRESS IMAGE APPLICATION

<form action= <?php echo $_SERVER[php_SELF]; ?> method="POST">

    <table width="226" border="1" align="center">

<?php

$radioList = array("Boot Room"=>"boot", "Wildlife"=>"Wildlife", "Gardening"=>"Gardening",
                   "Outdoor Life"=>"outlife", "Home"=>"home", "Gifts"=>"gifts");

foreach ($radioList as $label=>$value) {

    $selected = ($value==$_POST[group1])?' checked':'';
    echo "      <tr>\n";
    echo "        <td height=\"30\">$label</td>\n";
    echo "        <td><div align=\"center\">\n";
    echo "          <input type=\"radio\" name=\"group1\" value=\"$value\"$selected>\n";
    echo "        </div></td>\n";
    echo "      </tr>\n";
}

?>
      <tr>
        <td><input type="text" name="textfield"></td>
        <td><div align="center">
           <p>
             <input type="submit" value="Submit" name="submit_button"></p>

        </div></td>
      </tr>
    </table>

</form>

<?php

if( $_POST['submit_button']=='Submit' )
{
   switch($_POST['group1']){
      case 'wildlife':
          $cat="2";
           break;
      case 'gardening':
          $cat="3";
          break;
      case 'outlife':
          $cat="4";
          break; 
      case 'home':
          $cat="5";
          break;
      case 'gifts':
          $cat="6";
          break;
      case 'boot':
      default:
          $cat="1";
   } //End switch

   echo "The category is: $cat ($_POST[group1])";

}
?>
</div>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/54429-solved-radio-buttons/#findComment-269217
Share on other sites

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.