Connecting Tech Pros Worldwide Forums | Help | Site Map

Stopping an empty form being submitted

flexsingh's Avatar
Newbie
 
Join Date: Mar 2008
Posts: 17
#1: Mar 27 '08
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="project"; // Database name
$tbl_name="member"; // 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=$_POST['Member_No'];
$surname=$_POST['Surname'];
$forename=$_POST['Forename'];
$address=$_POST['Address'];
$dob=$_POST['DOB'];


// Insert data into mysql
$sql="INSERT INTO $tbl_name(Member_No, Surname, Forename, Address, DOB)VALUES('$memberno', '$surname', '$forename', '$address', '$dob')";
$result=mysql_query($sql);

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

header('Location: blank.php');
}

else {
echo "ERROR";
}

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

Any help or ideas would be very welcome.

Thank you in advanced.

ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#2: Mar 27 '08

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 [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
TheServant's Avatar
Expert
 
Join Date: Feb 2008
Location: Australia
Posts: 914
#3: Mar 27 '08

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.
flexsingh's Avatar
Newbie
 
Join Date: Mar 2008
Posts: 17
#4: Mar 27 '08

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 [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">Add Member Page!</h1>
</head>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>

<body background="main 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="updatemember_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">Insert Data Into mySQL Database </strong></td>
</tr>
<tr>
<td><font face="sans-serif, Arial" font color="white">Member 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">Surname</td>
<td>:</td>
<td><input name="Surname" type="varchar" id="Surname"></td>
</tr>
<tr>
<td><font face="sans-serif, Arial" font color="white">Forename</td>
<td>:</td>
<td><input name="Forename" type="varchar" id="Forename"></td>
</tr>
<tr>
<td><font face="sans-serif, Arial" font color="white">Address</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">DOB</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">Member No</b></u></td>
<td align="center" width="40%"><b><u><font face="sans-serif, Arial" font color="white">Membership Start Date</b></u></td>
<td align="center" width="30%"><b><u><font face="sans-serif, Arial" font color="white">Membership 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.
TheServant's Avatar
Expert
 
Join Date: Feb 2008
Location: Australia
Posts: 914
#5: Mar 27 '08

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.
flexsingh's Avatar
Newbie
 
Join Date: Mar 2008
Posts: 17
#6: Mar 27 '08

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.
TheServant's Avatar
Expert
 
Join Date: Feb 2008
Location: Australia
Posts: 914
#7: Mar 27 '08

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.
ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#8: Mar 28 '08

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. [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="project"; // Database name
$tbl_name="member"; // 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=$_POST['Member_No'];
$surname=$_POST['Surname'];
$forename=$_POST['Forename'];
$address=$_POST['Address'];
$dob=$_POST['DOB'];

// Insert data into mysql
$sql="INSERT INTO $tbl_name(Member_No, Surname, Forename, Address, DOB)VALUES('$memberno', '$surname', '$forename', '$address', '$dob')";
$result=mysql_query($sql)
or die("INSERT error: ".mysql_error());

// if successfully insert data into database, displays message "Successful".
echo "Successfully inserted in database";
// close connection
mysql_close();
exit;
}
if ($error > '')
echo "<span style='color:red;'>$error</span>";
} // END if submitted
?>
<html>
<br>
<br>
<head>
<h1><font face="sans-serif, Arial" color="black">Add Member Page!</h1>
</head>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br
<body background="main 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">Insert Data Into mySQL Database </strong></td>
</tr>
<tr>
<td><font face="sans-serif, Arial" color="black">Member 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">Surname</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">Forename</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">Address</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">DOB 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">Member No</b></u></td>
<td align="center" width="40%"><b><u><font face="sans-serif, Arial" color="black">Membership Start Date</b></u></td>
<td align="center" width="30%"><b><u><font face="sans-serif, Arial" color="black">Membership No</b></u></td>
</tr>
</table>
</body>
</html>[/php]Good luck!

Ronald
flexsingh's Avatar
Newbie
 
Join Date: Mar 2008
Posts: 17
#9: Mar 28 '08

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
flexsingh's Avatar
Newbie
 
Join Date: Mar 2008
Posts: 17
#10: Mar 28 '08

re: Stopping an empty form being submitted


It has worked fine thank you very much.
ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#11: Mar 28 '08

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