Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5

Thread: array keys

  1. #1
    New Coder
    Join Date
    Feb 2007
    Posts
    92
    Thanks
    1
    Thanked 0 Times in 0 Posts

    array keys

    I have a bunch of checkboxes and then on the next pg i check which boxes were ticked

    Here is my code


    PHP Code:
    $i 0
    $array ""
    foreach(
    array_keys($_POST) as $myKey

    echo 
    "ee"
    if (
    ereg('value'$myKey)) 

    echo 
    "hi"
      
    $id ereg_replace("value='checkbox'"""$myKey); 
      
    $array[$i] = $id
      
    $i $i+1


    If none of the boxes are ticked i get errors
    I want to check if boxes were not ticked and if none was ticked then send a user to another page
    But ive tried every way i can think of and cant get it to work

    Any ideas?

  • #2
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    You want to validate form data with isset or strlen:

    PHP Code:
    if (isset($var)) {
    //$var has value.

    } else {
    // $var does not have value.

    Also check out strlen.
    Leonard Whistler

  • #3
    New to the CF scene
    Join Date
    Mar 2007
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Good morning.

    If you do the checkboxes this way
    Code:
    <input type="checkbox" name="chkbx[]" value="1" />
    <input type="checkbox" name="chkbx[]" value="2" checked="checked" />
    <input type="checkbox" name="chkbx[]" value="3" />
    <input type="checkbox" name="chkbx[]" value="4" checked="checked" />
    <input type="checkbox" name="chkbx[]" value="5" checked="checked" />
    <input type="checkbox" name="chkbx[]" value="6" />
    <input type="checkbox" name="chkbx[]" value="7" />
    <input type="checkbox" name="chkbx[]" value="8" />
    and submit it to your script, you will get the follow array:

    PHP Code:
    $_POST["chkbx"] = Array (245); 

    Hope it helps

  • #4
    New Coder
    Join Date
    Feb 2007
    Posts
    92
    Thanks
    1
    Thanked 0 Times in 0 Posts
    The problem is i think there are always going to be values in the array because after checking check boxes (or not checking boxes) the user will click on a button to get to the next page where i want the validation to happen


    When i use isset() and no checkbox has been checked it stil gives a positive answer because of the button that was clicked

    any other suggestions!!
    thanks

  • #5
    New Coder
    Join Date
    Feb 2007
    Posts
    92
    Thanks
    1
    Thanked 0 Times in 0 Posts
    also i cannot use the other example by creating an array because i need to use the "name" value to pass details on the item that was checked!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •