www.webdeveloper.com
Results 1 to 7 of 7

Thread: Mail form and checkboxes

  1. #1
    Join Date
    Jul 2006
    Posts
    373

    Mail form and checkboxes

    Hi,

    i used the search and even googled to find about this but im still unable to resolve this problem. I cant get the checkboxes to work in a php form. My code is like this

    if(!isset($address)){ $address = $_POST['address']; }
    if(!isset($sex)){ $sex = $_POST['sex']; }
    if(!isset($hobbies))$hobbies= $_POST['hobbies'];
    foreach ($hobbies as $s) {
    echo "$s, ";
    }

    All the checkboxes related are named hobbies{} but i get an error saying argument for foreach is invalid. What am i doing wrong?

    Thank you!

  2. #2
    Join Date
    Sep 2009
    Posts
    93
    Quote Originally Posted by supercain View Post
    Hi,

    i used the search and even googled to find about this but im still unable to resolve this problem. I cant get the checkboxes to work in a php form. My code is like this

    if(!isset($address)){ $address = $_POST['address']; }
    if(!isset($sex)){ $sex = $_POST['sex']; }
    if(!isset($hobbies))$hobbies= $_POST['hobbies'];
    foreach ($hobbies as $s) {
    echo "$s, ";
    }

    All the checkboxes related are named hobbies{} but i get an error saying argument for foreach is invalid. What am i doing wrong?

    Thank you!

    your not put braces, so try line this

    if(!isset($address)){ $address = $_POST['address']; }
    if(!isset($sex)){ $sex = $_POST['sex']; }
    if(!isset($hobbies)){
    $hobbies= $_POST['hobbies'];
    foreach ($hobbies as $s) {
    echo "$s, ";
    }
    }

  3. #3
    Join Date
    Jul 2006
    Posts
    373
    No, sorry. Actually i forgot to post here the last } but my originally code had it and still doesnt work. I get the argument invalid error.

  4. #4
    Join Date
    Sep 2009
    Posts
    93
    I hava one doubt, actually $_POST is an array, and element in that are treat as an variable.

    Then how we treat as $_POST['hobbies'] an array.

    if it is not array foreach function is useless.

    howevery, please tr print_r($_POST['hobbies']) and conform is it array or not?

  5. #5
    Join Date
    Jul 2006
    Posts
    373
    like this?

    if(is_array($hobbies))
    $comma = "";
    $hobbies_type = "";
    foreach($hobbies as $value)
    {
    $hobbies_type .= $comma.$value;
    $comma = ", ";
    }

  6. #6
    Join Date
    Jul 2006
    Posts
    373
    Some help please?

  7. #7
    Join Date
    Jan 2009
    Posts
    3,346
    Quote Originally Posted by supercain View Post
    like this?

    if(is_array($hobbies))
    $comma = "";
    $hobbies_type = "";
    foreach($hobbies as $value)
    {
    $hobbies_type .= $comma.$value;
    $comma = ", ";
    }
    You are missing some curly braces:
    PHP Code:
    if(is_array($hobbies)){
       
    $comma "";
       
    $hobbies_type "";
       foreach(
    $hobbies as $value)
       {
          
    $hobbies_type .= $comma.$value;
          
    $comma ", ";
       }


Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles