HTML forms and PHP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • joker
    New Member
    • Oct 2006
    • 3

    HTML forms and PHP

    I'm somewhat of a newbie to PHP, however i would appreciate if someone would be kind enough and help me with my problem.
    To give you a better understanding of what my problem is, here is what i'm trying to do.
    I have a webpage (A.php) which contains an html form (formA) which has a number of text fields and a submit button. When i click the submit button i want to be able to get the value in the text field assigned to a PHP variable so that i can use those vars to create a query, however it seems that i cannot get the values of the text fields. I have tried a few different ways, but it seems that i jsut cannot get it to work.
    Could there be a problem with PHP 5.1.6. I am also using mySQL 4.1 and Apache 2.0.59.

    Thank you!!!

    The following in the actual code:

    Being a newbie is no excuse for not following the Posting guidelines!
    Since this is your first entry, I have enclosed your code within html tags.
    Read the Posting Guidelines before you post anything in a thread.
    Especially the part about putting code within code, php or html tags! - Ronald


    test.php:
    [html]
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>OGAY</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">


    <?php

    $query1 = "INSERT INTO clients (custID,company Name,contactNam e,address,phone ,fax,email)
    VALUES (null,'$company Name','$name',' $address','$pho ne','$fax','$em ail');";
    print ($query1);

    ?>
    </head>

    <body>

    <span style="font-size:12pt">Plea se fill in all fields.</span><br/><br/>
    <form name="addClient " method="get" action="test.ph p ">
    <table>
    <tr>
    <td>Company Name:*</td>
    <td><input type="text" name="companyNa me" size="30" maxlength="255" /></td>
    </tr>
    <tr>
    <td>Contact Name:*</td>
    <td><input type="text" name="name" size="30" maxlength="255" /></td>
    </tr>
    <tr>
    <td>Address:</td>
    <td><input type="text" name="address" size="30" maxlength="255" /></td>
    </tr>
    <tr>
    <td>Phone:*</td>
    <td><input type="text" name="phone" size="30" maxlength="10" /></td>
    </tr>
    <tr>
    <td>Fax:*</td>
    <td><input type="text" name="fax" size="30" maxlength="10" /></td>
    </tr>
    <tr>
    <td>Email Address:</td>
    <td><input type="text" name="email" size="30" maxlength="255" /></td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    </tr>
    <tr>
    <td colspan="2" align="center"> <input type="submit" name="addClient " value="Add Client" />
    &nbsp;&nbsp;<in put type="reset" name="clear" value="Clear" /></td>
    </tr>

    </table>
    </form>

    </body>
    </html>
    [/html]
    Last edited by ronverdonk; Oct 27 '06, 08:58 AM. Reason: code enclose in tags
  • jpluttme
    New Member
    • Oct 2006
    • 4

    #2
    You don't need the column ids and then the values. Your query should look like this:

    [PHP]$query1 = "INSERT INTO clients VALUES(null,'$c ompanyName','$n ame','$address' ,'$phone',' $fax','$email') ";

    print ($query1);[/PHP]

    Comment

    • ronverdonk
      Recognized Expert Specialist
      • Jul 2006
      • 4259

      #3
      I absolutely disagree! The database should never be closely connected to the performing code. I always require that programmers define the column names in their code. Reason:

      When a column is added to the table or the column sequence is changed, the program MUST be changed at the same time, otherwise errors will result.

      Database admin and programming often are separate functions, as in many companies. The dbadministrator must always have the freedom to change the table setup (adding columns, changing sequences, etc.) without great risk to the already running applications. In the worst case some columns will not be updated, but production can go on.

      Ronald :cool:

      Comment

      • ronverdonk
        Recognized Expert Specialist
        • Jul 2006
        • 4259

        #4
        Here is some code for your form. I changed/added the following:
        • chang the GET method to the POST method
        • start the code with verification of the submitted form
        • add minor error checking for each POSTed field (just existence and max length)
        • accumulate and print any field errors found
        • when errors: re-display the form with already filled in fields displayed
        • assign POSTed variables to PHP variables after some cleansing

        You'll have to add your own field checking (content, email address validity, etc.) and the MySQL code
        [php]
        <?php
        $errors = array();
        // -----------------------------------
        // Verify that a form is submitted
        // -----------------------------------
        if (isset($_POST['addClient'])) {
        // -------------------------------------------------------------------
        // when submitted check the input fields (just checking lengths here)
        // -------------------------------------------------------------------
        if (!isset($_POST['companyName']) OR strlen($_POST['companyName']) < 5)
        $errors[] = "Error in companyname";
        if (!isset($_POST['name']) OR strlen($_POST['name']) < 5)
        $errors[] = "Error in name";
        if (!isset($_POST['address']) OR strlen($_POST['address']) < 8)
        $errors[] = "Error in address";
        if (!isset($_POST['phone']) OR strlen($_POST['phone']) < 10)
        $errors[] = "Error in phone";
        if (!isset($_POST['fax']) OR strlen($_POST['fax']) < 10)
        $errors[] = "Error in fax";
        if (!isset($_POST['email']) OR strlen($_POST['email']) < 5)
        $errors[] = "Error in email";

        // ----------------------------------------------------------
        // checking done, see if there are any errors and print them
        //-----------------------------------------------------------
        if ($errors > "") {
        print '<span style="color:re d"><ul><li><b>' ;
        print implode('</b></li><li><b>',$er rors);
        print '</b></li></ul></span>';
        } // End IF

        // ---------------------------------------
        // When no errors found, process the form
        // ---------------------------------------
        else {
        // --------------------------------------
        // Assign POSTed values to PHP variables
        // --------------------------------------
        $companyName = strip_chars(tri m($_POST['companyName']));
        $name = strip_chars(tri m($_POST['name']));
        $address = strip_chars(tri m($_POST['address']));
        $phone = strip_chars(tri m($_POST['phone']));
        $fax = strip_chars(tri m($_POST['fax']));
        $email = strip_chars(tri m($_POST['email']));

        // --------------------------------------------------------------
        // do some more checking like fields content and email validity
        // --------------------------------------------------------------
        //
        // you can do that yourself
        //

        // ---------------------------------------------------------------------
        // Construct and issue the mySQL command and perform actions on result
        // ---------------------------------------------------------------------
        // ---- connect to the MySQL server
        // ---- connect to the data base
        // ---- construct your INSERT statement

        $query1 = "INSERT INTO clients
        (custID,company Name,contactNam e,address,phone ,fax, email)
        VALUES (null,'$company Name','$name',' $address','$pho ne','$fax','$em ail');";

        // ---- execute the SELECT
        // ---- process result (error)
        print ($query1);

        } // End ELSE

        } // End If submitted
        ?>
        <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
        <html>
        <head>
        <title>OGAY</title>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
        </head>
        <body>
        <span style="font-size:12pt">Plea se fill in all fields.</span><br/><br/>
        <form name="addClient " method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
        <table>
        <tr>
        <td>Company Name:*</td>
        <td><input type="text" name="companyNa me" size="30" maxlength="255" value="<?php echo (isset($_POST['companyName'])) ? $_POST['companyName'] : ""; ?>" /><br /></td>
        </tr>
        <tr>
        <td>Contact Name:*</td>
        <td><input type="text" name="name" size="30" maxlength="255" value="<?php echo (isset($_POST['name'])) ? $_POST['name'] : ""; ?>" /></td>
        </tr>
        <tr>
        <td>Address:</td>
        <td><input type="text" name="address" size="30" maxlength="255" value="<?php echo (isset($_POST['address'])) ? $_POST['address'] : ""; ?>" /></td>
        </tr>
        <tr>
        <td>Phone:*</td>
        <td><input type="text" name="phone" size="30" maxlength="10" value="<?php echo (isset($_POST['phone'])) ? $_POST['phone'] : ""; ?>" /></td>
        </tr>
        <tr>
        <td>Fax:*</td>
        <td><input type="text" name="fax" size="30" maxlength="10" value="<?php echo (isset($_POST['fax'])) ? $_POST['fax'] : ""; ?>" /></td>
        </tr>
        <tr>
        <td>Email Address:</td>
        <td><input type="text" name="email" size="30" maxlength="255" value="<?php echo (isset($_POST['email'])) ? $_POST['email'] : ""; ?>" /></td>
        </tr>
        <tr>
        <td>&nbsp;</td>
        </tr>
        <tr>
        <td colspan="2" align="center"> <input type="submit" name="addClient " value="Add Client" />
        &nbsp;&nbsp;<in put type="reset" name="clear" value="Clear" /></td>
        </tr>

        </table>
        </form>

        </body>
        </html>
        [/php]
        It is tested (crudely), so run it and see what it is doing.
        Good luck!

        Ronald :cool:

        Comment

        • joker
          New Member
          • Oct 2006
          • 3

          #5
          Ronald, Thank you very much. I just got around now to try your code, it works great after i got rid of the else block. For some reason after it executes the error checking "if" block, it doesn't go into the "else" block, ......strange but its ok.
          Thanks once again.

          Comment

          • ronverdonk
            Recognized Expert Specialist
            • Jul 2006
            • 4259

            #6
            I would be disapppointed with any programming/script language when it would go into an ELSE block after executing the IF block.

            Here the reason for those two blocks are that you want (IF block) to display the errors and then re-display the form again until all fields are correctly filled in. When they are correctly filled in, the IF block will not be executed (because $errors is empty) but go straight into the ELSE block where you perform your actual data handling.

            Or is that process unclear?

            Ronald :cool:

            Comment

            Working...