我将以下下拉列表作为 HTML 表单的一部分:
Print Options:<br/><select name="printopts">
<option selected=""></option>
<option value="Print Longest Answer">Print Longest Answer</option>
<option value="Print Sorted Order">Print Sorted Order</option>
</select>
如果用户选择“打印最长答案”或 strcmp($str1, $str2) 如果用户选择“打印排序顺序”,我需要我的 php 脚本来执行 strlen() 函数。
我了解这些功能是如何工作的,我只是不确定在我的脚本中的何处或如何调用它们。任何帮助都会很棒!干杯!
更新:到目前为止,这是我的脚本/表单。它从一些验证检查开始,但我试图弄清楚如何在这些验证检查发生后让下拉框执行 strlen() 和 strcmp() 函数。
<?php
if (isset ($_POST['answer1'], $_POST['answer2'], $_POST['printopts'])) {
$errors = array () ;
$answer1 = $_POST['answer1'];
$answer2 = $_POST['answer2'];
$printopts = $_POST['printopts'];
if (empty($answer1) || empty($answer2) || empty($printopts)) {
$errors[] = 'All fields are required';
} else {
if (strlen($answer1) < 4 ) {
$errors[] = 'Answer 1 is too short! Please Re-enter';
}
if (strlen($answer1) > 6 ) {
$errors[] = 'Answer 1 is too long! Please Re-enter';
}
if (strlen($answer2) < 4) {
$errors[] = 'Answer 2 is too short! Please Re-enter';
}
if (strlen($answer2) > 6) {
$errors[] = 'Answer 2 is too long! Please Re-enter';
}
if (is_numeric($answer1)) {
$errors[] = 'Answer 1 must only contain letters. Please Re-enter';
}
if (is_numeric($answer2)) {
$errors[] = 'Answer 2 must only contain letters. Please Re-enter';
}
if (!empty($errors)) {
foreach ($errors as $error) {
echo '<strong>', $error, '</strong><br/>';
}
}
else {
echo 'Thank you for your answers!';
}
}
}
?>
<form action="test.php" method="post">
<p>
Answer 1:<br/><input type="text" name="answer1"/><br/>
Answer 2:<br/><input type="text" name="answer2"/><br/>
Print Options:<br/><select name="printopts">
<option selected=""></option>
<option value="Print Longest Answer">Print Longest Answer</option>
<option value="Print Sorted Order">Print Sorted Order</option>
</select>
<br/>
</p>
<p><input type="submit" value="Submit" /></p>
</form>