Page 1 of 1

making a field required

Posted: Thu Feb 05, 2009 2:02 pm
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

Re: making a field required

Posted: Thu Feb 05, 2009 2:09 pm
by mickeyunderscore
'error' is not a PHP function. Unless you defined it yourself, this is the source of your problem.

Re: making a field required

Posted: Thu Feb 05, 2009 3:24 pm
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");
}

Re: making a field required

Posted: Thu Feb 05, 2009 5:20 pm
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.

Re: making a field required

Posted: Thu Feb 05, 2009 6:28 pm
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

Re: making a field required

Posted: Thu Feb 05, 2009 6:59 pm
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.

Re: making a field required

Posted: Thu Feb 05, 2009 7:05 pm
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

Re: making a field required

Posted: Thu Feb 05, 2009 7:55 pm
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.