0

I'm trying to write some code that will determine if no radio buttons are selected on a form

the form consists of many fields but have just included the radio buttons in form below

<form action="myPHPPage.php" method="post"> 

Value 1 <input type="radio" name="basic" value="myValue1"> 
Value 2 <input type="radio" name="silver" value="myValue2">
Value 3 <input type="radio" name="gold" value="myValue3">

<input type="submit" name="send" value="Submit">

then in the myPHPPage.php page I have something like below to assign the POST value to a variable:

if(!isset($_POST['basic'])) {
 $var = $_POST['basic'];};
 $someValue = $var;
}

But I want some code like: If no radio buttons selected $var = $someValue

Ritesh Chandora
  • 8,382
  • 5
  • 21
  • 38
wilky
  • 83
  • 1
  • 10

8 Answers8

1

You need to bind all-radio buttons in a group like

Value 1 <input type="radio" name="basic" value="basic"> 
Value 2 <input type="radio" name="basic" value="silver">
Value 3 <input type="radio" name="basic" value="gold">

Then in PHP

<?php
   if(isset($_POST['basic'])) { //remove ! from condition
      $var = $_POST['basic'];};
      echo $someValue = $var;
   }
?>
Rohan Kumar
  • 40,431
  • 11
  • 76
  • 106
0

TRY THIS....you should have all radio button same NAME

<form action="myPHPPage.php" method="post"> 

    Value 1 <input type="radio" name="basic" value="myValue1"> 
    Value 2 <input type="radio" name="basic" value="myValue2">
    Value 3 <input type="radio" name="basic" value="myValue3">

    <input type="submit" name="send" value="Submit">

</form>

here your php code on server after the submit is press

<?php
    if(isset($_POST['send'])) { //change index to submit button name

         if($_POST['basic'] == ''){ // if no button is selected then

         $var = $someValue;

         }//if ends here

        else
         { //if any of the radio is selected "if you don't want else so remove that"

         $var = $_POST['basic'];

        }//else ends here

    }//isset ends here

?>

HaRsH
  • 313
  • 3
  • 15
0

Try this:

<?php

// Handle Post
if (isset($_POST['send']))
{
    // Get Post Values
    $basic  = isset($_POST['basic'])  ? $_POST['basic']  : '';
    $silver = isset($_POST['silver']) ? $_POST['silver'] : '';
    $gold   = isset($_POST['gold'])   ? $_POST['gold']   : '';

    // Atleast one is selected
    if (!empty($basic) || !empty($silver) || !empty($gold))
    {
        echo 'You have made atleast one selection';
    }
    else
    {
        echo 'You have not made any selection.';
    }
}

?>

<form action="" method="post"> 
Value 1 <input type="radio" name="basic" value="myValue1"> 
Value 2 <input type="radio" name="silver" value="myValue2">
Value 3 <input type="radio" name="gold" value="myValue3">
<input type="submit" name="send" value="Submit">
</form>
Latheesan
  • 23,247
  • 32
  • 107
  • 201
0

If you are using radio, all the names of input radio for that particular option should be same.

Value 1 <input type="radio" name="basic" value="basic"> 
Value 2 <input type="radio" name="basic" value="silver">
Value 3 <input type="radio" name="basic" value="gold">

<?php
if(isset($_POST['basic'])) 
{
  $var = $_POST['basic'];
}
?>
Parag Tyagi
  • 8,780
  • 3
  • 42
  • 47
0

The first thing you have to know about radio button is that if they are reflecting the same attribute, they should have a same name. Thus your form have to be changed like this.

  <form action="myPHPPage.php" method="post"> 

Value 1 <input type="radio" name="radioName" value="basic"> 
Value 2 <input type="radio" name="radioName" value="silver">
Value 3 <input type="radio" name="radioName" value="gold">

<input type="submit" name="send" value="Submit">

Then you want to assign a value if no radio button is selected. You can do the following:

$var="";
$someValue="abc";
    if(isset($_POST['radionName'])) {
 $var = $_POST['radionName'];// This means one radio button is selected, thus you have a value.
}
else{
$var=$someValue; // No radio button is selected, thus you can assign a default desired value.
}
user1577291
  • 366
  • 1
  • 3
  • 15
0
    <form action="myPHPPage.php" method="post"> 

if buttons are optional please make a group of radio button with name attribute value.

 Value 1 <input type="radio" name="basic" value="myValue1"> 
 Value 2 <input type="radio" name="basic" value="myValue2">
 Value 3 <input type="radio" name="basic" value="myValue3">

 <input type="submit" name="send" value="Submit">

 Then use a php condition here.
 <?php


if(isset($_POST['basic'])) {
 $var = $_POST['basic'];};
 $someValue = $var;
}
MayUr
  • 67
  • 1
  • 13
0

You can check all $_POST elements,than can compare name and their values.

foreach($_POST as $key=>$value)
{
  echo "$key=$value";
}

PHP: Possible to automatically get all POSTed data?

Community
  • 1
  • 1
ridvanzoro
  • 646
  • 1
  • 10
  • 25
-1
<form action="myPHPPage.php" method="post"> 

   Value 1 <input type="radio" name="basic" value="myValue1"> 
   Value 2 <input type="radio" name="basic" value="myValue2">
   Value 3 <input type="radio" name="basic" value="myValue3">

   <input type="submit" name="send" value="Submit">

   <?php


    if(!isset($_POST['basic'])) {
     $var = $_POST['basic'];};
     $someValue = $var;


     Please Try this one
MayUr
  • 67
  • 1
  • 13
  • 1
    You are doing it wrong. `if(!isset` stands for if the POST is not set. You are getting the value inside it's TRUE part, this will throw a WARNING. And the OP asked to set $var=$somevalue if the POST is not set. You are missing it. – Sarvap Praharanayuthan Apr 02 '14 at 08:04