There is a big confusion in some freshers that how to take values associated with radio button as well as check box in PHP. Here is the piece of code that helps to do so.
cricket <input type='radio' name='radio' id='radio1' value='cricket' />
football <input type='radio' name='radio' id='radio2' value='football' />
tennis <input type='radio' name='radio' id='radio3' value='tennis' />
Don't forget to add values attribute in input tag
Now lets look how to take values in php
<?php
$val = $_POST['radio'];
echo $val;
?>
this code will print the opted input radio.
similarly for check box
cricket <input type='checkbox' name='check[]' id='check1' value='cricket' />
football <input type='checkbox' name='check[]' id='check2' value='football' />
tennis <input type='checkbox' name='check[]' id='check3' value='tennis' />
here also we can have above piece of code. But if you choose cricket and football you will get it as an array
so you can have
$var = implode(",",$_POST['check']);
echo $var
this will print cricket,football
cricket <input type='radio' name='radio' id='radio1' value='cricket' />
football <input type='radio' name='radio' id='radio2' value='football' />
tennis <input type='radio' name='radio' id='radio3' value='tennis' />
Don't forget to add values attribute in input tag
Now lets look how to take values in php
<?php
$val = $_POST['radio'];
echo $val;
?>
this code will print the opted input radio.
similarly for check box
cricket <input type='checkbox' name='check[]' id='check1' value='cricket' />
football <input type='checkbox' name='check[]' id='check2' value='football' />
tennis <input type='checkbox' name='check[]' id='check3' value='tennis' />
here also we can have above piece of code. But if you choose cricket and football you will get it as an array
so you can have
$var = implode(",",$_POST['check']);
echo $var
this will print cricket,football
No comments:
Post a Comment