input - Radio buttons in PHP not working properly -


i have code:

<?php  if (isset($_post['ans'])) {     $answer = $_post['ans'];      if ($answer == "ans1") {                   echo 'correct';           }     else {         echo 'incorrect';     } } ?> <form action="example.php" method="post">   answer 1 <input type="radio" name="ans" value="ans1" /><br />   answer 2 <input type="radio" name="ans" value="ans2"  /><br />   answer 3 <input type="radio" name="ans" value="ans3"  /><br />   answer 4 <input type="radio" name="ans" value="ans4"  /><br />   <input type="submit" value="submit" /> </form> 

when select wrong button, tells me incorrect , when select right 1 tells me correct. problem if no button selected nothing happens. not echo incorrect or correct. if answer nothing should incorrect.

try add else if if button selected empty wil pass it.

    <?php if (isset($_post['ans'])) {     $answer = $_post['ans'];      if ($answer == "ans1") {                   echo 'correct';           }     else if(empty($answer)){         echo 'select button';     }     else {         echo 'incorrect';     }  } ?> 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -