PHP Form Part-6 - How to Deals with Text Boxs


 

PHP Form Part-6 - How to Deals with Text Boxs

In the previous part of HTML FORM tutorial, we learned how to deal with text box. In this part of the tutorial we will learn how to deal with radio buttons. A radio button does not give the choice to the users to select multiple option. It restrict the user to having only one choice. We are going to use the radio button in the previous tutorial coding.

In the previous part of HTML FORM tutorial, we learned how to deal with text box. In this part of the tutorial we will learn how to deal with radio buttons. A radio button does not give the choice to the users to select multiple option. It restrict the user to having only one choice. We are going to use the radio button in the previous tutorial coding.

Topic : HTML FORM - How to Deal with Text Boxs in PHP

Part - 6

In the previous part of HTML FORM tutorial, we learned how to deal with text box.

 In this part of the tutorial we will learn how to deal with radio buttons. A radio button does not give the choice to the users to select multiple option. It restrict the user to having only one choice. We are going to use the radio button in the previous tutorial coding.

Let's see the code first :

Page1: loginpage.php

<html>

<head>

<title>Login Page</title>

</head>

<body>

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

Username : <input type="text" name="userid" value="" size="30" /><br/><br/>

Password : <input type="password" name="pass" value="" size="30" /><br/><br/>

Gender : <input type="radio" name="gender" value="male" />&nbsp;Male

<input type="radio" name="gender" value="male" />&nbsp;Female<br/><br/>

<input type="submit" name="submit" value="Submit Form" />

</form>

</body>

</html>

The text in red color and bold indicates the radio button coding. Again, if we want to access the value of a radio button with the help of PHP Code,  we need to get the name attribute of the form.

Now, the next step is to access the value of the radio button in the second page and print the value of selected radio button.

Second page should be written like this :

Page 2: detail_page.php

<?php

if(isset($_POST['submit'])){

    $userid = $_POST['userid'];

    $password = $_POST['pass'];

   $selected_radio = $_POST['gender'];

    if($userid != '' && $password != '' && $selected_radio != 'checked'){

0

        echo "Welcome back, ".$userid."!<br/>";

       echo "You are ".$selected_radio."!<br/>";

    }

1

    else{

        echo "You are not a member of this site!";

           }

2

      }

?>

The output of the above code is : Welcome back, [email protected]
                                                        You are male!

3

In the above code, the letters in red and bold are indicating the radio button coding. We  took variable and assigned the value of radio button. For example :

 $selected_radio = $_POST['gender'];

Then, we checked the condition. If the variable is not equals to checked then it should go inside and print the selected one otherwise show the error message.

4

if($selected_radio != 'checked'){

 In the next step, we print the value of the variable.

  echo "You are ".$selected_radio."!<br/>";

5

This code print the value of the selected radio button.

In the next part of this tutorial, we will learn how to access the value of multiple checkbox.

Ads