-1

我将以下下拉列表作为 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>
4

2 回答 2

0
function do_print($str1, $str2) {

if ($_GET['printopts'] == "Print Longest Answer") {

   strlen();

} else {

  strcmp($str1,$str2);

}

 }

表单提交后执行do_print。

于 2013-03-15T12:16:47.320 回答
0

对,首先你需要在<form> html标签和方法参数中设置action='myscript.php'(例如:method='get')。所以它是这样的:

<form action='myscript.php' method='get'>
...
</form>

然后在 myscript.php 中,您将需要检查您的 php 代码:

if($_GET['printopts']=='Print Longest Answer') {
     //do your strlen here
} else {
     //do strcmp here
}
于 2013-03-15T12:17:02.793 回答