I created 5 functions and I run each one individually, what im trying to do is run each function and have it stop once a condition is reached.
Take a look at
function checkFirstName($input) {
$pattern = '/[a-zA-Z]{2,15}/';
if(preg_match($pattern, $input)) {
//set a SESSION variable
$_SESSION['First_Name'] = $input;
return true;
} else {
//refresh the page
header('Location: index.php?error=1');
}
}
function checkLastName($input) {
$pattern = '/[a-zA-Z]{2,15}/';
if(preg_match($pattern, $input)) {
//set a SESSION variable
$_SESSION['Last_Name'] = $input;
return true;
} else {
//refresh the page
header('Location: index.php?error=2');
}
}
function checkPhone($input) {
$pattern = '/\([0-9]{3}\) [0-9]{3}-[0-9]{4}/';
if(preg_match($pattern, $input)) {
//set a SESSION variable
$_SESSION['Phone_Number'] = $input;
return true;
} else {
//refresh the page
header('Location: index.php?error=3');
}
}
function checkCity($input) {
$pattern = '/[a-zA-Z]{3,20}/';
if(preg_match($pattern, $input)) {
//set a SESSION variable
$_SESSION['City'] = $input;
return true;
} else {
//refresh the page
header('Location: index.php?error=4');
}
}
function checkState($input) {
$pattern = '/[a-zA-Z]{2}/';
if(preg_match($pattern, $input)) {
//set a SESSION variable
$_SESSION['State'] = $input;
return true;
} else {
//refresh the page
header('Location: index.php?error=5');
}
}
//fire off each function
checkFirstName($_POST['First_Name']);
checkLastName($_POST['Last_Name']);
checkPhone($_POST['Phone_Number']);
checkCity($_POST['City']);
checkState($_POST['State']);
Once an error is found in the first function (screenshot) why does all the functions run:?
Off topic:-
I would also question the logic of creating 5 near identical functions, it sort of defeats the object of using functions, to re-use the same code many times.
Iâm sure with a bit of modification you could come up with a single function to do the whole lot.
Is there any html output from the script before the functions are called?
That would do it, header has to come first.
Not really, the idea of the function is not to do everything all in one go, but to be re-used over and over with different variable values on each call.
Sorry itâs a bit late here and I donât have time to go in depth on this, but a hint.
Maybe something that starts like:-
function validae($NameOfFormField, $TypeOfRegexToUseOnIt) {...}
If this were actually an associative array instead of just a bunch of variables, that would be neat.
You could call it with: $RegexArray[$TypeOfRegexToUseOnIt]
As in: if(preg_match($RegexArray[$TypeOfRegexToUseOnIt], $NameOfFormField)) {...}
An example of a similar function:-
function sani($name, $type) {
$regarr = array(
'int' => '0-9',
'bin' => '0-1',
'alph' => 'a-z',
'date' => '0-9A-Z .a-z/',
'text' => 'A-Za-zĂ-Ăż 0-9,.',
'phone' => '0-9 +'
) ;
if(isset($_POST[$name])) {
$name = preg_replace('#[^'.$regarr[$type].']#iu', '', $_POST[$name]);
return $name ;
}
else {
// Someone is being naughty, what shall I do??
exit() ; // Yep, it ends in exit ;)
}
} // End sani
Well it appears that the header() works ok as its redirected to index.php?error=5 as in the last of the functions though so I guess thats not the issue.
In terms of the other functionâŚ
damn, thats such a nice way to do it, is this better
Because you pass $Input_Name in as a parameter, thereâs no need to reference the global $_POST array any more, so youâd just use the direct variable. Like youâve done in the line where you set the $_SESSION variable (and youâve a typo there, curly-brace instead of square)
In my opinion, and it might just be that, the function shouldnât be doing the final part. You should set a return code and have the main code look at whether the function returned âgoodâ or âbadâ and then act accordingly. The function you have written is fine for what it does, but what if you want the same check somewhere else, but at the end it needs to redirect somewhere else, or not redirect but do something else on error? For me youâd do something like:
if (validateAll("First_Name", $_POST['firstname'])) {
// do whatever the true return code indicates, set session var, etc.
}
else {
// do whatever false should, redirect etc.
}
So you donât need to have separate versions of the function for different outcomes. Think of the function as doing the validation and nothing else - it checks the string against the regex, and says âgoodâ or âbadâ.
No, Iâm saying you should pass the contents of the variable in, rather than just the index within the $_POST array.
function validateAll($Input_Name, $Regex) {
...
if (preg_match($RegArray[$Regex], $Input_Name])) {
..
then call it like:
if (validateAll($_POST['First_Name'], 'Name')) ....
What if you want to validate a variable using this function when the variable isnât in the $_POST array? This way will still work, your way can only validate $_POST member variables.
ok, made the changes, submitted the form (so I shouldâve been redirected to index.php?error=1 but got redirected to index.php?error=5
Did I miss something?
Heres the function & code to run it 5 timesâŚ
Does it work any better if you get rid of the var_dump() you can see in the top left corner? If youâve already sent output to the screen, your header() function wonât work.
As my understanding, you are appending a variable that does not exist. The problem is that you believe your function will return a variable along with the value of it based on your input, but that is not true. Since you donât have a returning variable, $input becomes invalid. Even if you return the value of $input inside your function, you would then need to globalize it in order for it to be called outside of the function. Unless you specifically give the function checking that variable and then reference it. This whole idea wonât work. I am not sure how itâs going to be done in procedural, but if itâs just like OOP, then you would have to separate your function checking from the actual logic. Like so
However, this will only give $_SEESSION['First_Name'] the value of true. So you would have to do more work. Also, if you want the userâs post value, why not create a variable for $_POST['First_Name'] and then just reference that variable and append it to $_SESSION['First_Name']?
NOTE
This example snippet is not tested since I am on my phone. So donât blame me if it doesnât work.
Function parameters exist only within that function - you should perhaps have a read up on variable scope:
// this is your mainline code
// here, $input does not exist
if (checkFirstName($_POST['First_Name'])) { .. do some some } else { .. do not}
..
..
function checkFirstName($input, $regex) { // this is where $input starts to exist
// it contains whatever you pass in as the first argument.
..
..
..
} // and this is where $input ceases to exist, at the end of the function,
In the same way that your function doesnât need to know about any variables outside of the function, any variables inside the function are only available within that function unless you specifically declare them as global. This is why, in the example above, you donât need to care whether or not your main code also contains a variable called $input - the one declared as a parameter for the function will not overwrite the other one.