Page 2 of 2

Re: Simple search form to get status from external file

Posted: Mon Feb 06, 2012 5:37 pm
by Celauran
OK, I wasn't sure if you wanted to return partial matches or what you wanted to do when a match was found. Are you looking for something like this?

Code: Select all

<?php

if (!empty($_POST))
{
    $serial = $_POST['serial_box_div']; //loads the var value
    if (preg_match('/\d{5}/', $serial))
    {
        $file = @fopen("assitenciatecnica.txt", "r"); //read file
        if ($file)
        { //validate file
            while (!feof($file))
            {
                $line = fgets($file, 1024); //read line
                if (preg_match('/^' . $serial . '/', $line))
                {
                    $data = explode(' ', $line);
                    $id = rtrim($data[1], ';');
                    echo "<div id=\"id_{$id}\">Div id_{$id} created.</div>";
                }
            }
        }
    }
}

?>

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
        <title>Debug</title>
    </head>
    <body>
        <form action="" method="post"><input name="serial_box_div" type="text" /> <input name="" type="submit" value="enviar"/></form>
    </body>
</html>

Re: Simple search form to get status from external file

Posted: Mon Feb 06, 2012 5:49 pm
by Colucas
hmm nice! thats it! rtrim gets the next character after the ; ? That's exactly what I was looking for!! ^^

Can you please also add another behaviour? If the serial isn't found, then a error div would appear? Tried else's along the code but none worked, guess that's not how its done!

Re: Simple search form to get status from external file

Posted: Mon Feb 06, 2012 5:51 pm
by Celauran

Code: Select all

<?php

if (!empty($_POST))
{
    $serial = $_POST['serial_box_div']; //loads the var value
    if (preg_match('/\d{5}/', $serial))
    {
        $file = @fopen("assitenciatecnica.txt", "r"); //read file
        if ($file)
        { //validate file
            while (!feof($file))
            {
                $line = fgets($file, 1024); //read line
                if (preg_match('/^' . $serial . '/', $line))
                {
                    $data = explode(' ', $line);
                    $id = rtrim($data[1], ';');
                    echo "<div id=\"id_{$id}\">Div id_{$id} created.</div>";
                }
            }
        }
    }
    else
    {
        echo "Serial must be 5 digits.";
    }
}

?>

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
        <title>Debug</title>
    </head>
    <body>
        <form action="" method="post"><input name="serial_box_div" type="text" /> <input name="" type="submit" value="enviar"/></form>
    </body>
</html>