Checking for empty fields in a form.

Discussion in 'PHP' started by monster55, Apr 1, 2008.

  1. #1
    Hello Forum Members:

    I work for a high school as an instructor.I have a "form.html" file on the server onto which students fill up their information. Upon submitting the information, the data is recorded onto an excel file. If a student leaves any of the form fields blank, I would like an error message stating the form feilds that were left empty.

    Here's a link to the form:
    www.nhgs.tec.va.us/~schinnab/form.html

    Following is the existing PHP code which writes the input data onto an excel file and also checks for duplication of data. If any of you can help me with the fragment of the code which can be added to the code below to check for empty fields and listing them, I would be very grateful. Thanks!

    <?php
    $debug=0;
    $where_form_is="http://".$_SERVER['SERVER_NAME'].strrev(strstr(strrev($_SERVER['PHP_SELF']),"/"));
    $firstline="First Name:,Last Name:,Street No:,City:,State:,Zip Code:,Grade Level:\n";
    $fileLine = $_POST['field_1'] . "," . $_POST['field_2'] . "," . $_POST['field_3'] . "," . $_POST ['field_4']. ",". $_POST['field_5'] . ",". $_POST['field_6'] . ",". $_POST ['field_7'] . "\r\n";

    $filename = './files/Data_Set.csv';
    if (is_readable($filename)) {
    if (!$handle = fopen($filename, 'r')) {
    echo "Cannot open file ($filename)";
    exit;
    }
    if ($handle) {
    //Strip First Line
    fgets($handle,4096);
    while (!feof($handle)) {
    $lines[] = fgets($handle, 4096);
    }
    fclose($handle);
    }
    for ($i=0;$i<sizeof($lines);$i++) {
    if($debug && $lines[$i]){echo "Line ".($i+1)."=".$lines[$i]."<br />";}
    $columns = explode(",",$lines[$i]);
    if($debug && $columns[0]){
    echo "Key 0 =".$columns[0]." Post =".$_POST['field_1']."<br />";
    echo "Key 1 =".$columns[1]." Post =".$_POST['field_2']."<br />";
    echo "Key 2 =".$columns[2]." Post =".$_POST['field_3']."<br />";
    echo "Key 3 =".$columns[3]." Post =".$_POST['field_4']."<br />";
    echo "Key 4 =".$columns[4]." Post =".$_POST['field_5']."<br />";
    echo "Key 5 =".$columns[5]." Post =".$_POST['field_6']."<br />";
    echo "Key 6 =".$columns[6]." Post =".$_POST['field_7']."<br />";
    }
    if ((trim($columns[0])==trim($_POST['field_1']))&&
    (trim($columns[1])==trim($_POST['field_2']))&&
    (trim($columns[2])==trim($_POST['field_3']))&&
    (trim($columns[3])==trim($_POST['field_4']))&&
    (trim($columns[4])==trim($_POST['field_5']))&&
    (trim($columns[5])==trim($_POST['field_6']))&&
    (trim($columns[6])==trim($_POST['field_7']))) {
    $errormessage='<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>interests - created by phpFormGenerator</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><link href="style.css" rel="stylesheet" type="text/css">
    <link rel="stylesheet" type="text/css" href="calendar/calendar-blue2.css" />
    <script type="text/javascript" src="calendar/calendar.js"></script>
    <script type="text/javascript" src="calendar/calendar-en.js"></script>
    <script type="text/javascript" src="calendar/calendar-setup.js"></script></head>
    <body onLoad="collapseAll()">
    <div id="mainForm">
    <div id="formHeader">
    <h2 class="formInfo">interests</h2>
    <p class="formInfo"></p>
    </div>
    <BR/><ul class=mainForm><li class=mainForm><label class=formFieldQuestion>ERROR!<BR/>
    This entry already exists! Please go back and try again.<br />'.
    "<br />".$_POST['field_1']."<br />".$_POST['field_2']."<br />".$_POST['field_3']." <br />".$_POST['field_4']." <br />".$_POST['field_5']." <br />".$_POST['field_6']."<br />".$_POST['field_7']
    .'</label></li></ul><BR/></div><div id="footer"><p class="footer"><a class=footer href=http://phpformgen.sourceforge.net>Generated by phpFormGenerator</a></p></div>
    </body>
    </html>';
    echo $errormessage;
    exit;
    }
    }

    } else {
    echo "The file is not readable";
    }

    if (is_writable($filename)) {

    if (!$handle = fopen($filename, 'a'))
    {
    echo "Cannot open file ($filename)";
    exit;
    }
    if (fwrite($handle, $fileLine) === FALSE) {
    echo "Cannot write to file ($filename)";
    exit;
    }

    fclose($handle);

    } else {
    echo "The file is not writable";
    }

    if(!$debug){ include("confirm.html"); }

    ?>
     
    monster55, Apr 1, 2008 IP
  2. monster55

    monster55 Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Hello Members,

    Can someone please reply?

    Thanks!!
     
    monster55, Apr 2, 2008 IP
  3. vishnups

    vishnups Banned

    Messages:
    166
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #3
    You need to use Javascript validation which will be done client side or go for php validation server side. If you find it difficult I can do it for you.
     
    vishnups, Apr 2, 2008 IP
  4. monster55

    monster55 Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Hello Vishnups,

    Thanks for the reply. Would you mind adding the code for PHP validation to my code in my previous message? Kindly let me know.

    Thank you so much!!
    monster55
     
    monster55, Apr 3, 2008 IP
  5. monster55

    monster55 Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Hello Vishnups,

    Would you mind helping me ? Kindly let me know.

    Thank you so much!!
    monster55
     
    monster55, Apr 15, 2008 IP
  6. JLEville

    JLEville Peon

    Messages:
    147
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    You could use if(isset($_POST['variable_name']))'s in the beginning of the script to see whether they left some aspects of the form blank and if they did; you can give them an error message.
     
    JLEville, Apr 15, 2008 IP
  7. Sabbir

    Sabbir Banned

    Messages:
    210
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    58
    #7
    Hi,

    check this work of mine..(i always love to show my work. :))
    http://www.modlexcentre.com/new/enquee1.php
    check the java code.. i did it in javascript so i did not need to post beck to check all the fields.

    thanks
     
    Sabbir, Apr 15, 2008 IP
  8. monster55

    monster55 Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Hello JLEville,

    Thanks for the reply.

    Would you mind adding the line for a single field (say, the first field) in the code from my first message? My PHP coding skills is terrible. After I see your code, I can then see the pattern and reproduce the same for the rest of the fields.

    Kindly let me know.

    Thanks!!
     
    monster55, Apr 16, 2008 IP
  9. monster55

    monster55 Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Hello JLEville,

    Could you please reply?

    Thanks!
     
    monster55, Apr 25, 2008 IP
  10. monster55

    monster55 Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Hello JLEville,

    Thanks for the reply.

    Would you mind adding the line for a single field (say, the first field) in the code from my first message? My PHP coding skills are terrible. After I see your code, I can then see the pattern and reproduce the same for the rest of the fields.

    Kindly let me know.

    Thanks!!
     
    monster55, May 2, 2008 IP