Jump to content

[SOLVED] $GET assoc array and validation


scottybwoy

Recommended Posts

Hi Billys

I am trying to make an assosiative array of $_GET / $_POST values, is this poss.  May it be possible to get the complete url and extract the $k = $v and put them into an associative array?  Could someonr point me in the right direction of functions to use, or if my method asked above is viable.  Thanx
Link to comment
https://forums.phpfreaks.com/topic/31100-solved-get-assoc-array-and-validation/
Share on other sites

Thanks Fellas,

Sorry I was very tired yesterday, had 4hrs sleep over the w/e! and was also busy at work.  Just letting you know cos with a fresh head this morning, I noticed that I had confused myself by looking at a wrong function that I had already completed and tried to make it do something else, lol, sorry for taking your time.

However let me go on to what I am actually trying to do.  I want to create server side validation for my form.  I already have client side validation via JavaScript.  Am I going the right way about it, cos it seems a bit long, here's what I have so far :

[code]
<?php
function insertCust($data_array)
{
global $company, $custId, $data_array, $template;

$company = $data_array['company'];

foreach ($data_array as $v)
{
//take out whitespace
str_replace('/^\s+|\s+$/g', '', $v);
}

if (empty($company))
{
echo "You forgot to insert customer name!";
$template = CUST_PAGE;
break;
} else if (!empty($company)) {
$data_array['custId'] = $this->makeCustId($company);
}

if (empty($data_array['tel']))
{
echo "You need to insert at least a main telephone number!";
$data_array['custId'] = $this->makeCustId($company);
$template = CUST_PAGE;
break;
} else if (!empty($data_array['tel'])) {
('/^\+?[0-9 ()-]+[0-9]$/');
}

if (empty($data_array['addLine1']))
{
echo "You need to insert at least a building name and street!";
$data_array['custId'] = $this->makeCustId($company);
$template = CUST_PAGE;
break;
}

if (empty($data_array['postcode']))
{
echo "C'mon how are we meant to know where they are without a postcode?";
$data_array['custId'] = $this->makeCustId($company);
$template = CUST_PAGE;
break;
}

if (empty($data_array['type']))
{
echo "It'll make your life a whole lot easier if we know what type of company they are, don't you think?";
$data_array['custId'] = $this->makeCustId($company);
$template = CUST_PAGE;
break;
}

if (empty($data_array['pay']))
{
echo "Now how we gonna pay you if they don't pay us?";
$data_array['custId'] = $this->makeCustId($company);
$template = CUST_PAGE;
break;
}

foreach ($data_array as $k => $v)
{
$_GET[$k];
}
}

function addClient()
{
global $clientId, $custId, $data_array, $template;

$custId = $_GET['custId'];
$company = $_GET['company'];

if (empty($company) && empty($custId))
{
echo "Dying";
die("You forgot to select a company!");
} else {

$this->makeClientId($custId);

$data_array = array("company" => $company, "clientId" => $clientId);
$template = CLIENT_PAGE;
}
}
?>
[/code]
Hmm, I tidied this up a bit today, but it's still rather long and it doesn't work, when calling it, it just returns with the enties back in it and no validation messages at all.  Here's the validation code (not quite complete) :
[code]
<?php
function insertCust($data_array)
{
global $company, $custId, $data_array, $template;



foreach ($data_array as $k => $v)
{
//take out trailing whitespace
str_replace('/^\s+|\s+$/g', '', $v);

if (empty($data_array[$k]))
{
$validate[$k] = FALSE;
} else {
$validate[$k] = TRUE;
}

return $validate;
}

if ($validate['company'] == FALSE)
{
$invalid = array(1 => "You forgot to insert customer name!");
} else if ($validate['company'] == TRUE) {
$company = $data_array['company'];
$data_array['custId'] = $this->makeCustId($company);
}

if ($validate['tel'] == FALSE)
{
$invalid = array("You need to insert at least a main telephone number!");
} else if ($validate['tel'] == TRUE) {
$email = $data_array['email'];
preg_match('/^\+?[0-9 ()-]+[0-9]$/', $email);
}

if ($validate['addLine1'] == FALSE)
{
$invalid = array("You need to insert at least a building name and street!");
} else if ($validate['addLine1'] == TRUE) {
$addLine1 = preg_replace('|[^a-z0-9 ]|i', '', $addLine1);
}

if ($validate['postcode'] == FALSE)
{
$invalid = array("C'mon how are we meant to know where they are without a postcode?");
}

if ($validate['type'] == FALSE)
{
$invalid = array("It'll make your life a whole lot easier if we know what type of company they are, don't you think?");
} elseif ($validate['type'] == TRUE) {
$type = $data_array['type'];

$valid = $this->recordCheck('cusType', 'tpName', $type);

if ($valid == FALSE)
{
$invalid = array("Sorry you have selected an incorect Type of Customer");
}
}

if ($validate['pay'] == FALSE)
{
$invalid = array("Now how we gonna pay you if they don't pay us?");
} elseif ($validate['pay'] == TRUE) {
$type = $data_array['pay'];

$valid = $this->recordCheck('cusPay', 'pyName', $type);

if ($valid == FALSE)
{
$invalid = array("Sorry you have selected an incorect Payment Type for Customer");
}
}

if (isset($invalid))
{
print_r($invalid);
$template = CUST_PAGE;
} else {
//insert $data_array into table
echo "info is correct";
$template = CUST_PAGE;
}
}
?>
[/code]
Thanks for taking a look ;)
1) At the begining of the function, write $invalid=array();
2) Instead of writing every time: "$invalid = array("Some error here");", write "$invalid[] = "Some error here";".
3) Instead of "if (isset($invalid))" write "if (empty($invalid))".

I think everything will be fine this way :)

Orio.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.