Hello, I have got the following array string through URL.
http://www.xmeniafitnessclub.com/1024768/enquiry.php?fname=hgg&sex=female&facility=weightloss&facility=weightgain&facility=yoga&facility=danceclass&phone=363636&email=anuradha.jo1shi%40cognizant.com&feedback=%0D%0Ayuy&submit=Submit

I need to seprate out following values.. I am trying this array but with no luck..

# **&facility=weightloss&facility=weightgain&facility=yoga&facility=danceclass** #

HTML code for same is -

<tr>
                <td><label>Facility *:</label> </td>
                <td>

                    <input type="checkbox" name="facility" value="gym"/> Gym<br />
                    <input type="checkbox" name="facility" value="cardio"  /> Cardio <br />
                    <input type="checkbox" name="facility" value="gymcardio" /> Gym+Cardio <br />
                    <input type="checkbox" name="facility" value="weightloss" /> Weight Loss <br />
                    <input type="checkbox" name="facility" value="weightgain" />Weight Gain
                </td>

                <td>
                    <input type="checkbox" name="facility" value="aerobics" /> Aerobics <br />
                    <input type="checkbox" name="facility" value="yoga"  /> Yoga <br />
                    <input type="checkbox" name="facility" value="danceclass" /> Dance Class <br />
                    <input type="checkbox" name="facility" value="kickboxing" /> Kick Boxing <br />
                    <input type="checkbox" name="facility" value="guitar" /> Guitar 

                </td>
            </tr>

I have tried this using ...

**$fac = explode("=", urldecode($_GET['facility']));**

Can anybody please help me out?

Thanks in advance.

Recommended Answers

All 12 Replies

Member Avatar for LastMitch

@anuradha.joshi10

Can you post the array code. You just post the html code only.

You can use the input values by array as mentioned below,

<input type="checkbox" name="facility[]" value="gym"/> Gym<br />
<input type="checkbox" name="facility[]" value="cardio"  /> Cardio <br /> ...

and using php you can get the values thorugh a simple foreach loop,

foreach($_GET['facility'] as $val)
{
    echo $val . ","; // prints weightloss,weightgain...
}

by comma separated values,

$values = implode(",", $_GET['facility']);
echo $values; // prints weightloss,weightgain, and so on...

I agree with paulraj

Hi paulrajj,
Thanks for solution.
I have tried my level best on given solution with no luck.
Can you please share your mail ID, so that I can send my files on your mail.
Thanks in advance.
Anuradha Joshi.

you can post your whole php script here, with database sql files if any, with some sample data

HTML code
<td>

                    <input type="checkbox" name="facility[]" value="gym"/> Gym<br />
                    <input type="checkbox" name="facility[]" value="cardio"  /> Cardio <br />
                    <input type="checkbox" name="facility[]" value="gymcardio" /> Gym+Cardio <br />
                    <input type="checkbox" name="facility[]" value="weightloss" /> Weight Loss <br />
                    <input type="checkbox" name="facility[]" value="weightgain" />Weight Gain
                </td>

                <td>
                    <input type="checkbox" name="facility[]" value="aerobics" /> Aerobics <br />
                    <input type="checkbox" name="facility[]" value="yoga"  /> Yoga <br />
                    <input type="checkbox" name="facility[]" value="danceclass" /> Dance Class <br />
                    <input type="checkbox" name="facility[]" value="kickboxing" /> Kick Boxing <br />
                    <input type="checkbox" name="facility[]" value="guitar" /> Guitar 

                </td>
Javascript validation and redirecting to PHP file:-
// Facilty Checkbox validation
            // require that at least one checkbox be checked
            alert('check called');
            var checkSelected = false;
            for (i = 0;  i < formobj.facility.length;  i++)
            {
                if (formobj.facility[i].checked)
                checkSelected = true;
            }
            if (!checkSelected)
            {
            //alert("Please select at least one of the \"Facility\" options.");
                document.getElementById("message").innerHTML='<div class="redColor">Please select at least one of the \"Facility\" options.</div>';
                return false;
            }

            /* to get the value of select checkbox*/
            // only allow up to 2 checkboxes be checked

            var strfacility="";
            for (i = 0;  i < formobj.facility.length;  i++)
            {
            //alert (strfacility);
            if (formobj.facility[i].checked)
            strfacility = strfacility +" " + formobj.facility[i].value; 
            }
            alert (strfacility);
PHP Code
    foreach($_GET['facility'] as $val)
    {
    print_r $val . ","; // prints weightloss,weightgain...
    }

    // To print in email
        $Body .= "Facility: ";

Giving some errors as follow:-

Parse error: syntax error, unexpected T_VARIABLE in /home/content/34/9525934/html/1024768/enquiry.php on line 21

When I am writing following code:-

$facility1 = Trim(stripslashes($_GET['facility']));
  echo $facility1;

It just returns last value....

Can you please guide me where I am going wrong.

add complete html code, where is form, submit button

HTML Code :-

<div class="formcontainer">

                    <div id="message"></div><!--message-->

                                    <form action="enquiry.php" method="get" name="cform" onsubmit="javascript:return validate_form(this);">
        <table cellspacing="8">


            <tr>
                <td><label>Name *:</label> </td>
                <td><input type="text" size="41" name="fname" class="txt"/></td>
            </tr>
            <tr>
                <td><label>Gender *:</label> </td>
                <td>
                    <input type="radio" name="sex" value="male" checked="yes" /> Male
                    </td><td>
                    <input type="radio" name="sex" value="female" /> Female

                    <!-- <select name="sex" size="1">
<option>Select a Gender</option>
<option value="M">Male</option>
<option value="F">Female</option>
</select><br>-->
                </td>
            </tr>
            <tr>
                <td><label>Facility *:</label> </td>
                <td>

                    <input type="checkbox" name="facility[]" value="gym"/> Gym<br />
                    <input type="checkbox" name="facility[]" value="cardio"  /> Cardio <br />
                    <input type="checkbox" name="facility[]" value="gymcardio" /> Gym+Cardio <br />
                    <input type="checkbox" name="facility[]" value="weightloss" /> Weight Loss <br />
                    <input type="checkbox" name="facility[]" value="weightgain" />Weight Gain
                </td>
                    <!--</tr>-->
                <td>
                    <input type="checkbox" name="facility[]" value="aerobics" /> Aerobics <br />
                    <input type="checkbox" name="facility[]" value="yoga"  /> Yoga <br />
                    <input type="checkbox" name="facility[]" value="danceclass" /> Dance Class <br />
                    <input type="checkbox" name="facility[]" value="kickboxing" /> Kick Boxing <br />
                    <input type="checkbox" name="facility[]" value="guitar" /> Guitar 
                    <!--</table>-->
                </td>
            </tr>
            <tr>
                <td><label>Phone Number * : </label></td>
                <td><input type="text" size="41" name="phone" class="txt" /></td>
            </tr>
            <tr>
                <td><label>Email * :</label> </td>
                <td><input type="text" size="41" name="email" class="txt" /></td>
            </tr>
            <tr>
                <td><label>Please enter your message (if any) :</label> </td>
                <td><textarea cols="38" rows="5" name="feedback"></textarea></td>
            </tr>
            <tr>
                <td><input type="reset" name="reset" value="Reset" class="reset" /></td>
                <td><input type="submit" name="submit" value="Submit" class="submit" /></td>
            </tr>    
        </table>

        </form>



        </div><!--formcontainer-->

Try this in enquiry.php
<?php

$fac=implode(",",$_GET['facility']) ;
$Body .= "Facility: ".$fac;
 echo $Body;

?>

Member Avatar for diafol

This may be slightly off topic, but when you code for large numbers of checkboxes, I find the bitwise approach helpful. It goes something like this:

TABLE: activities
activity_id: int (or tinyint) PK
label: varchar (20)
activity_no: int

TABLE: users
user_id: int PK
(other user data like firstname, surname etc)
activities: int

The activities table stores as follows:

1 | Gym | 2
2 | Cardio | 4
3 | Gym&Cardio | 8
4 | Weight Loss | 16
5 | Weight Gain | 32
(etc - so next activity_no will be 64, 128, 256...)

The user data will then be something like:

1 | (other field data) | 24 (meaning Gym&Cardio (8) + Weight Loss (16))
2 | (other field data) | 36 (meaning Cardio (4) + Weight Gain (32))
(etc the activities field is just the sum of all the activities taken by the user)

You can retrieve the data like this - I'll use the old mysql just for clarity and I'll leave out any error-checking and mysql_num_rows():

$u = mysql_query("SELECT activities FROM users WHERE user_id = ...");
$d = mysql_fetch_assoc($u);
$acts = $d['activities']; //this is the summed number for all activities for that user

And for the activities form. This

$checks = '<td>';$x = 0;
$a = mysql_query("SELECT label, activity_no FROM activities");
$num = ceil(mysql_num_rows($a)/2) - 1;
while($d = mysql_fetch_assoc($a)){
    $this_activity = $d['activity_no'];
    $checks .= ($x == $num) ? '</td><td>' : ''; //this inserts a new column after half of the checkboxes giving you roughly balanced columns
    $sel = ($acts & $this_activity) ? ' checked="checked"' : ''; //determine whether this activity has been stored for the user - THIS IS A KEY LINE
    $label = $d['label'];
    $checks .= "\n<input type=\"checkbox\" value=\"$this_activity\" name=\"facility[]\"$sel /> $label<br />";
    $x++;
}
$checks .= '</td>';

...

<form...>
    ...
    <tr>
        <?php echo $checks;?>
    </tr>
    ...
</form>

This way, you do not have to hard code your html every time you decide to add new activities.

When the user updates/posts the form, the update is simple:

$newacts = array_sum(array_map('intval',$_POST['facility']));
$q = mysql_query("UPDATE users SET activities = $newacts WHERE user_id = ...");

Anyway - this is a bit ugly - typed quickly and not tested. This approach has served me well in the past as it frees you from having to change the DB table structure and the html form markup.

If you do delete an activity, it can be easily applied to all users like this:

$delete_act = 8; (remove activity_no = 8 i.e. Gym&Cardio)
$q = mysql_query("UPDATE users SET activities = activities - $delete_act WHERE activities & $delete_act");

Just a thought. Ignore it by all means.

Hi urtrivedi,
It's working fine now.
Thanks for immediate help :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.