Reading Data in a form: using radio buttons, lesson -5-…

September 25
S

orry for not posting anything the last few weeks, I have good reasons; anyway, I will fix that. In the next few days I will post what I should posted earlier. Now, let’s start. I will talk today about radio buttons, radio buttons are usually used when we want the user to select only one item from two choices.

 

Step *1: open xammp control panal, open your browser and open the editor. In htdocs folder create another folder and name it (radiobutton). Inside this folder you will save the files we will create in this lesson. In this folder we will have two files radiobutton.html and phpradiobutton.php.

 

Step *2: using the editor, create an HTML file, name it radiobutton.html, type this code in it and save.


<html>

<head>

<title>entering data with radio button</title>

</head>

<body>

<h1>

entering data with radio button

</h1>

<form method="post" action="phpradiobutton.php">

do like ice cream?

<input type="radio" value="yes" />

yes

<input name="radios" value="no" />

no

<br/>

<br/>

<input type="submit"  value="send" />

</form>

</body>

</html>

As we used to do before, we should create a form using the “form” tag. Use “method” to select the way of sending the data and “action” to add the path of PHP file. Then we add two inputs tag, in the first input tag, in “type” property I will type radio, in “name” property I will name it readios, in “value” property  I will type “yes”, to give the first radio button the value yes.

 

in the second input tag, the same, in “type” property I will type radio, in “name” property I will name it readios, in “value” property  I will type “no”, to give the second radio button the value no. and of course follow each radio button with the value it represents. And finally, add the submit button as we used to do before.

 

Step *3: Create a PHP file and name it phpradiobutton.php. Type this code in it and save.


<html>

<head>

<title>  reading data from radio buttons</title>

</head>

<body>

<h1>

reading data from radio buttons

</h1>

you selected:

<?php

if (isset ($_REQUEST["radios"])) {

echo $_REQUEST["radios"];

}

else {

echo "no radio was selected.<br/>";

}

?>

</body>

</html>

This step shows us how we could reading data in a form, the data we sent by the form we created above and using radio buttons.

 

As in check box we should check first if the user clicks any of the radio buttons by using the function (isset). After that we should type the value in the radio button. And if the value of (isset) function is false, that’s mean the user didn’t select any of the radio button. We can use “else” of “if statement” to indicates that the user did not select any of the radio buttons.

 

That’s all for this lesson. Wait for the next lesson, I will talk about  list boxes. seeyas.

 


StumbleUpon.com Add us to your digg favorites Add us to your delicious favorites add us to your facebook favorites follow us on twitter Add us to your technorati favorites Add us to your wordpress favorites Add us to your reddit favorites Add us to livejournal favorites

Leave a Reply