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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.