Lykas
Lykas

Reputation: 117

How to check which radio button is clicked by PHP $_POST?

I am trying to know which radio button is clicked by PHP in a form with $_POST['...']. This is my form:

<form action="" method="post" enctype="multipart/form-data">
   <div class="input-group">
        <span class="input-group-addon">Add:</span>
        <input type='radio' name='add1' id='program' value='program' onchange="hideCollege()" checked>  Program Intern</input>
        <input type='radio' name='add1' id='department_coop' value='department_coop' onchange="showCollegelist()">  Department Cooperation</input>
        <input type='radio' name='add1' id='foreigner' value='foreigner' onchange="hideCollege()">  Foreigner Area</input>
    </div>
</form>

And I am trying to receive the values of the three radio buttons by:

if(isset($_POST['program'])){
    $program = 1;
}
else if(isset($_POST['foreigner'])){
    $foreigner = 1;
}
else if(isset($_POST['department_coop'])){
    $coop = 1;
    $college = $_POST['college'];
    $department = $_POST['department'];
}

But it seems that no if statements are true, and turns out that no variables are assigned value. Does anyone know how to get to what I aim to do? Thanks a lot in advance.

Upvotes: 0

Views: 44

Answers (3)

VinothRaja
VinothRaja

Reputation: 1413

Try this below section . Post request value should processed on form name attributes. So must check the conditional form post name value.

if(isset($_POST['add1']) && $_POST['add1']=='program' ){
    $program = 1;
}
else if(isset($_POST['add1']) && $_POST['add1']=='foreigner' ){
     $foreigner = 1;
}
else if(isset($_POST['add1']) && $_POST['add1']=='department_coop' ){
     $coop = 1;
    $college = $_POST['college'];
    $department = $_POST['department'];
}

Upvotes: 2

Lithilion
Lithilion

Reputation: 1129

If you access $_POST['add1'] check for the value as it should either be program, department_coop or foreigner (in this case)

Upvotes: 0

Talk2Nit
Talk2Nit

Reputation: 1135

You should check the values of the radio button on the basis of name attribute.

if(isset($_POST['add1']) && $_POST['add1']=='program'){
    $program = 1;
}
else if(isset($_POST['add1']) && $_POST['add1']=='foreigner'){
    $foreigner = 1;
}
else if(isset($_POST['add1']) && $_POST['add1']=='department_coop'){
    $coop = 1;
    $college = $_POST['college'];
    $department = $_POST['department'];
}

Then you will get the checked value.

Upvotes: 1

Related Questions