Search code examples
phphtmlformsstrcmpstrlen

PHP for HTML forms


I have the below drop down list as part of a HTML form:

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>

I need my php script to carry out the strlen() function if the user chooses "Print Longest Answer" or strcmp($str1, $str2) if the user chooses "Print Sorted Order".

I understand how the functions work, I'm just not sure where or how to call them in my script. Any help would be super! Cheers!

UPDATE: This is my script/form so far. It begins with some validation checks but I'm trying to work out how to get the strlen() and strcmp() functions for the drop down boxes to execute after these validation checks happen.

<?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>

Solution

  • Right, first you need to set action='myscript.php' in the <form> html tag and the method parametre (eg.: method='get'). So it is something like this:

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

    Then in that myscript.php you will need to have your php code that checks:

    if($_GET['printopts']=='Print Longest Answer') {
         //do your strlen here
    } else {
         //do strcmp here
    }