确定是否没有使用PHP选择单选按钮

时间:2014-04-02 07:55:40

标签: php

我正在尝试编写一些代码来确定表单上是否没有选择单选按钮

表单由许多字段组成,但只包含下面表单中的单选按钮

<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">

然后在myPHPPage.php页面中,我有类似下面的内容,将POST值分配给变量:

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

但是我想要一些代码:如果没有选择单选按钮$ var = $ someValue

8 个答案:

答案 0 :(得分:1)

您需要在all-radio buttons中绑定group

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

<?php
   if(isset($_POST['basic'])) { //remove ! from condition
      $var = $_POST['basic'];};
      echo $someValue = $var;
   }
?>

答案 1 :(得分:0)

尝试这个....你应该让所有的单选按钮名称相同

<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>

在提交按下

后,您的服务器上的PHP代码
<?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

&GT;

答案 2 :(得分:0)

试试这个:

<?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>

答案 3 :(得分:0)

如果您使用的是广播,则该特定选项的input radio的所有名称都应相同。

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'];
}
?>

答案 4 :(得分:0)

关于单选按钮,您必须要了解的第一件事是,如果它们反映相同的属性,它们应该具有相同的名称。 因此,你的表格必须像这样改变。

  <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">

如果没有选择单选按钮,则要分配值。 您可以执行以下操作:

$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.
}

答案 5 :(得分:0)

    <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">

 Then use a php condition here.
 <?php


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

答案 6 :(得分:0)

您可以检查所有$ _POST元素,而不是比较名称及其值。

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

PHP: Possible to automatically get all POSTed data?

答案 7 :(得分:-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