Login or Sign up Help | Site Map
Connecting Tech Pros Worldwide

Stopping an empty form being submitted

Question posted by: flexsingh (Newbie) on March 27th, 2008 09:18 PM
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?

Code: ( text )
  1. <?php
  2. // this starts the session
  3. session_start();
  4. ?>
  5. <?php
  6.  
  7. $db_name="project"; // Database name
  8. $tbl_name="member"; // Table name
  9.  
  10. // Connect to server and select database.
  11. mysql_connect($_SESSION['host'], $_SESSION['username'], $_SESSION['password'])or die("cannot connect");
  12. mysql_select_db("$db_name")or die("cannot select DB");
  13.  
  14. // Get values from form
  15. $memberno=$_POST['Member_No'];
  16. $surname=$_POST['Surname'];
  17. $forename=$_POST['Forename'];
  18. $address=$_POST['Address'];
  19. $dob=$_POST['DOB'];
  20.  
  21.  
  22. // Insert data into mysql
  23. $sql="INSERT INTO $tbl_name(Member_No, Surname, Forename, Address, DOB)VALUES('$memberno', '$surname', '$forename', '$address', '$dob')";
  24. $result=mysql_query($sql);
  25.  
  26. // if successfully insert data into database, displays message "Successful".
  27. if($result){
  28.  
  29. header('Location: blank.php');
  30. }
  31.  
  32. else {
  33. echo "ERROR";
  34. }
  35.  
  36. // close connection
  37. mysql_close();
  38. ?>


Any help or ideas would be very welcome.

Thank you in advanced.
Would you like to answer this question?
Sign up for a free account, or Login (if you're already a member).
ronverdonk's Avatar
ronverdonk
Moderator
4,138 Posts
March 27th, 2008
09:30 PM
#2

Re: Stopping an empty form being submitted
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
Code: ( text )
  1. // Test values from form
  2. if (!isset($_POST['Member_No']) OR $_POST['Member_No'] == '' OR
  3.     !isset($_POST['Surname'])   OR $_POST['Surname']   == '' OR
  4.     !isset($_POST['Forename'])  OR $_POST['Forename']  == '' OR
  5.     !isset($_POST['Address'])   OR $_POST['Address']   == '' OR
  6.     !isset($_POST['DOB'])       OR $_POST['DOB']       == '' )
  7.     // handle error and goto form script

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
__________________
RTFM is an almost extinct art form.

Reply
TheServant's Avatar
TheServant
Needs Regular Fix
441 Posts
March 27th, 2008
09:52 PM
#3

Re: Stopping an empty form being submitted
Quote:
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.

Reply
flexsingh's Avatar
flexsingh
Newbie
17 Posts
March 27th, 2008
10:00 PM
#4

Re: Stopping an empty form being submitted
Quote:
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
Code: ( text )
  1. // Test values from form
  2. if (!isset($_POST['Member_No']) OR $_POST['Member_No'] == '' OR
  3.     !isset($_POST['Surname'])   OR $_POST['Surname']   == '' OR
  4.     !isset($_POST['Forename'])  OR $_POST['Forename']  == '' OR
  5.     !isset($_POST['Address'])   OR $_POST['Address']   == '' OR
  6.     !isset($_POST['DOB'])       OR $_POST['DOB']       == '' )
  7.     // handle error and goto form script

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


Code: ( text )
  1. <html>
  2.  
  3. <br>
  4. <br>
  5. <head>
  6. <h1><font face="sans-serif, Arial" font color="white">Add Member Page!</h1>
  7. </head>
  8. <br>
  9. <br>
  10. <br>
  11. <br>
  12. <br>
  13. <br>
  14. <br>
  15. <br>
  16.  
  17. <body background="main background1.jpg" link="blue" vlink="blue">
  18.  
  19. <table width="350" border="0" align="center" cellpadding="0" cellspacing="1">
  20. <tr>
  21. <td><form name="update member form" method="post" action="updatemember_ac.php">
  22. <table width="100%" border="0" cellspacing="1" cellpadding="3">
  23. <tr>
  24. <td colspan="3" align="center"><strong><font face="sans-serif, Arial" font color="white">Insert Data Into mySQL Database </strong></td>
  25. </tr>
  26. <tr>
  27. <td><font face="sans-serif, Arial" font color="white">Member No</td>
  28. <td>:</td>
  29. <td><input name="Member_No" type="int" id="Member_No" value="from below"></td>
  30. </tr>
  31. <tr>
  32. <td><font face="sans-serif, Arial" font color="white">Surname</td>
  33. <td>:</td>
  34. <td><input name="Surname" type="varchar" id="Surname"></td>
  35. </tr>
  36. <tr>
  37. <td><font face="sans-serif, Arial" font color="white">Forename</td>
  38. <td>:</td>
  39. <td><input name="Forename" type="varchar" id="Forename"></td>
  40. </tr>
  41. <tr>
  42. <td><font face="sans-serif, Arial" font color="white">Address</td>
  43. <td>:</td>
  44. <td><textarea name="Address" cols="16" row="4" id="Address"></textarea></td>
  45. </tr>
  46. <tr>
  47. <td><font face="sans-serif, Arial" font color="white">DOB</td>
  48. <td>:</td>
  49. <td><input name="DOB" type="date" id="DOB" value="YYYY-MM-DD"></td>
  50. </tr>
  51. <td colspan="3" align="center"><input type="submit" name="Submit" value="Finish"></td>
  52. </tr>
  53. </table>
  54. </form>
  55. </td>
  56. </tr>
  57. </table>
  58. <br><br>
  59. <br><br>
  60. <table align="right" width="600" border="0" cellspacing="0" cellpadding="3" >
  61. <tr>
  62. <td align="center" width="30%"><b><u><font face="sans-serif, Arial" font color="white">Member No</b></u></td>
  63. <td align="center" width="40%"><b><u><font face="sans-serif, Arial" font color="white">Membership Start Date</b></u></td>
  64. <td align="center" width="30%"><b><u><font face="sans-serif, Arial" font color="white">Membership No</b></u></td>
  65. </tr>
  66. </table>
  67. </body
  68. </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.

Reply
TheServant's Avatar
TheServant
Needs Regular Fix
441 Posts
March 27th, 2008
10:08 PM
#5

Re: Stopping an empty form being submitted
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.

Reply
flexsingh's Avatar
flexsingh
Newbie
17 Posts
March 27th, 2008
10:16 PM
#6

Re: Stopping an empty form being submitted
Quote:
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.

Reply
TheServant's Avatar
TheServant
Needs Regular Fix
441 Posts
March 27th, 2008
10:23 PM
#7

Re: Stopping an empty form being submitted
Quote:
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.

Reply
ronverdonk's Avatar
ronverdonk
Moderator
4,138 Posts
March 27th, 2008
11:42 PM
#8

Re: Stopping an empty form being submitted
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.
Code: ( text )
  1. <?php
  2. // this starts the session
  3. session_start();
  4. $error='';
  5. if (isset($_POST['Submit'])) {
  6.    // check the passed values   
  7.    if (!isset($_POST['Member_No']) OR $_POST['Member_No'] == '' OR
  8.        !isset($_POST['Surname'])   OR $_POST['Surname']   == '' OR
  9.        !isset($_POST['Forename'])  OR $_POST['Forename']  == '' OR
  10.        !isset($_POST['Address'])   OR $_POST['Address']   == '' OR
  11.        !isset($_POST['DOB'])       OR $_POST['DOB']       == '' )   {
  12.       $error="Error, you must fill in all fields in this form";
  13.    }
  14.    else {   
  15.       $db_name="project"; // Database name
  16.       $tbl_name="member"; // Table name
  17.  
  18.       // Connect to server and select database.
  19.       mysql_connect($_SESSION['host'], $_SESSION['username'], $_SESSION['password'])or die("cannot connect");
  20.       mysql_select_db("$db_name")or die("cannot select DB");
  21.  
  22.       // Get values from form
  23.       $memberno=$_POST['Member_No'];
  24.       $surname=$_POST['Surname'];
  25.       $forename=$_POST['Forename'];
  26.       $address=$_POST['Address'];
  27.       $dob=$_POST['DOB'];
  28.  
  29.       // Insert data into mysql
  30.       $sql="INSERT INTO $tbl_name(Member_No, Surname, Forename, Address, DOB)VALUES('$memberno', '$surname', '$forename', '$address', '$dob')";
  31.       $result=mysql_query($sql)
  32.          or die("INSERT error: ".mysql_error());
  33.  
  34.       // if successfully insert data into database, displays message "Successful".
  35.       echo "Successfully inserted in database";
  36.       // close connection
  37.       mysql_close();
  38.       exit;     
  39.   }
  40. if ($error > '')
  41.    echo "<span style='color:red;'>$error</span>";
  42. } // END if submitted
  43. ?>
  44. <html>
  45. <br>
  46. <br>
  47. <head>
  48. <h1><font face="sans-serif, Arial" color="black">Add Member Page!</h1>
  49. </head>
  50. <br>
  51. <br>
  52. <br>
  53. <br>
  54. <br>
  55. <br>
  56. <br>
  57. <br
  58. <body background="main background1.jpg" link="blue" vlink="blue">
  59. <table width="350" border="0" align="center" cellpadding="0" cellspacing="1">
  60. <tr>
  61. <td>
  62. <form name="update member form" method="post" action="<?php $_SERVER['PHP_SELF'] ?>">
  63. <table width="100%" border="0" cellspacing="1" cellpadding="3">
  64. <tr>
  65. <td colspan="3" align="center"><strong><font face="sans-serif, Arial" color="black">Insert Data Into mySQL Database </strong></td>
  66. </tr>
  67. <tr>
  68. <td><font face="sans-serif, Arial" color="black">Member No</td>
  69. <td>:</td>
  70. <td><input name="Member_No" type="int" id="Member_No" value="from below"></td>
  71. </tr>
  72. <tr>
  73. <td><font face="sans-serif, Arial" color="black">Surname</td>
  74. <td>:</td>
  75. <td><input name="Surname" type="varchar" id="Surname" value="<?php echo (isset($_POST['Surname'])) ? $_POST['Surname'] : ""; ?>"></td>
  76. </tr>
  77. <tr>
  78. <td><font face="sans-serif, Arial" color="black">Forename</td>
  79. <td>:</td>
  80. <td><input name="Forename" type="varchar" id="Forename" value="<?php echo (isset($_POST['Forename'])) ? $_POST['Forename'] : ""; ?>"></td>
  81. </tr>
  82. <tr>
  83. <td><font face="sans-serif, Arial" color="black">Address</td>
  84. <td>:</td>
  85. <td><textarea name="Address" cols="16" rows="4" id="Address"><?php echo (isset($_POST['Address'])) ? $_POST['Address'] : ""; ?></textarea></td>
  86. </tr>
  87. <tr>
  88. <td><font face="sans-serif, Arial" color="black">DOB YYYY-MM-DD</td>
  89. <td>:</td>
  90. <td><input name="DOB" type="date" id="DOB" value="<?php echo (isset($_POST['date'])) ? $_POST['date'] : ""; ?>"></td>
  91. </tr>
  92. <td colspan="3" align="center"><input type="submit" name="Submit" value="Finish"></td>
  93. </tr>
  94. </table>
  95. </form>
  96. </td>
  97. </tr>
  98. </table>
  99. <br><br>
  100. <br><br>
  101. <table align="right" width="600" border="0" cellspacing="0" cellpadding="3" >
  102. <tr>
  103. <td align="center" width="30%"><b><u><font face="sans-serif, Arial" color="black">Member No</b></u></td>
  104. <td align="center" width="40%"><b><u><font face="sans-serif, Arial" color="black">Membership Start Date</b></u></td>
  105. <td align="center" width="30%"><b><u><font face="sans-serif, Arial" color="black">Membership No</b></u></td>
  106. </tr>
  107. </table>
  108. </body>
  109. </html>
Good luck!

Ronald

Reply
flexsingh's Avatar
flexsingh
Newbie
17 Posts
March 28th, 2008
12:44 PM
#9

Re: Stopping an empty form being submitted
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 : March 28th, 2008 at 04:30 PM. Reason: code out post too long
Reply
flexsingh's Avatar
flexsingh
Newbie
17 Posts
March 28th, 2008
03:52 PM
#10

Re: Stopping an empty form being submitted
It has worked fine thank you very much.

Reply
ronverdonk's Avatar
ronverdonk
Moderator
4,138 Posts
March 28th, 2008
04:31 PM
#11

Re: Stopping an empty form being submitted
Quote:
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

Reply
Reply
Not the answer you were looking for? Post your question . . .
169,969 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Top PHP Forum Contributors