multiple input validation

Discussion in 'PHP' started by steyr, Aug 8, 2007.

  1. #1
    Hy!

    I have a form where the user is able to select which attribute is going to be part of the query and has one field to insert the attributes value. This works fine. The only problem is that I have some troubles to do the input validation. So ensure that there is a attribute value is not the thing, but I can not check if the value is numeric because it could also be a date.
    Has somebody an idea how to solve this without changing the structure of the forms!

    
    <select name = "attribute">
    <option value="ID">defined_area_id = </option>
    <option value="date">defined_area_date = </option>
    </selection>
    
    <input name = "values">
    <input type="submit" value="submit">
    
    Code (markup):

     
    steyr, Aug 8, 2007 IP
  2. killerj

    killerj Active Member

    Messages:
    765
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    95
    #2
    killerj, Aug 8, 2007 IP
  3. steyr

    steyr Guest

    Messages:
    27
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I will have only very few users for my php project and therefore I think it is enough to give a field for entering a date and then I check the input using the checkdate() function. And I have not much experiance in PHP so I want to play a little with different solutions to get to know the language better.

    I have solved the task by myself. Maybe not elegant, but it works:

    
    
    if (!isset($_POST['fields']) || trim($_POST['fields']) == '') {
            die("Please choose a value");
        }
        
        elseif (!isset($_POST['values']) || trim($_POST['values']) == '') {
            die("ERROR: please insert a value");
        }
        
        elseif (is_numeric($_POST['values'])) {
            //die ("ERROR: Whatever you just said isn't a number!");}
            echo "You inserted a area ID of {$_POST['values']}";}
            
       else {$a = $_POST['values'];
            $parts = explode('.', $a);
     
            if (!checkdate($parts[1], $parts[0], $parts[2])) {
            die("ERROR: wrong input");}}
    
    
    Code (markup):
     
    steyr, Aug 8, 2007 IP