Today's Posts Follow Us On Twitter! TFL Members on Twitter  
Forum search: Advanced Search  
Navigation
Marketplace
  Members Login:
Lost password?
  Forum Statistics:
Forum Members: 24,254
Total Threads: 80,792
Total Posts: 566,472
There are 1648 users currently browsing (tf).
 
  Our Partners:
 
  TalkFreelance     Design and Development     Programming     PHP and MySQL :

If a feild is empty... (noob)

Thread title: If a feild is empty... (noob)
Reply    
    Thread tools Search this thread Display Modes  
05-14-2009, 09:19 PM
#1
PicoDeath is offline PicoDeath
Status: Junior Member
Join date: Jun 2007
Location: United Kingdom
Expertise:
Software:
 
Posts: 63
iTrader: 0 / 0%
 

PicoDeath is on a distinguished road

  Old  If a feild is empty... (noob)

It's been quite sometime since I last tried some PHP and I'm having an issue..

I've forgotten how to make sure a feild has something entered into it, if it's empty I'd like it to say 'Blah is empty, please go back and retry'.

Here's my code.

Code:
<?php if(isset($_POST['send'])) {

#      elseif ($_REQUEST[ 'name' ]) == ""
#	echo "Please go back and enter a name.";

   include("dbconnect.php");
   $name = $_REQUEST['name'];
   $gender = $_REQUEST['gender'];
   $dob = $_REQUEST['dob'];
   $telephone = $_REQUEST['telephone'];
   $email = $_REQUEST['email'];
   $comment = $_REQUEST['comment'];
   $ip = $_SERVER['REMOTE_ADDR'];
   $browser = $_SERVER['HTTP_USER_AGENT'];
   $port = $_SERVER['SERVER_PORT'];
   
	echo $name;
	echo $gender;
	echo $dob;
	echo $telephone;
	echo $email;
	echo $comment;
	echo $ip;
	echo $browser;

	$query="INSERT INTO feedback VALUES ('$name', '$gender', '$dob', '$telephone', '$email', '$ip', '$browser', '$port', '$comment')";
	mysql_query($query);
	echo mysql_error();

} else {
    
	echo ('	<center>');
  	echo (' <form action="'.htmlspecialchars($_SERVER['PHP_SELF']).'" method="post">');
	echo ('	<table cellpadding="3px" cellspacing="0px" border="0px">');
	echo ('	<tr><td>Name:</td><td> <input type="Text" name="name" size="25"></td></tr>');
	echo ('	<tr><td>Gender:</td><td><select name="gender"><option value="male">Male</option><option value="female">Female</option></select></td></tr>');
	echo ('	<tr><td>D.O.B:</td><td> <input type="Text" name="dob" size="25"></td></tr>');
	echo ('	<tr><td>Telephone:</td><td> <input type="Text" name="telephone" size="25"></td></tr>');
	echo ('	<tr><td>Email:</td><td> <input type="Text" name="email" size="25"></td></tr>');
	echo ('	<tr><td>Comment:</td><td> <textarea cols="25" rows="5" name="comment"></textarea></td></tr>');
	echo ('	<tr><td><input type="submit" name="send" value="Submit"></td><td><input type="reset" name="reset" value="Reset"></td></tr>');
	echo ('	</table>');
	echo ('	</form>');
	echo ('	</center>');

}

?>
Thanks.

Reply With Quote
05-15-2009, 01:52 PM
#2
Holland is offline Holland
Status: I'm new around here
Join date: May 2009
Location:
Expertise:
Software:
 
Posts: 3
iTrader: 0 / 0%
 

Holland is on a distinguished road

  Old

Code:
<?php
if(isset($_POST['send']))
{
 if($_POST['name']=="" || $_POST['gender']=="" || $_POST['dob']=="" || $_POST['email']=="" || $_POST['telephone']=="" || $_POST['comment']=="")
 {
  echo 'Please make sure you entered something in every field. 
 }
 else
 {
  include("dbconnect.php");
  echo $_POST['name'];
  echo $_POST['gender'];
  echo $_POST['dob'];
  echo $_POST['telephone'];
  echo $_POST['email'];
  echo $_POST['comment'];
  echo $_SERVER['REMOTE_ADDR'];
  echo $_SERVER['HTTP_USER_AGENT'];
  echo $_SERVER['SERVER_PORT'];

  $query="INSERT INTO feedback VALUES ('$name', '$gender', '$dob', '$telephone', '$email', '$ip', '$browser', '$port', '$comment')";
  mysql_query($query);
  echo mysql_error();
 }
} 
else 
{
 echo ' <center>';
 echo ' <form action='.htmlspecialchars($_SERVER['PHP_SELF']).' method=post>';
 echo ' <table cellpadding=3px cellspacing=0px border=0px>';
 echo ' <tr><td>Name:</td><td> <input type=Text name=name size=25></td></tr>';
 echo ' <tr><td>Gender:</td><td><select name=gender><option value=male>Male</option><option value=female>Female</option></select></td></tr>';
 echo ' <tr><td>D.O.B:</td><td> <input type=Text name=dob size=25></td></tr>';
 echo ' <tr><td>Telephone:</td><td> <input type=Text name=telephone size=25></td></tr>';
 echo ' <tr><td>Email:</td><td> <input type=Text name=email size=25></td></tr>';
 echo ' <tr><td>Comment:</td><td> <textarea cols=25 rows=5 name=comment></textarea></td></tr>';
 echo ' <tr><td><input type=submit name=send value=Submit></td><td><input type=reset name=reset value=Reset></td></tr>';
 echo ' </table>';
 echo ' </form>';
 echo ' </center>';
}

?>
I suggest you look through the basics.

Reply With Quote
05-27-2009, 10:34 PM
#3
pixolia is offline pixolia
Status: I'm new around here
Join date: May 2009
Location: Sacramento, CA
Expertise:
Software:
 
Posts: 14
iTrader: 0 / 0%
 

pixolia is on a distinguished road

  Old

I think the better practice would be to detect spaces or 'null'. Null = completely empty text field and spaces could be entered for filler (Assuming the user entering information into these text fields was a little more intelligent than not)

Reply With Quote
05-29-2009, 02:54 AM
#4
travmanx is offline travmanx
Status: I'm new around here
Join date: May 2009
Location:
Expertise:
Software:
 
Posts: 2
iTrader: 0 / 0%
 

travmanx is on a distinguished road

  Old

As pixolio stated above, using the NULL is the best way to approach this.

Here are 3 solutions

Code:
if($_POST['name']==NULL || $_POST['gender']==NULL || $_POST['dob']==NULL || $_POST['email']==NULL || $_POST['telephone']==NULL || $_POST['comment']==NULL)
Here's how I would approach:
Code:
if(!$_POST){ //Saves the user computer time (not alot)
if(!$_POST['name'] || !$_POST['gender'] || !$_POST['dob'] || $_POST['email'] || $_POST['telephone'] || !$_['comment']){
echo 'Some or all the fields were left blank';
}
echo 'All fields left blank';
}
and finally probably the most simple way but not sure how effective against attacks and vulns

Code:
$array = $_POST; //since $_POST is an array
foreach($array as $k => $v){
if(!$v) { //You can use $v = NULL or $v = ''
return('A required field was left blank');
}
Hope this helps.

Reply With Quote
06-10-2009, 03:42 AM
#5
workdraft is offline workdraft
workdraft's Avatar
Status: I'm new around here
Join date: Jun 2009
Location: pi
Expertise:
Software:
 
Posts: 8
iTrader: 0 / 0%
 

workdraft is on a distinguished road

  Old

i suggest you validate the data first before submitting. use javascript, or perhaps a js framework such as jquery, prototype or anything to validate if required fields are filled in.

Reply With Quote
Reply    


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

  Posting Rules  
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump:
 
  Contains New Posts Forum Contains New Posts   Contains No New Posts Forum Contains No New Posts   A Closed Forum Forum is Closed