making a field required

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
phpstudent
Forum Newbie
Posts: 10
Joined: Wed Feb 04, 2009 11:54 pm

making a field required

Post by phpstudent »

I have a simple form to collect name and email, but i would like to have the fields required. I have already started to get a bunch of empty records in my database. I have tried a couple things, but i keep getting errors.

here is the code as it works, but with nothing required:

Code: Select all

<?php
$con = mysql_connect("localhost","username","password"); 
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("dbname", $con); //Replace with your MySQL DB Name
$name=mysql_real_escape_string($_POST['name']); //This value has to be the same as in the HTML form file
$email=mysql_real_escape_string($_POST['email']); //This value has to be the same as in the HTML form file
$sql="INSERT INTO freeemails (name,email) VALUES ('$name','$email')"; /*form_data is the name of the MySQL table where the form data will be saved.
name and email are the respective table fields*/
if (!mysql_query($sql,$con)) {
 die('Error: ' . mysql_error());
}
mysql_close($con);
?>
 
here is one example of a way i tried to make it required for name, but im missing something simple im sure... any help would be appreciated:

bad code:

Code: Select all

<?php
$con = mysql_connect("localhost","username","password"); 
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("dbname", $con); //Replace with your MySQL DB Name
$name=mysql_real_escape_string($_POST['name']); //This value has to be the same as in the HTML form file
$email=mysql_real_escape_string($_POST['email']); //This value has to be the same as in the HTML form file
$sql="INSERT INTO freeemails (name,email) VALUES ('$name','$email')"; /*form_data is the name of the MySQL table where the form data will be saved.
name and email are the respective table fields*/
if (!isset($_POST['name']) || $_POST['name'] == ''){
error('You did not enter anything in the name field.');
}
if (!mysql_query($sql,$con)) {
 die('Error: ' . mysql_error());
}
mysql_close($con);
?>
 

so the

if (!isset($_POST['name']) || $_POST['name'] == ''){
error('You did not enter anything in the name field.');
}

is obviously wrong, any ideas?

thanks in advance,
michael
Last edited by phpstudent on Thu Feb 05, 2009 2:53 pm, edited 2 times in total.
mickeyunderscore
Forum Contributor
Posts: 129
Joined: Sat Jan 31, 2009 9:00 am
Location: UK

Re: making a field required

Post by mickeyunderscore »

'error' is not a PHP function. Unless you defined it yourself, this is the source of your problem.
phpstudent
Forum Newbie
Posts: 10
Joined: Wed Feb 04, 2009 11:54 pm

Re: making a field required

Post by phpstudent »

for anyone having a similar problem, i was finally able to get it to work by using this:

if (empty($name) || empty($email)) { die("All fields must be filled in. Please go back and try again");
}
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: making a field required

Post by RobertGonzalez »

Are you validating anything outside of it being not empty? Have you looked at what empty() looks for? Just asking because you could still get empty strings in your database (or what appear to be empty strings) if you are not careful. Plus if you are not checking for data types then you might end up with numbers in your email fields or emails in your name fields.
phpstudent
Forum Newbie
Posts: 10
Joined: Wed Feb 04, 2009 11:54 pm

Re: making a field required

Post by phpstudent »

I am brand new to php, so no, i am not validating anything except for if it is empty.

if you have a suggestion on how i can validate that text is actually text, and emails are actually emails i would love to hear your thoughts.

the solution i have right now seems to be working, although i would have much prefered that the script would have just given a pop up message instead of sending to a blank page telling the user to hit their back button and correctly fill in information

thanks
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: making a field required

Post by RobertGonzalez »

Pop ups can be achieved with a little javascript. Look at jQuery. It is the easiest to use library out there (in my opinion).

As for the other stuff, I'd recommend some searching and playing around with some code to get the hang of making fields required.
User avatar
Skoalbasher
Forum Contributor
Posts: 147
Joined: Thu Feb 07, 2008 8:09 pm

Re: making a field required

Post by Skoalbasher »

phpstudent wrote:I am brand new to php, so no, i am not validating anything except for if it is empty.

if you have a suggestion on how i can validate that text is actually text, and emails are actually emails i would love to hear your thoughts.
If you're brand new to PHP, I would stay away from this for just a little bit. When I first started, it took me a little bit to understand what had to be done here. You can go look up Regular Expressions for info on how to validate stuff like that.

I'm sure the guys that chill in this forum could help you with validating email addresses.

viewforum.php?f=38
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: making a field required

Post by califdon »

You can divide the answer to your question into several pieces. As a database issue, you can make columns in your table NOT NULL, which will not accept a new record where there is no value for that column. In your application script, you can either write Javascript code to validate fields before the processing script is even called (my preference, much better user experience) and/or in your processing script you can write php code to further validate fields before writing the new record to the database. This covers the possibility of a hacker bypassing your input form and client-side script, but it means that your user will receive another whole page load, usually not a desirable thing. Depending on how critical security may be for an application, you may do all 3. There are lots of tutorials on validating web form data, using Javascript, Ajax or PHP. I agree with Skoalbasher that it's a fairly advanced task for a brand new beginner. It's also extremely important, though, so my advice would be to tackle some other issues first and when you have a few successful demo scripts working, THEN turn your attention to form data validation. Good luck.
Post Reply