3

Hi I am pretty new to PHP. I am trying to get the selected value from my radio button in PHP but I am unable to get the selected value. I have populated the values by connecting to my DB (MySQL) but I am unable to get the assigned value from the radio button. It always escapes the if condition and says "No Value Selected" and I am not able to assign the value and save it to my DB

Appreciate your help.

My index.php is as follows `

<?php
session_start();
$_SESSION['timein']=  time();
?>

<?php
include("config.php");
        $conn = mysqli_connect($dbHost, $dbuser, $dbpassword, $dbDatabase);
        $query_salutation_type='SELECT salutation_description FROM tbl_salutation;';
        $select_salutation_type=mysqli_query($conn, $query_salutation_type);

        while($row1 = mysqli_fetch_array($select_salutation_type))
        {
            echo '<input type="radio" name="salutation_description" value="'.$row1[0].'"/>'.$row1[0];

        }
?>
<html>
<body>        
 <form method="post" action="capture_data.php" >    
       <input type="submit" name="submit" value="Submit"/>
</form> 
    </body>
 </html>

`

My capture_data.php is as follows

  <?php 


if(isset($_POST['submit']))
{

if(isset($_POST['salutation_description']))
{
    $selected_val = $_POST['salutation_description'];
    echo "You have selected :" .$selected_val; 
}
 else 
{
    echo 'No Value Selected';
}
}
?>

index.php

1
  • The radio buttons aren't inside the form. Commented Jan 28, 2016 at 5:45

4 Answers 4

2

The form only sends inputs that are between <form> and </form>. You're echoing the radio buttons before the <form>.

<?php
session_start();
$_SESSION['timein']=  time();
?>
<html>
<body>        
 <form method="post" action="capture_data.php" >    

<?php
include("config.php");
$conn = mysqli_connect($dbHost, $dbuser, $dbpassword, $dbDatabase);
$query_salutation_type='SELECT salutation_description FROM tbl_salutation;';
$select_salutation_type=mysqli_query($conn, $query_salutation_type);

while($row1 = mysqli_fetch_array($select_salutation_type))
{
    echo '<input type="radio" name="salutation_description" value="'.$row1[0].'"/>'.$row1[0];

}
?>
       <input type="submit" name="submit" value="Submit"/>
</form> 
</body>
</html>
Sign up to request clarification or add additional context in comments.

Comments

2
<?php
session_start();
$_SESSION['timein']=  time();
?>

<?php
include("config.php");
        $conn = mysqli_connect($dbHost, $dbuser, $dbpassword, $dbDatabase);
        $query_salutation_type='SELECT salutation_description FROM tbl_salutation;';
        $select_salutation_type=mysqli_query($conn, $query_salutation_type);
$radioHtml = "";
        while($row1 = mysqli_fetch_array($select_salutation_type))
        {
            $radioHtml.= '<input type="radio" name="salutation_description" value="'.$row1[0].'"/>'.$row1[0];

        }
?>
<html>
<body>        
 <form method="post" action="capture_data.php" >
       <?php echo $radioHtml; ?>
       <input type="submit" name="submit" value="Submit"/>
</form> 
    </body>
 </html>

keep radio buttons within your form

Comments

2

You need to use your radio button inside the <form> tag as:

Modified Code:

<?php
session_start();
$_SESSION['timein']=  time();
?>
<html>
<body>        
<form method="post" action="capture_data.php" >  

<?php
include("config.php");
$conn = mysqli_connect($dbHost, $dbuser, $dbpassword, $dbDatabase);
$query_salutation_type='SELECT salutation_description FROM tbl_salutation;';
$select_salutation_type=mysqli_query($conn, $query_salutation_type);

while($row1 = mysqli_fetch_array($select_salutation_type))
{
echo '<input type="radio" name="salutation_description" value="'.$row1[0].'"/>'.$row1[0];

}
?>

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

</form> 
</body>
</html>

Comments

0

Hi Lad try to relocate the while loop inside the form tag of your HTML. Like the example below. :D

<?php
session_start();
$_SESSION['timein']=  time();
?>

<?php
include("config.php");
        $conn = mysqli_connect($dbHost, $dbuser, $dbpassword, $dbDatabase);
        $query_salutation_type='SELECT salutation_description FROM tbl_salutation;';
        $select_salutation_type=mysqli_query($conn, $query_salutation_type);
?>
<html>
<body>        
 <form method="post" action="capture_data.php" >    
<?php
while($row1 = mysqli_fetch_array($select_salutation_type))
        {
            echo '<input type="radio" name="salutation_description" value="'.$row1[0].'"/>'.$row1[0];

        }
?>
       <input type="submit" name="submit" value="Submit"/>

</form> 
    </body>
 </html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.