Jump to: navigation, search

PHP HTML Radio

From w3cyberlearnings

Contents

PHP works with HTML Radio

Radio box is a single select option from multiple options.

Syntax Radio

<input type="radio" name="name" />

Example 1

<?php
if ($_POST) {
	print_r($_POST);
}
?>
<html>
<head>
<title>Radio</title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
	<input type="radio" name="group" value="group 1"/>Group 1<br/>
	<input type="radio" name="group" value="group 2"/>Group 2<br/>
	<input type="radio" name="group" value="group 3" checked="checked"/>Group 3<br/>
	<input type="radio" name="group" value="group 4"/>Group 4<br/>
	<input type="submit"/>
</form>
</body>
</html>

Output

Php form radio 1.png

Example 2

<?php
$msg_infor = "";
$msg_error = "";

if (!empty($_POST['submit']) && isset($_POST['submit'])) {
	if (!empty($_POST['name'])) {
		$msg_infor .= '<li>' . $_POST['name'] . '</li>';
	} else {
		$msg_error .= '<li>User name can not be blank</li>';
	}

	if (!empty($_POST['age'])) {
		$msg_infor .= '<li>' . $_POST['age'] . '</li>';
	} else {
		$msg_error .= '<li>Age can not be empty</li>';
	}

	if (!empty($_POST['role']) && isset($_POST['role'])) {
		$msg_infor .= '<li>' . $_POST['role'] . '</li>';
	} else {
		$msg_error .= '<li>Select your role</li>';
	}

	if (!empty($_POST['gender']) && isset($_POST['gender'])) {
		$msg_infor .= '<li>' . $_POST['gender'] . '</li>';
	} else {
		$msg_error .= '<li>Gender is required</li>';
	}
}

if ($msg_error == "") {
	echo '<ul>';
	echo $msg_infor;
	echo '</ul>';
} else {
	echo '<ul>';
	echo $msg_error;
	echo '</ul>';
}
?>
<html>
<head>
<title>Request</title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<table border="0">
	<tr>
		<td>
			<label id="name">Name:</label>
		</td>
		<td>
			<input type="text" name="name" size="20" maxlength="20"/>
		</td>
	</tr>
	<tr>
		<td>
			<label id="age">Age:</label>
		</td>
		<td>
			<input type="text" name="age" size="2" maxlength="2"/>
		</td>
	</tr>
	<tr>
		<td>
			<label id="gender">Gender:</label>
		</td>
		<td>
			<input type="radio" name="gender" value="male"/>Male           
			<input type="radio" name="gender" value="female"/>Female
		</td>
	</tr>
	<tr>
		<td valign="top">
			<label id="role">Role:</label>
		</td>
		<td>
			<input type="radio" name="role" value="Programmer"/>Programmer<br/>           
			<input type="radio" name="role" value="It Support"/>IT Support<br/>
			<input type="radio" name="role" value="Manager"/>Manager<br/>           
			<input type="radio" name="role" value="System Administrator"/>System Administrator<br/>
		</td>
	</tr>
	<tr>
		<td colspan="2">
			<input type="submit" name="submit" value="Request"/>
		</td>
	</tr>
</table>
</form>
</body>
</html>

Output

Php form radio 2.png


Related Links


  1. HTML POST
  2. HTML GET
  3. HTML Textarea
  1. HTML Hidden Field
  2. HiddenField Multiple Pages
  3. HTML Password
  1. HTML Textbox
  2. HTML Checkbox
  3. HTML Radio
  1. Drop down list
  2. Pass value uses URL
  3. Pass value uses URL 2.
  4. Multiple Submit Buttons
Navigation
Web
SQL
MISC
References