用PHP编写一个程序,从表单一个输入框中多次输入年龄,年龄最大值为 100,最小值为 0,然后按 年龄大小排序
时间: 2024-03-02 07:54:16 浏览: 37
好的,以下是一个简单的 PHP 程序,可以实现你要求的功能:
```php
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// 获取表单中输入的年龄,以逗号分隔
$ages = explode(',', $_POST['ages']);
// 将字符串数组转换为整数数组
$ages = array_map('intval', $ages);
// 筛选出符合年龄范围的数字
$ages = array_filter($ages, function ($age) {
return $age >= 0 && $age <= 100;
});
// 按照年龄大小排序
sort($ages);
// 输出结果
echo implode(',', $ages);
}
?>
<form method="post">
<label>输入年龄(多个以逗号分隔):</label>
<input type="text" name="ages">
<button type="submit">排序</button>
</form>
```
这个程序会从表单中获取输入的年龄,以逗号分隔。然后将字符串数组转换为整数数组,并筛选出符合年龄范围的数字。最后按照年龄大小排序,并将结果输出。
相关问题
用PHP编写一个程序,从表单多次输入年龄,年龄最大值为 100,最小值为 0,然后按 年龄大小排序,
好的,以下是一个简单的 PHP 程序,可以实现您所需的功能:
```php
<!DOCTYPE html>
<html>
<head>
<title>年龄排序程序</title>
</head>
<body>
<h2>请输入年龄</h2>
<form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<input type="number" name="age[]" min="0" max="100"><br>
<input type="number" name="age[]" min="0" max="100"><br>
<input type="number" name="age[]" min="0" max="100"><br>
<input type="number" name="age[]" min="0" max="100"><br>
<input type="number" name="age[]" min="0" max="100"><br>
<input type="submit" value="排序">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$ages = $_POST["age"];
sort($ages);
echo "<h2>排序后的年龄</h2>";
foreach ($ages as $age) {
echo $age . "<br>";
}
}
?>
</body>
</html>
```
这个程序使用 HTML 表单让用户多次输入年龄,然后通过 PHP 的 sort() 函数将输入的年龄排序,并将排序后的结果输出到页面上。注意,我们在 HTML 表单中设置了每个输入框的最大值和最小值,以确保输入的年龄在合理的范围内。
希望这个程序能够满足您的需求。
帮我用js写一个小程序阈值输入框
以下是一个基于JavaScript的小程序阈值输入框示例代码,你可以按照自己的需求进行修改和适配:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>阈值输入框</title>
</head>
<body>
<form>
<label for="threshold">阈值:</label>
<input type="number" id="threshold" name="threshold" min="0" max="1000">
<input type="submit" value="提交">
</form>
<script>
const thresholdInput = document.getElementById("threshold");
const form = document.querySelector("form");
form.addEventListener("submit", event => {
event.preventDefault();
const threshold = thresholdInput.value;
// 在这里可以对阈值进行处理和使用
console.log(`阈值为:${threshold}`);
});
</script>
</body>
</html>
```
该示例代码使用了HTML和JavaScript来创建一个阈值输入框,并在提交表单时获取输入的阈值。阈值输入框的类型为number,最小值为0,最大值为1000。在提交表单时,阻止默认行为,获取阈值输入框的值,并在控制台中打印出来。
需要注意的是,该示例代码仅供参考,实际应用中可能需要根据具体情况进行修改和优化。