![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Sep 2004
Location: Cyprus
Posts: 147
Rep Power: 5
![]() |
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. |
|
|
|
|
|
#2 |
|
Professional Programmer
|
how would you put something other than text into a form?
|
|
|
|
|
|
#3 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: Sep 2004
Location: Cyprus
Posts: 147
Rep Power: 5
![]() |
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. |
|
|
|
|
|
#5 |
|
Professional Programmer
|
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. |
|
|
|
|
|
#6 |
|
Hobbyist Programmer
Join Date: Sep 2004
Location: Cyprus
Posts: 147
Rep Power: 5
![]() |
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. |
|
|
|
|
|
#7 |
|
Professional Programmer
|
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] |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|