Jump to content

is_array


kirstenmarie

Recommended Posts

I've never used is_array before or keys to check for the value.  I have a multi check box form.  Some or all of the boxes can be checked or none is ok.  I am trying to create exception handling before the values hit the database to make sure that only the value of the check box is being passed.  Would my code look like..

 

if is_array($_POST[interest] = true)

  value [1] == "sewing"->sewing 

if value [1] !=="sewing" thrown an exception

  value [2] == "hiking" ->hiking

 

 

Am I on the right track?

profile.php

Link to comment
https://forums.phpfreaks.com/topic/288329-is_array/
Share on other sites

if (is_array($_POST['interest']) && count($_POST['interest']) > 0)
{
       // code here
}

 

Note that the above code will give an undefined variable notice if you show all errors. Instead, you could use something like this:

if(isset($_POST['interest'])) {
     //process the "interest" values here
}

When the POST variable is set, it should be an array as long as the corresponding checkboxes are named appropriately.

Link to comment
https://forums.phpfreaks.com/topic/288329-is_array/#findComment-1478711
Share on other sites

Would my code look like..

 

if is_array($_POST[interest] = true)

  value [1] == "sewing"->sewing 

if value [1] !=="sewing" thrown an exception

  value [2] == "hiking" ->hiking

 

 

Am I on the right track?

 

Assuming that visitors can check more than one "interest" box, you could process the selected values like this:

if(isset($_POST['interest']) && is_array($_POST['interest'])) {
     foreach($_POST['interest'] as $currInterest) {
          echo "<div>$currInterest</div>";
     }
}
 
Of course, you'll need to adapt the code to fit your needs. Note that I added an is_array() test to make sure the "interest" variable is an array before using the foreach loop. That way you don't get errors if the user decides to tamper with the data submitted.
Link to comment
https://forums.phpfreaks.com/topic/288329-is_array/#findComment-1478716
Share on other sites

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.