Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 27th, 2006, 9:45 AM   #1
TecBrain
Hobbyist Programmer
 
Join Date: Sep 2004
Location: Cyprus
Posts: 147
Rep Power: 5 TecBrain is on a distinguished road
Form Validation

Am trying to check whether the input is text or not, so what i did is creating a function and return the value false or true, is this legal, if no then how can i go around this problem.

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) 
{
	$countryname= $_POST['countryname'];
	$currency = $_POST['currency'];
	$image = $fileName = $_FILES['uploaded']['name'];
	
	function isLetters($countryname) 
	{
  	return !preg_match ('/[^A-z]/', $countryname);
	}
	
	isLetters($countryname);
	
	if ((is_float($currency)) || (!$countryname))
	{
		echo "error";
		exit();
	}
	else if (empty($countryname) || empty($currency))
	{
		echo "error2";
		exit();
	}
	
	else
	{
	$query = "INSERT INTO countries (countryname, currency, imgpath) VALUES ('$countryname', '$currency', '$image')";
	mysql_select_db($database, $dbcnx);
	mysql_query($query) or die('Error, insert query failed');
	}
}
__________________
Personal Portfolio
TecBrain Support Forum
Linux VS Windows ... Dont Even Think of it ..
Distribution: Slackware
if (OS==Linux) return success
There are 10 kinds of people, those who can read binary numbers and those who can't.
TecBrain is offline   Reply With Quote
Old Feb 27th, 2006, 11:58 AM   #2
Lich
Professional Programmer
 
Lich's Avatar
 
Join Date: May 2005
Location: Detroit
Posts: 254
Rep Power: 4 Lich is on a distinguished road
Send a message via AIM to Lich Send a message via MSN to Lich
how would you put something other than text into a form?
__________________
--John Cruz
Web Developer
www.cruzweb.net
Lich is offline   Reply With Quote
Old Feb 27th, 2006, 12:01 PM   #3
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Depends on how one defines 'text'....
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Feb 27th, 2006, 1:06 PM   #4
TecBrain
Hobbyist Programmer
 
Join Date: Sep 2004
Location: Cyprus
Posts: 147
Rep Power: 5 TecBrain is on a distinguished road
This is what I have done, I did create a function to validate my countryName, not sure where i have done my mistake though...

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) 
{
	$countryname= $_POST['countryname'];
	$currency = $_POST['currency'];
	$image = $fileName = $_FILES['uploaded']['name'];
	
	function isLetters($countryname) 
	{
	  	if (preg_match('|^[a-z]+$|i', $countryname))
	    	
	    	return true;
	  	else
	   		return false;
	}
	
		if ((isLetters($countryname)) && (is_float($currency)))
		{
		$query = "INSERT INTO countries (countryname, currency, imgpath) VALUES ('$countryname', '$currency', '$image')";
		mysql_select_db($database, $dbcnx);
		mysql_query($query) or die('Error, insert query failed');
		}
		else
		{
			echo "error";
			exit();
		}
}
__________________
Personal Portfolio
TecBrain Support Forum
Linux VS Windows ... Dont Even Think of it ..
Distribution: Slackware
if (OS==Linux) return success
There are 10 kinds of people, those who can read binary numbers and those who can't.
TecBrain is offline   Reply With Quote
Old Feb 27th, 2006, 5:56 PM   #5
Lich
Professional Programmer
 
Lich's Avatar
 
Join Date: May 2005
Location: Detroit
Posts: 254
Rep Power: 4 Lich is on a distinguished road
Send a message via AIM to Lich Send a message via MSN to Lich
What I would do is create a has out of the country names and generate a dropdown menu based on that.

For example, say we have a small hash

$countries array(0 => "United States", 1 => "United Kingdom", 2 => "Algeria")

then make a foreach loop to do the select box, with the number being the value and the country what it displays. So like
<option value="0">United States</option> except the loop will generate it from the hash.

So then, it sets the post variable to the form action script, so you'll get a 1, a 2, or a 3. You can validate it by saying
$country = $_POST[country]
$country = $countries[$country]

That way not only will it validate for text, but it will validate to make sure that it's a real country from a list that you have pre-defined. You can also easily do this with a SQL database instead of a hash.
__________________
--John Cruz
Web Developer
www.cruzweb.net
Lich is offline   Reply With Quote
Old Feb 28th, 2006, 4:37 AM   #6
TecBrain
Hobbyist Programmer
 
Join Date: Sep 2004
Location: Cyprus
Posts: 147
Rep Power: 5 TecBrain is on a distinguished road
Sounds like a good idea, but i will need some help on it, this is what i have done, I did create an array in my config.php but how can i use this array in my html code, to list all the countries....
$ArrCountry = array(0=>"USA", 1=>"JAP", 2=>"EURO", 3=>"CYP");
__________________
Personal Portfolio
TecBrain Support Forum
Linux VS Windows ... Dont Even Think of it ..
Distribution: Slackware
if (OS==Linux) return success
There are 10 kinds of people, those who can read binary numbers and those who can't.
TecBrain is offline   Reply With Quote
Old Feb 28th, 2006, 11:56 AM   #7
Lich
Professional Programmer
 
Lich's Avatar
 
Join Date: May 2005
Location: Detroit
Posts: 254
Rep Power: 4 Lich is on a distinguished road
Send a message via AIM to Lich Send a message via MSN to Lich
This is just off the top of my head, but this is the basic jist of it
[PHP]echo "<select name=\"country\">";
foreach($ArrCountry as $value => $country)
{
echo <option value=\"$value\">$country</option>";
}
echo "</select>";[/PHP]
__________________
--John Cruz
Web Developer
www.cruzweb.net
Lich is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:34 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC