user1784592
user1784592

Reputation: 129

How to POST radio button value in php

I am doing project using Zend framework. here within the form I add field using radio buttons. after the form post. it doesn't send that radio button value(but other fields (eg -text field can post)). this is my code in the view.

<form class="custom" method="post">

   <?php
     foreach ($answers as $answer) {
         echo '<input name="q_answer" value="'.$answer.'" type="radio" >'.$answer;
     }
   ?>
    <input class="small secondary button" type="submit" value="  Ok  ">
</form>

this is my code within controller

if($request->isPost()){
   $ans = $_POST['q_answer'];
}

so when I post the form. it gives Undefined index: q_answer error. what is the wrong. please help me.( within the controller I print posted values using var_dump but 'q_answer' value not available)

Upvotes: 0

Views: 450

Answers (1)

Marcin Orlowski
Marcin Orlowski

Reputation: 75635

If no option is selected this field is not appear in $_POST. So you should first check with isset() if it is present and the try to process. And while you are using ZF, you should use getPost()instead of digging directly in $_POST:

$ans = getPost( 'q_answer', 'default-value-if-no-element-is-found' );

Upvotes: 1

Related Questions