Welcome Guest, Not a member yet? Register   Sign In
$this->input->post() data and "undefined index"
#1

[eluser]tinawina[/eluser]
I am using CI's this->input->post() to work with data posted to a search form. In the form I have a series of checkboxes. In my controller I set up an array containing the checkbox names and I then loop through the post array to determine what to process:

Code:
$checkbox_data = array('subject', 'document_type', 'document_format');

foreach ($checkbox_data AS $value)
{
if ( $this->input->post($value) != '' ) ) // eg., $this->input->post('subject')
{
  $data[] = $this->search_lib->process_array_input($value, $this->input->post($value), $this->input->post($value . '_qualifier'));
}
}

This is working except for the fact that I am getting an error notice: "Message: Undefined index: subject"

I am checking for this index before trying to process it. Not sure why I'm getting this error. Also I only get the error for the line that starts with $data[] =.... I don't get it for the check I'm doing in the if statement even though I'm calling $value in the same way as I do to create the $data array.

What needs doing to get rid of this error? Any help is appreciated - thanks!
#2

[eluser]CroNiX[/eluser]
$this->input->post(var) will return boolean FALSE (currently, but will change to NULL in CI3) if the value doesn't exist, not an empty string which is what you're testing for. Checkboxes that aren't checked don't get sent in the form submission, so they will be FALSE.

Code:
if ( $this->input->post($value) !== FALSE ) //use tripple === operator, you also had an extra )

I'm guessing that the "subject" checkbox wasn't checked, which would cause this error if testing for an empty string, because "subject" doesn't exist in the POST array.
#3

[eluser]tinawina[/eluser]
Hi CroNiX and thanks for this. I tried your fix but I still get the error message. I get this message when ome or more 'subject' boxes are checked. If I don't check a subject box I dont get the error. Any other ideas? I'll take 'em! This has been driving me nuts since yesterday a.m.....
#4

[eluser]CroNiX[/eluser]
Post the portion of your form containing your "subject" checkboxes.
#5

[eluser]tinawina[/eluser]
Code:
&lt;input type="checkbox" name="subject[]" value="Aging" /&gt;Aging<br />
&lt;input type="checkbox" name="subject[]" value="Agriculture and Food" /&gt;Agriculture and Food<br />
&lt;input type="checkbox" name="subject[]" value="Animal Welfare" /&gt;Animal Welfare<br />
&lt;input type="checkbox" name="subject[]" value="Arts and Culture" /&gt;Arts and Culture<br />
&lt;input type="checkbox" name="subject[]" value="Athletics and Sports" /&gt;Athletics and Sports<br />
&lt;input type="checkbox" name="subject[]" value="Children and Youth" /&gt;Children and Youth<br />
&lt;input type="checkbox" name="subject[]" value="Civil Society" /&gt;Civil Society<br />
&lt;input type="checkbox" name="subject[]" value="Community and Economic Development" /&gt;Community and Economic Development<br />
&lt;input type="checkbox" name="subject[]" value="Computers and Technology" /&gt;Computers and Technology<br />
&lt;input type="checkbox" name="subject[]" value="Consumer Protection" /&gt;Consumer Protection<br />
&lt;input type="checkbox" name="subject[]" value="Crime and Safety" /&gt;Crime and Safety<br />
&lt;input type="checkbox" name="subject[]" value="Disabilities" /&gt;Disabilities<br />
&lt;input type="checkbox" name="subject[]" value="Education and Literacy" /&gt;Education and Literacy<br />
&lt;input type="checkbox" name="subject[]" value="Employment and Labor" /&gt;Employment and Labor<br />
&lt;input type="checkbox" name="subject[]" value="Energy and Environment" /&gt;Energy and Environment<br />
&lt;input type="checkbox" name="subject[]" value="Gay, Lesbian, Bi and Trans" /&gt;Gay, Lesbian, Bi and Trans<br />
&lt;input type="checkbox" name="subject[]" value="General" /&gt;General<br />
&lt;input type="checkbox" name="subject[]" value="Government Reform" /&gt;Government Reform<br />
&lt;input type="checkbox" name="subject[]" value="Health" /&gt;Health<br />
&lt;input type="checkbox" name="subject[]" value="Housing and Homelessness" /&gt;Housing and Homelessness<br />
&lt;input type="checkbox" name="subject[]" value="Human Rights and Civil Liberties" /&gt;Human Rights and Civil Liberties<br />
&lt;input type="checkbox" name="subject[]" value="Human Services" /&gt;Human Services<br />
&lt;input type="checkbox" name="subject[]" value="Humanitarian and Disaster Relief" /&gt;Humanitarian and Disaster Relief<br />
&lt;input type="checkbox" name="subject[]" value="Hunger" /&gt;Hunger<br />
&lt;input type="checkbox" name="subject[]" value="Immigration" /&gt;Immigration<br />
&lt;input type="checkbox" name="subject[]" value="International Development" /&gt;International Development<br />
&lt;input type="checkbox" name="subject[]" value="Journalism and Media" /&gt;Journalism and Media<br />
&lt;input type="checkbox" name="subject[]" value="Medical Research" /&gt;Medical Research<br />
&lt;input type="checkbox" name="subject[]" value="Men" /&gt;Men<br />
&lt;input type="checkbox" name="subject[]" value="Nonprofits and Philanthropy" /&gt;Nonprofits and Philanthropy<br />
&lt;input type="checkbox" name="subject[]" value="Parenting and Families" /&gt;Parenting and Families<br />
&lt;input type="checkbox" name="subject[]" value="Peace and Conflict" /&gt;Peace and Conflict<br />
&lt;input type="checkbox" name="subject[]" value="Poverty" /&gt;Poverty<br />
&lt;input type="checkbox" name="subject[]" value="Prison and Judicial Reform" /&gt;Prison and Judicial Reform<br />
&lt;input type="checkbox" name="subject[]" value="Race and Ethnicity" /&gt;Race and Ethnicity<br />
&lt;input type="checkbox" name="subject[]" value="Religion" /&gt;Religion<br />
&lt;input type="checkbox" name="subject[]" value="Science" /&gt;Science<br />
&lt;input type="checkbox" name="subject[]" value="Substance Abuse and Recovery" /&gt;Substance Abuse and Recovery<br />
&lt;input type="checkbox" name="subject[]" value="Transportation" /&gt;Transportation<br />
&lt;input type="checkbox" name="subject[]" value="Welfare and Public Assistance" /&gt;Welfare and Public Assistance<br />
&lt;input type="checkbox" name="subject[]" value="Women" /&gt;Women
#6

[eluser]CroNiX[/eluser]
The problem is probably in your search_lib::process_array_input().

$this->input->post('subject') will be an array of all the checked "subject" checkbox values.

So, it will look something like:
Code:
array(
  0 => 'Aging',
  1 => 'Agriculture and Food',
  2 => 'Women'
)
If only the first 2 and very last checkboxes were checked.
#7

[eluser]tinawina[/eluser]
When check Aging, Agriculture and Food, Women and then var_dump($this->input->post()) I get this data pertaining to subject:

Code:
["subject"]=> array(3) { [0]=> string(5) "Aging" [1]=> string(20) "Agriculture and Food" [2]=> string(5) "Women" }

I am able to process this just fine and get the data working for me. But if I check any subject checkbox I get that index error message. If I don't check anything I don't see the error.




Theme © iAndrew 2016 - Forum software by © MyBB