Hi
How can I pass value of my radio buttons to my form.php code?

I will be very thankfull for your answer.

<form action="form1.php" method="post">
First name:  <input type="text" name="fname"><br><br>
Last name:  <input type="text" name="lname"><br><br>

<h3>What is the best programming language to learn for hacking</h3>
Python
<input type="radio" name="rb_python" value="Python"><br>
Perl
<input type="radio" name="rb_perl" value="Perl"><br>
Java
<input  type="radio" name="rb_java" value="java"><br>

<input type="submit" name="submit" value="Submit"><br>
</form>
Member Avatar for diafol

The name attr should be the same for each.

Ok thanks
But what about php coden?

<input type="radio" name="hack_language" value="Python"><br>
Perl
<input type="radio" name="hack_language" value="Perl"><br>
Java
<input  type="radio" name="hack_language" value="java"><br>

I think my php code is wrong.

#form.php
$python_status = "unchcked";
$perl_status = "unchcked";
$java_status = "unchcked"; 
if(isset($_POST["submit"])){
    $selected_radio = $_POST["hack_language"];
    if ($selected_radio == "Python"){
        $python_status = "checked";

    }
    elseif($selected_radio == "Perl"){
        $perl_status = "checked";
        echo "You select" .
    }
    else
    {
        if($selected_radio == "java")
        $java_status ="checked";
    }


}

In your Action page just print Value using

$_POST('hack_language')

<?php echo $_POST('hack_language'); ?>
Member Avatar for diafol

Give radio options a default checked in the form if possible, to ensure a value if always sent.

$selected_radio = $_POST["hack_language"];
echo "You selected $selected_radio";

Thanks so much
what about else i got error?

 else
{
if($selected_radio == "java")
$java_status ="checked";
}
}
Member Avatar for diafol

Our code means you don't need all those conditionals...

if(isset($_POST["submit"])){
    $selected_radio = $_POST["hack_language"];
    echo "You selected $selected_radio";
}

That's it - no need for anything else. Unless you want to do soemthing else?

Thank sir

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.