Forums

Need help reading fields in PHP

hrhsii 05 May, 2010
Hi I've seen several problems with people getting blank data from check boxes, I see fixes like:

 
<?php
// substitute the values from the array for any missing values from the form$check_array = array(
  'check0' => '0',         
  'check1' => '0'            
);
foreach ( $check_array as $k => $v ) {
  $temp = JRequest::getString($k, $v, 'post');
  JRequest::setVar($k, $temp);
}
?>


This works for me in my emails, I get results even if the check boxes were not used.

My problem is that i need to check the check boxes and drop down select boxes to see if people qualify to be registered to my joomla site.

Can someone please post some examples of how to read checkbox arrays and dropdown selections,

I have tried:
1)


$checkbox_array = array(
  'check0',          
  'check1'          
);

foreach ( $checkbox_array as $k => $v ) {
  $temp = JRequest::getVar($v, '', 'post');
echo $v." -- ". $temp[0]. " -- ". $k. "<br />";
}


v = check? the field name
temp[0] = Yes or blank
k = index 0, 1, 2, 3, etc

how do i read the details when the checkbox is blank, somehow the email is able to do i, but I can't, can someone help please?

Once I can put the data into a variable I should be fine, I can do it with normal html, but i don't get the
JRequest::getString($k, $v, 'post');
syntax, is there somewhere that explains it?
GreyHead 05 May, 2010
Hi hrhsii,

Please an you give a real example of what you want to check? It's a bit hard to give a theoretical answer as there are several ways that you might approach this.

Bob
hrhsii 05 May, 2010
Thanks for the speedy reply

For normal text fields i have used:

global $mainframe;
if ( JRequest::getVar('password_text') != JRequest::getVar('confirm_password_text') )
return 'Sorry, your passwords do not match, please try again!';


For check boxes I found someone using, and have used:

$sing_chk = JRequest::getVar('check0', array(), 'POST', 'array', 0);

  echo "Single: ";
if (in_array('Yes', $sing_chk)){
  echo "Yes";
} else{
  echo "No";
}

Is this the most efficient way of reading check box arrays, it looks a bit longwinded?

Also I have a hidden field called qualified_hidden, I want to change this to 1 if my conditions are met, so that it is stored in the database with the new change. How do I change a field?

Do I do
JRequest::setVar('1', 'qualified_hidden');
or
JRequest::setString('1', 'qualified_hidden');
and what parameters do i put in the brackets?
GreyHead 06 May, 2010
Hi hrhsii,

I'm sorry I still don't understand what you are trying to do. The input example looks like server-side validation code but the check-box example celarly isn't.

For a yes/no checkbox I'd suggest that you use a radio button pair rather than checkboxes.

I'm not sure where you are setting the hidden field value either but the syntax is
JRequest::setVar('name', 'value');
and you appear to have the value first.

Bob
hrhsii 06 May, 2010
Thank you James for your reply.

All my fields come from my Form,and yes it is all server-side validation code.
Thanks the
 JRequest::setVar('name', 'value');

worked... Is there a tutorial or manual on how to us this JRequest:: or is it different with every Joomla component/plugin?

I have used:

<?php
// substitute the values from the array for any missing values from the form$check_array = array(
  'check0' => '0',         
  'check1' => '0'            
);
foreach ( $check_array as $k => $v ) {
  $temp = JRequest::getString($k, $v, 'post');
  JRequest::setVar($k, $temp);
}
?>
 


in the Before Submit section, to initialize the checkboxes if they are blank, then i used:


$qualify = JRequest::getVar('qualified_hidden');

$sing_chk = JRequest::getVar('check0', array(), 'POST', 'array', 0);

if (JRequest::getVar('income_select') == "£0-£12,000"){
   $qualify=1;
}

if ((JRequest::getVar('income_select') == "£12,000-£24,000" ) && !(in_array('Yes', $sing_chk))){
   $qualify=1;
}

if ($qualify==1){
   JRequest::setVar('qualified_hidden', '1' );
}

$sorry = 'Sorry, you do not qualify, our gyms only allow people on low incomes,<br /> our limit is £12,000 if you are single, and up to £24,000 if you are a couple.<br /><br />Please check back in a few months or sign up to our newsletter to be kept informed of any changes.';

if ($qualify != 1)
return $sorry;


In the Validation section, so that if the conditions are not met the form is not processed.

I just wanted to know if there was a more efficient way of checking checkboxes than this way:
 in_array('Yes', $sing_chk) 


Finally, is there a way to save the information of people that fail my conditions in the database, as at the momnent in the validation section, I simply do a return with an appology message, and only successfully submitted form information is automatically saved in the database.
GreyHead 08 May, 2010
Hi hrisii,

Not sure who James is . . . ?

JRequest is standard Joomla! code. It is documented here.

Bob
This topic is locked and no more replies can be posted.

VPS & Email Hosting 20% discount
hostinger