Sure, here’s an example PHP code that allows users to select their age, sex, and plan and echoes the selected values:
<html>
<head>
<title>Select Age, Sex, and Plan</title>
</head>
<body>
<form method="post">
<label for="age">Age:</label>
<select name="age" id="age">
<?php
for ($i=11; $i<=20; $i++) {
echo '<option value="'.$i.'">'.$i.'</option>';
}
?>
</select>
<br>
<label for="sex">Sex:</label>
<input type="radio" name="sex" id="male" value="male">
<label for="male">Male</label>
<input type="radio" name="sex" id="female" value="female">
<label for="female">Female</label>
<br>
<label for="plan">Plan:</label>
<select name="plan" id="plan">
<option value="1">Plan 1</option>
<option value="2">Plan 2</option>
<option value="3">Plan 3</option>
<option value="4">Plan 4</option>
</select>
<br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
if (isset($_POST['submit'])) {
$age = $_POST['age'];
$sex = $_POST['sex'];
$plan = $_POST['plan'];
echo "You selected age ".$age.", sex ".$sex.", and plan ".$plan.".";
}
?>
</body>
</html>
This code generates a form with a dropdown for age, radio buttons for sex, and a dropdown for plan. When the user submits the form, the PHP code checks if the form has been submitted (isset($_POST['submit'])
) and then retrieves the selected values from the form data ($_POST['age']
, $_POST['sex']
, and $_POST['plan']
) and echoes them back to the user.
add , each plan display value pairs by age.
To display the value pairs for each plan by age, you can modify the PHP code to create an array that stores the value pairs for each plan, and then use a loop to display the value pairs based on the selected age and plan. Here’s an example code:
<html>
<head>
<title>Select Age, Sex, and Plan</title>
</head>
<body>
<form method="post">
<label for="age">Age:</label>
<select name="age" id="age">
<?php
for ($i=11; $i<=20; $i++) {
echo '<option value="'.$i.'">'.$i.'</option>';
}
?>
</select>
<br>
<label for="sex">Sex:</label>
<input type="radio" name="sex" id="male" value="male">
<label for="male">Male</label>
<input type="radio" name="sex" id="female" value="female">
<label for="female">Female</label>
<br>
<label for="plan">Plan:</label>
<select name="plan" id="plan">
<option value="1">Plan 1</option>
<option value="2">Plan 2</option>
<option value="3">Plan 3</option>
<option value="4">Plan 4</option>
</select>
<br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
if (isset($_POST['submit'])) {
$age = $_POST['age'];
$sex = $_POST['sex'];
$plan = $_POST['plan'];
$value_pairs = array(
1 => array(
11 => 100,
12 => 110,
13 => 120,
14 => 130,
15 => 140,
16 => 150,
17 => 160,
18 => 170,
19 => 180,
20 => 190
),
2 => array(
11 => 200,
12 => 220,
13 => 240,
14 => 260,
15 => 280,
16 => 300,
17 => 320,
18 => 340,
19 => 360,
20 => 380
),
3 => array(
11 => 300,
12 => 330,
13 => 360,
14 => 390,
15 => 420,
16 => 450,
17 => 480,
18 => 510,
19 => 540,
20 => 570
),
4 => array(
11 => 400,
12 => 440,
13 => 480,
14 => 520,
15 => 560,
16 => 600,
17 => 640,
18 => 680,
19 => 720,
20 => 760
)
);
echo "You selected age ".$age.", sex ".$sex.", and plan ".$plan.".<br>";
echo "The value pairs for plan ".$plan." and age ".$age." are:<br>";
foreach ($value_pairs[$plan] as $key => $value) {
if ($key == $age) {
echo "Age ".$key.": ".$value."<br>";
}
}
?>
</body>
</html>
สั่งให้พิมพ์โค๊ดต่อ
continuously complete above code.
หรือ
Continuing from the previous code, here's how you can display the value pairs for the selected plan and age:
This code uses a loop to iterate through the array of value pairs for the selected plan, and checks if the age matches the selected age. If there’s a match, it displays the age and value pair for that age.
You can copy this code into a file on your web server, save it as a .php file, and open it in your web browser to test it out.
ใช้ได้
<html>
<head>
<title>Select Age, Sex, and Plan</title>
</head>
<body>
<form method="post">
<label for="age">Age:</label>
<select name="age" id="age">
<?php
for ($i=11; $i<=20; $i++) {
echo '<option value="'.$i.'">'.$i.'</option>';
}
?>
</select>
<br>
<label for="sex">Sex:</label>
<input type="radio" name="sex" id="male" value="male">
<label for="male">Male</label>
<input type="radio" name="sex" id="female" value="female">
<label for="female">Female</label>
<br>
<label for="plan">Plan:</label>
<select name="plan" id="plan">
<option value="1">Plan 1</option>
<option value="2">Plan 2</option>
<option value="3">Plan 3</option>
<option value="4">Plan 4</option>
</select>
<br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
if (isset($_POST['submit'])) {
$age = $_POST['age'];
$sex = $_POST['sex'];
$plan = $_POST['plan'];
$value_pairs = array(
1 => array(
11 => 100,
12 => 110,
13 => 120,
14 => 130,
15 => 140,
16 => 150,
17 => 160,
18 => 170,
19 => 180,
20 => 190
),
2 => array(
11 => 200,
12 => 220,
13 => 240,
14 => 260,
15 => 280,
16 => 300,
17 => 320,
18 => 340,
19 => 360,
20 => 380
),
3 => array(
11 => 300,
12 => 330,
13 => 360,
14 => 390,
15 => 420,
16 => 450,
17 => 480,
18 => 510,
19 => 540,
20 => 570
),
4 => array(
11 => 400,
12 => 440,
13 => 480,
14 => 520,
15 => 560,
16 => 600,
17 => 640,
18 => 680,
19 => 720,
20 => 760
)
);
echo "You selected age ".$age.", sex ".$sex.", and plan ".$plan.".<br>";
echo "The value pairs for plan ".$plan." and age ".$age." are:<br>";
foreach ($value_pairs[$plan] as $key => $value) {
if ($key == $age) {
echo "Age ".$key.": ".$value."<br>";
}
}
}
?>
</body>
modified code that allows the user to choose the sex and display different value pairs for each plan based on the sex:
รูปแบบการ pairs
$value_pairs = array(
'male' => array(
1 => array(
11 => 100,
12 => 110,
),
2 => array(
11 => 200,
12 => 220,
),
'female' => array(
1 => array(
11 => 50,
12 => 55,
--------------------------------
$value_pairs = array(
1 => array(
'male' => array(
11 => 100,
12 => 110,
),
'female' => array(
11 => 90,
12 => 99,
)
),
2 => array(
'male' => array(
11 => 200,
12 => 220,
),
'female' => array(
11 => 190,
12 => 209,
)
),
-------------------------------- แบบนี้ดูง่าย
$value_pairs = array(
1 => array(
11 => array('male' => 100, 'female' => 90),
12 => array('male' => 110, 'female' => 100),
),
2 => array(
11 => array('male' => 200, 'female' => 190),
12 => array('male' => 220, 'female' => 200),
),
-------------------------------
$value_pairs = array(
1 => array(
11 => array(
'male' => 100,
'female' => 90
),
12 => array(
'male' => 110,
'female' => 100
)
),
2 => array(
11 => array(
'male' => 200,
'female' => 180
),
12 => array(
'male' => 220,
'female' => 200
)
),
3 => array(
11 => array(
'male' => 300,
'female' => 270
),
12 => array(
'male' => 330,
'female' => 300
)
),
4 => array(
11 => array(
'male' => 400,
'female' => 360
),
12 => array(
'male' => 440,
'female' => 400
)
)
);
echo "You selected age ".$age.", sex ".$sex.", and plan ".$plan.".<br>";
echo "The value pairs for plan ".$plan." and age ".$age." are:<br>";
echo "For ".$sex.", the value is ".$value_pairs[$plan][$age][$sex].".<br>";
}
?>