Form handling with PHP

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Glyphman

    Form handling with PHP

    I have a bunch of pages with long forms, with lots of input
    types-text, radios, textareas, and the debugging process has become
    overwhelming.
    What I need to happen is to make sure that
    1. Every field on the page is filled out-if not, a list of incorrect
    fields is displayed, and any radio boxes that were checked still are
    2. The fields are queried to a table in a database. (The table has the
    same column names as the field names)

    Here's what I did; can someone tell me why this doesn't work?

    <?php
    require_once ('../mysql_connect.p hp');
    $page_title = 'Test page';

    if (isset($_POST['submit'])) { // Handle the form.

    // Create an empty new variable.
    $message = NULL;

    // Create a function for escaping the data.
    function escape_data ($data) {
    global $dbc; // Need the connection.
    if (ini_get('magic _quotes_gpc')) {
    $data = stripslashes($d ata);
    }
    return mysql_real_esca pe_string($data , $dbc);
    }

    //Create an array of all formfield names
    $required=array ('field1','fiel d2','field3');

    //Check to see if all field names are filled in
    function field_check($re quired) {
    foreach($requir ed as $q) {
    if(empty($_POST[$q])) {
    $message = 'Error: The following field was not filled in: $q';
    $ok=FALSE;
    }
    else {
    $ok;
    }
    }
    }

    // If no empty posts are returned
    if ($ok) {
    //set a variable for the current user
    $u = addslashes($_CO OKIE['user_id']);

    //insert all variable values into the table
    foreach($requir ed as $q) {
    $query = "INSERT INTO test_table ($q) VALUES ('$'.'$q')";
    }

    // Run the query
    $result = @mysql_query ($query); // Run the query.

    // If it ran OK, write the the page with a link to continue.
    if ($result) {
    include ('templates/header.inc');
    $page_title = 'Entry Recorded';
    echo "<p><b><a href=\"next_pag e.php\"
    class=\"content \">Continue</a>";
    include ('templates/footer.inc');
    exit(); // Quit the script.
    }
    // If it did not run OK, show an error
    else {
    $message = '<p>Your form could not be recorded due to a system
    error. We apologize for any inconvenience.</p><p>' . mysql_error() .
    '</p>';
    }
    }

    // Close the database connection
    mysql_close($db c);
    }

    include ('templates/header.inc');

    // Print the error message if there is one.
    if (isset($message )) {
    echo '<font color="red">', $message, '</font>';
    }

    // Print the page.
    ?>

    <form action="<? echo $_SERVER['PHP_SELF']; ?>" method="post">
    <table>
    <tr>
    <td><input type="radio" name="field1" value="1"</td>
    <td><input type="radio" name="field1" value="2"></td>
    <td><input type="text" name="field2"></td>
    <td><textarea name="field3" rows="20" cols="30">
    </textarea></td>
    </tr>
    </table>
    <p>
    <input type="submit" name="submit" value="Add to Record">
    </p>
    </form>
    <?
    include ('templates/footer.inc'); // Include the HTML footer.
    ?>
    Thanks.
  • Geoff Berrow

    #2
    Re: Form handling with PHP

    I noticed that Message-ID:
    <dda92337.04021 60915.4bab4ba3@ posting.google. com> from Glyphman
    contained the following:
    [color=blue]
    >Here's what I did; can someone tell me why this doesn't work?[/color]

    It doesn't work because it isn't coded properly...


    What bit doesn't work?

    --
    Geoff Berrow (put thecat out to email)
    It's only Usenet, no one dies.
    My opinions, not the committee's, mine.
    Simple RFDs http://www.ckdog.co.uk/rfdmaker/

    Comment

    • Manuel Lemos

      #3
      Re: Form handling with PHP

      Hello,

      On 02/16/2004 02:15 PM, Glyphman wrote:[color=blue]
      > I have a bunch of pages with long forms, with lots of input
      > types-text, radios, textareas, and the debugging process has become
      > overwhelming.
      > What I need to happen is to make sure that
      > 1. Every field on the page is filled out-if not, a list of incorrect
      > fields is displayed, and any radio boxes that were checked still are
      > 2. The fields are queried to a table in a database. (The table has the
      > same column names as the field names)[/color]

      You may want to try this class instead of reinventing the wheel. It can
      compose and validate HTML forms, either on client side with Javascript
      or on the server side with the class code. It supports many common
      validation types.

      You can compose the forms output with a Smarty plugin that makes it
      possible to integrate with the class. The generated output embeds the
      necessary Javascript but you do not need to know Javascript at all.



      --

      Regards,
      Manuel Lemos

      PHP Classes - Free ready to use OOP components written in PHP
      Free PHP Classes and Objects 2026 Versions with PHP Example Scripts, PHP Tutorials, Download PHP Scripts, PHP articles, Remote PHP Jobs, Hire PHP Developers, PHP Book Reviews, PHP Language OOP Materials


      PHP Reviews - Reviews of PHP books and other products


      Metastorage - Data object relational mapping layer generator

      Comment

      Working...