PHP form validation help

greif

New Member
Hi, I use following php form plugin to do some checks, set to onLoad:

Code:
defined('_JEXEC') or die();
$app = JFactory::getApplication();
$link = 'http://domain.com/index.php/';
$poptavkaid = JRequest::getVar(bs_objednavky___PoptavkaID_raw);
$user =& JFactory::getUser();
$uid = $user->get('id');
$groups = JAccess::getGroupsByUser($uid, false);
if (!in_array(9, $groups)) {
$db =& JFactory::getDBO();
$query3 = "SELECT userID FROM `bs_poptavky` WHERE bs_poptavky.id = $poptavkaid";$db->setQuery( $query3 );
$result3 = $db->loadResult();
if ($result3 != $uid) {$app->redirect($link, 'Pros?m, p?ihla?te se pod ??tem, ze kter?ho jste odeslali popt?vku!');}
$query = "SELECT count(id) FROM `bs_objednavky` WHERE bs_objednavky.PoptavkaID = $poptavkaid";
$db->setQuery( $query );
$result = $db->loadResult();
if ( $result != 0) {$app->redirect($link, 'Objedn?vka ji? byla vypln?na!');}
$query2 = "SELECT count(id) FROM `bs_poptavky` WHERE bs_poptavky.id = $poptavkaid";$db->setQuery( $query2 );
$result2 = $db->loadResult();
if ( $result2 == 0) {$app->redirect($link, 'Popt?vka neexistuje!');}}

This code works great, when the form loads first time. However, when the user wants to submit the form and any element validation fails, the page reloads, the php code seems to get run again, php validation fails for some reason and I get redirected... Any idea how to solve this?

Thanks
Vasik
 
Could this be because when the validation fails and the page gets reloaded, the URL parameter ie. domain.com/form/?bs_objednavky___PoptavkaID_raw=5 isn't retained?
 
Well the way to test is....

Load up two screens, the front end and the backend.

Let the front end load up as normal for your form. Once loaded go into the backend and modify your script by inserting the line below the JRequest.

Code:
$poptavkaid = JRequest::getVar(bs_objednavky___PoptavkaID_raw);
echo '<pre>'; print_r($poptavkaid); exit;
The on the front end do what you need to do to fail the validation.

On the reload it will abort but display the value of PoptavkaID_raw.

If the value is correct then that isn't the problem, if it doesn't then I guess it is. :)


So maybe try it out first before working on a solution.
 
Tested it now, and it seems we are on the right track - when I first load the form, $poptavkaid is string, I can echo it with echo $poptavkaid. Hovewer, when the form reloads after element validations, it is an array... Not sure why and what to do...
 
Yup, the result is correct, but I don't know how to deal with the variable once being string and once array...
 
You could use is_array

Code:
$poptavkaid = JRequest::getVar(bs_objednavky___PoptavkaID_raw);
[COLOR=Blue]if (is_array($poptavkaid)) {
$poptavkaid = $poptavkaid[0];
}[/COLOR]
$uid = $user->get('id');


I don't know what the array looks like though, so not sure if the value would be correct.

This may online be a bandaid though as I don't kow the actual reason why it's an array the second time.

Somebody more senior would have to advise that one.... Cue Uncle Hugh. :)
 
This workaround works great, thanks! I'm still curious though, why the variable is array the second time - might be because it is loading the variable from the form - I mean the first time it gets the variable from URL, the second time it get's it from the form already?
 
Yes I think that probably is the case and sounds logical but Rob or Hugh should be able to confirm that. :)
 
We are in need of some funding.
More details.

Thank you.

Members online

No members online now.
Back
Top