473,503 Members | 2,166 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Stopping an empty form being submitted

flexsingh
17 New Member
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.
Mar 27 '08 #1
10 2208
ronverdonk
4,258 Recognized Expert Specialist
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
Mar 27 '08 #2
TheServant
1,168 Recognized Expert Top Contributor
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.
Mar 27 '08 #3
flexsingh
17 New Member
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.
Mar 27 '08 #4
TheServant
1,168 Recognized Expert Top Contributor
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.
Mar 27 '08 #5
flexsingh
17 New Member
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.
Mar 27 '08 #6
TheServant
1,168 Recognized Expert Top Contributor
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.
Mar 27 '08 #7
ronverdonk
4,258 Recognized Expert Specialist
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
Mar 27 '08 #8
flexsingh
17 New Member
Ow thank you soo very much, words can not show how thankful I am, I will try this right away thank you again :D
Mar 28 '08 #9
flexsingh
17 New Member
It has worked fine thank you very much.
Mar 28 '08 #10
ronverdonk
4,258 Recognized Expert Specialist
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
Mar 28 '08 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

5
7889
by: Batezz | last post by:
I have created a form (below) How do I stop it redirecting to another page (productsearchresults.php) when form is submitted if both the fields are blank? Any help appreciated. Batezz
3
2013
by: lkrubner | last post by:
A friend using my software called me and said some templates were being reset to default when he submitted a form. I'm trying to track down the problem. It looks like when he assigns a template to...
6
1358
by: zzapper | last post by:
Hi, If I make a mistake designing a JavaScript Form Checker, ie get the name (or case) of a form Field wrong, JavaScript seems to default to immediately returning true and the form is submitted...
5
2246
by: Thejo | last post by:
Hi all, I started programming in PHP recently and have a query about empty $_POST arrays. I can see two scenarios when this could happen. 1. When some tries to directly load the page to which...
7
5536
by: al | last post by:
Greetings all, I use request.form("textbox1") to get data back to page, dim str as string str = request.form("textbox1").tostring But str is always empty after refresh???? I use asp.net...
9
7012
by: Dave | last post by:
Hi, I've been trawling the web for answer to my problem with no luck although I'm hardly alone it seems! Below is the generated source for an ASP page that posts a value called 'album' to...
7
14665
by: Anette | last post by:
Hi, I'm very new at PHP. I've tried for a long time now to get a page that has a form in a certain way: 1. When form is submitted the data in a table should be writted on the same page. 2....
26
3867
by: pepper.gabriela | last post by:
Hello, a stupid question but... page_A.php is a page with a form. The user inserts text in four fields, then he clicks a submit button. The data goes to page_B.php: this page controls the data...
9
6103
by: Dhiru1009 | last post by:
Hi guys, I am trying to build a user registration form using PHP and MYSQL but encountring a problem. When I click on submit with empty fields it adds records to database also it doesn't matter...
0
7093
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7287
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7349
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7467
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5022
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4688
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3177
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1521
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
399
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.