Stopping an empty form being submitted

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • flexsingh
    New Member
    • Mar 2008
    • 17

    #1

    Stopping an empty form being submitted

    Hello there, I have a form and when data is inputted it goes into the table and I can get it out pefectly fine, the problem I have is that if the promary key is empty it will not submit, but if any of the other fields are empty except the primary key then the forms submits.

    Is there a way to stop the form submitting unless all the fields are filled in first?

    [PHP]<?php
    // this starts the session
    session_start() ;
    ?>
    <?php

    $db_name="proje ct"; // Database name
    $tbl_name="memb er"; // Table name

    // Connect to server and select database.
    mysql_connect($ _SESSION['host'], $_SESSION['username'], $_SESSION['password'])or die("cannot connect");
    mysql_select_db ("$db_name") or die("cannot select DB");

    // Get values from form
    $memberno=$_POS T['Member_No'];
    $surname=$_POST['Surname'];
    $forename=$_POS T['Forename'];
    $address=$_POST['Address'];
    $dob=$_POST['DOB'];


    // Insert data into mysql
    $sql="INSERT INTO $tbl_name(Membe r_No, Surname, Forename, Address, DOB)VALUES('$me mberno', '$surname', '$forename', '$address', '$dob')";
    $result=mysql_q uery($sql);

    // if successfully insert data into database, displays message "Successful ".
    if($result){

    header('Locatio n: blank.php');
    }

    else {
    echo "ERROR";
    }

    // close connection
    mysql_close();
    ?>[/PHP]

    Any help or ideas would be very welcome.

    Thank you in advanced.
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    Where is the submitting form? I assume it is a separate script. You can test the POSTed values and, when any of them is blank, re-display the form in the other script. like [php]
    // Test values from form
    if (!isset($_POST['Member_No']) OR $_POST['Member_No'] == '' OR
    !isset($_POST['Surname']) OR $_POST['Surname'] == '' OR
    !isset($_POST['Forename']) OR $_POST['Forename'] == '' OR
    !isset($_POST['Address']) OR $_POST['Address'] == '' OR
    !isset($_POST['DOB']) OR $_POST['DOB'] == '' )
    // handle error and goto form script[/php]
    A lot easier is to combine the form script and the form 'catcher' script so you can, whenever an error occurs, just re-display the form again with all the fields, that were filled in and valid, re-displayed in the form.

    But then you'll have to show the form script code.

    But this way the form is always submitted because you do the checking at the server side. If you want to do the checking before submission at the client side, you'll have to use JavaScript validation and submission.

    Ronald

    Comment

    • TheServant
      Recognized Expert Top Contributor
      • Feb 2008
      • 1168

      #3
      Originally posted by ronverdonk
      But this way the form is always submitted because you do the checking at the server side. If you want to do the checking before submission at the client side, you'll have to use JavaScript validation and submission.
      I agree, the way I do it is check everything is good on the client side with javascript (it just checks if the input is write and if it's not it will display an error box, otherwise it submits it). Then when you get to the sevrer side you use something like ronverdonk's code above with it's own validation.

      Basically this reduces the processing required on the server side, it lets the user know their input is wrong instantly instead of having to submit a form and reload the page, and most importantly, if the user has disabled javascript, you can still validate their info using php on the server side.

      Comment

      • flexsingh
        New Member
        • Mar 2008
        • 17

        #4
        Originally posted by ronverdonk
        Where is the submitting form? I assume it is a separate script. You can test the POSTed values and, when any of them is blank, re-display the form in the other script. like [php]
        // Test values from form
        if (!isset($_POST['Member_No']) OR $_POST['Member_No'] == '' OR
        !isset($_POST['Surname']) OR $_POST['Surname'] == '' OR
        !isset($_POST['Forename']) OR $_POST['Forename'] == '' OR
        !isset($_POST['Address']) OR $_POST['Address'] == '' OR
        !isset($_POST['DOB']) OR $_POST['DOB'] == '' )
        // handle error and goto form script[/php]
        A lot easier is to combine the form script and the form 'catcher' script so you can, whenever an error occurs, just re-display the form again with all the fields, that were filled in and valid, re-displayed in the form.

        But then you'll have to show the form script code.

        But this way the form is always submitted because you do the checking at the server side. If you want to do the checking before submission at the client side, you'll have to use JavaScript validation and submission.

        Ronald

        Hmm this is my form sorry


        [HTML]<html>

        <br>
        <br>
        <head>
        <h1><font face="sans-serif, Arial" font color="white">A dd Member Page!</h1>
        </head>
        <br>
        <br>
        <br>
        <br>
        <br>
        <br>
        <br>
        <br>

        <body background="mai n background1.jpg " link="blue" vlink="blue">

        <table width="350" border="0" align="center" cellpadding="0" cellspacing="1" >
        <tr>
        <td><form name="update member form" method="post" action="updatem ember_ac.php">
        <table width="100%" border="0" cellspacing="1" cellpadding="3" >
        <tr>
        <td colspan="3" align="center"> <strong><font face="sans-serif, Arial" font color="white">I nsert Data Into mySQL Database </strong></td>
        </tr>
        <tr>
        <td><font face="sans-serif, Arial" font color="white">M ember No</td>
        <td>:</td>
        <td><input name="Member_No " type="int" id="Member_No" value="from below"></td>
        </tr>
        <tr>
        <td><font face="sans-serif, Arial" font color="white">S urname</td>
        <td>:</td>
        <td><input name="Surname" type="varchar" id="Surname"></td>
        </tr>
        <tr>
        <td><font face="sans-serif, Arial" font color="white">F orename</td>
        <td>:</td>
        <td><input name="Forename" type="varchar" id="Forename"> </td>
        </tr>
        <tr>
        <td><font face="sans-serif, Arial" font color="white">A ddress</td>
        <td>:</td>
        <td><textarea name="Address" cols="16" row="4" id="Address"></textarea></td>
        </tr>
        <tr>
        <td><font face="sans-serif, Arial" font color="white">D OB</td>
        <td>:</td>
        <td><input name="DOB" type="date" id="DOB" value="YYYY-MM-DD"></td>
        </tr>
        <td colspan="3" align="center"> <input type="submit" name="Submit" value="Finish"> </td>
        </tr>
        </table>
        </form>
        </td>
        </tr>
        </table>
        <br><br>
        <br><br>
        <table align="right" width="600" border="0" cellspacing="0" cellpadding="3" >
        <tr>
        <td align="center" width="30%"><b> <u><font face="sans-serif, Arial" font color="white">M ember No</b></u></td>
        <td align="center" width="40%"><b> <u><font face="sans-serif, Arial" font color="white">M embership Start Date</b></u></td>
        <td align="center" width="30%"><b> <u><font face="sans-serif, Arial" font color="white">M embership No</b></u></td>
        </tr>
        </table>
        </body
        </html>[/HTML]

        I duno where that code should go. I think I understand what you mean though, I can only check if there was data inputted in after the submission, I really just wanted it to say sorry please fill in all fields first because it creates alot of empty spaces which aint really good for the presentation.

        I really have no knowledge of javascripts so if you could provide any code, I know its a huge ask but it would be soo appreciated.

        Thank you for your quick responce.

        Comment

        • TheServant
          Recognized Expert Top Contributor
          • Feb 2008
          • 1168

          #5
          Just google javascript form validation. That's where I get most of my javascript code when I haven't done something before. But I want to stress that you need server side protection! Where the form is submitted to needs some validation. The javascript is for convenience of the user, and is in no way a protection against malicious code.

          Comment

          • flexsingh
            New Member
            • Mar 2008
            • 17

            #6
            Originally posted by TheServant
            Just google javascript form validation. That's where I get most of my javascript code when I haven't done something before. But I want to stress that you need server side protection! Where the form is submitted to needs some validation. The javascript is for convenience of the user, and is in no way a protection against malicious code.
            Thank you for your help, its ok the site isnt for commercial use or anything like that soo security isnt going to be a problem, thank you again much appreciated.

            Comment

            • TheServant
              Recognized Expert Top Contributor
              • Feb 2008
              • 1168

              #7
              Originally posted by flexsingh
              Thank you for your help, its ok the site isnt for commercial use or anything like that soo security isnt going to be a problem, thank you again much appreciated.
              No problem, hope to see you stick around the forums.

              Comment

              • ronverdonk
                Recognized Expert Specialist
                • Jul 2006
                • 4259

                #8
                In order to give a helping hand, I combined your two scripts into one script. This contains the validation, the insertion into the db, prinnting of the error message (if any), displaying the form and filling in all the fields that were correctly filled in the first time. I have not tested this, but it should run with (maybe) some changes at your side. [php]<?php
                // this starts the session
                session_start() ;
                $error='';
                if (isset($_POST['Submit'])) {
                // check the passed values
                if (!isset($_POST['Member_No']) OR $_POST['Member_No'] == '' OR
                !isset($_POST['Surname']) OR $_POST['Surname'] == '' OR
                !isset($_POST['Forename']) OR $_POST['Forename'] == '' OR
                !isset($_POST['Address']) OR $_POST['Address'] == '' OR
                !isset($_POST['DOB']) OR $_POST['DOB'] == '' ) {
                $error="Error, you must fill in all fields in this form";
                }
                else {
                $db_name="proje ct"; // Database name
                $tbl_name="memb er"; // Table name

                // Connect to server and select database.
                mysql_connect($ _SESSION['host'], $_SESSION['username'], $_SESSION['password'])or die("cannot connect");
                mysql_select_db ("$db_name") or die("cannot select DB");

                // Get values from form
                $memberno=$_POS T['Member_No'];
                $surname=$_POST['Surname'];
                $forename=$_POS T['Forename'];
                $address=$_POST['Address'];
                $dob=$_POST['DOB'];

                // Insert data into mysql
                $sql="INSERT INTO $tbl_name(Membe r_No, Surname, Forename, Address, DOB)VALUES('$me mberno', '$surname', '$forename', '$address', '$dob')";
                $result=mysql_q uery($sql)
                or die("INSERT error: ".mysql_error() );

                // if successfully insert data into database, displays message "Successful ".
                echo "Successful ly inserted in database";
                // close connection
                mysql_close();
                exit;
                }
                if ($error > '')
                echo "<span style='color:re d;'>$error</span>";
                } // END if submitted
                ?>
                <html>
                <br>
                <br>
                <head>
                <h1><font face="sans-serif, Arial" color="black">A dd Member Page!</h1>
                </head>
                <br>
                <br>
                <br>
                <br>
                <br>
                <br>
                <br>
                <br
                <body background="mai n background1.jpg " link="blue" vlink="blue">
                <table width="350" border="0" align="center" cellpadding="0" cellspacing="1" >
                <tr>
                <td>
                <form name="update member form" method="post" action="<?php $_SERVER['PHP_SELF'] ?>">
                <table width="100%" border="0" cellspacing="1" cellpadding="3" >
                <tr>
                <td colspan="3" align="center"> <strong><font face="sans-serif, Arial" color="black">I nsert Data Into mySQL Database </strong></td>
                </tr>
                <tr>
                <td><font face="sans-serif, Arial" color="black">M ember No</td>
                <td>:</td>
                <td><input name="Member_No " type="int" id="Member_No" value="from below"></td>
                </tr>
                <tr>
                <td><font face="sans-serif, Arial" color="black">S urname</td>
                <td>:</td>
                <td><input name="Surname" type="varchar" id="Surname" value="<?php echo (isset($_POST['Surname'])) ? $_POST['Surname'] : ""; ?>"></td>
                </tr>
                <tr>
                <td><font face="sans-serif, Arial" color="black">F orename</td>
                <td>:</td>
                <td><input name="Forename" type="varchar" id="Forename" value="<?php echo (isset($_POST['Forename'])) ? $_POST['Forename'] : ""; ?>"></td>
                </tr>
                <tr>
                <td><font face="sans-serif, Arial" color="black">A ddress</td>
                <td>:</td>
                <td><textarea name="Address" cols="16" rows="4" id="Address"><? php echo (isset($_POST['Address'])) ? $_POST['Address'] : ""; ?></textarea></td>
                </tr>
                <tr>
                <td><font face="sans-serif, Arial" color="black">D OB YYYY-MM-DD</td>
                <td>:</td>
                <td><input name="DOB" type="date" id="DOB" value="<?php echo (isset($_POST['date'])) ? $_POST['date'] : ""; ?>"></td>
                </tr>
                <td colspan="3" align="center"> <input type="submit" name="Submit" value="Finish"> </td>
                </tr>
                </table>
                </form>
                </td>
                </tr>
                </table>
                <br><br>
                <br><br>
                <table align="right" width="600" border="0" cellspacing="0" cellpadding="3" >
                <tr>
                <td align="center" width="30%"><b> <u><font face="sans-serif, Arial" color="black">M ember No</b></u></td>
                <td align="center" width="40%"><b> <u><font face="sans-serif, Arial" color="black">M embership Start Date</b></u></td>
                <td align="center" width="30%"><b> <u><font face="sans-serif, Arial" color="black">M embership No</b></u></td>
                </tr>
                </table>
                </body>
                </html>[/php]Good luck!

                Ronald

                Comment

                • flexsingh
                  New Member
                  • Mar 2008
                  • 17

                  #9
                  Ow thank you soo very much, words can not show how thankful I am, I will try this right away thank you again :D
                  Last edited by ronverdonk; Mar 28 '08, 04:30 PM. Reason: code out post too long

                  Comment

                  • flexsingh
                    New Member
                    • Mar 2008
                    • 17

                    #10
                    It has worked fine thank you very much.

                    Comment

                    • ronverdonk
                      Recognized Expert Specialist
                      • Jul 2006
                      • 4259

                      #11
                      Originally posted by flexsingh
                      It has worked fine thank you very much.
                      As I said, I merely combined the 2 separate scripts you already had into 1 script.

                      But I am glad this is your solution. See you next time.

                      Ronald

                      Comment

                      Working...