473,657 Members | 2,727 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="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.
Mar 27 '08 #1
10 2226
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">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.
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="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
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

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

Similar topics

5
7903
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
2027
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 some webpages, any page not assigned to that template is having its template reset to the default. I'm not sure why. I've been assuming that with checkboxes nothing gets submitted to the server if the checkbox is left unchecked. Isn't that so? ...
6
1378
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 even though incomplete. This makes debugging difficult. Can any of the grey matter here advise on how to get round this, any tips etc. zzapper (vim, cygwin, wiki & zsh) --
5
2255
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 data is being posted (for example, opening www.foo.com/xyz.php directly when a form action is xyz.php) 2. When the user clicks the submit button without entering anything.
7
5540
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 textbox control with form posting to itself.
9
7026
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 another ASP page. The other page retrieves the value with Request.Form('album'); On Firefox this works fine every time. On IE6, I always get nothing. I'm pretty sure it's the posting side that is at fault, so that's what I've shown here. Oh, I tried...
7
14687
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. When I load and reload the page I want it to be lika a new session (only show the form not the data). As it is now isset($_POST and isset($_POST is always set and the data is always shown. Except when a click the link "Please try again" (hich is a...
26
3889
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 submitted, then echoes a message. If there were problems with the submitted data the message says: "a problem occurred with your data. Click here to come back to the form" (page_A.php). When the user comes back, he finds the fields white.
9
6120
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 what information I put it always add records to database when I click on submit. What can I do to make sure user will not be able to add records to database until he enters the right information? I am posting my code for you guys to have a look...
0
8394
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8306
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6164
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4304
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
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 we have to send another system
2
1955
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1615
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.