1

I have 4 checkboxes. All in form of;

input type="checkbox" name="Group[]" id="Group" value="uniquevalue"

I have 3 of 4 of them checked. How can I send the values that are checked in the form

"a = chkedvalue1 &b = chkedvalue2 &c = chkedvalue3"?

I'm using jquery 1.4.2

Thank you! Spent a whole day with this issue to no avail.

1
  • 1
    Don't. Really. Have the server side process expect Group[]=chkedvalue1&Group[]=chkedvalue2&Group[]=chkedvalue3 (which is how the form data will be encoded using a normal forum submission (this way you can build on things that work)) and then just use jQuery's serialize() Commented Dec 23, 2010 at 11:54

3 Answers 3

4

First, remove that id attribute, it needs to be unique. Then to get the array of values you can just .serialize() the <form> like this:

var data = $("form").serialize();

Note this will produce Group[]=..., remove the [] from the name attributes if you just want Group= for each.

...or if you want the array of values, use .map(), like this:

var arr = $("input[name='Group[]']:checked").map(function() { 
            return this.value; 
          }).get();
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. This was the answer.
3
<?php
$con = mysql_connect("localhost","root","");
mysql_select_db("test",$con);
$ID = $_GET['id'];
$sql = "select city_name from city where id='$id'";
$fetch = mysql_fetch_array(mysql_query($sql));
$result = explode(",",$fetch['city_name']);
?>
<html>
<body>
<form method="post" >
<input type="checkbox" name="city[]" value="Faridabad" <?php for($i=0;$i<4;$i++) if($result[$i]=='Faridabad')echo 'checked'; ?> />Faridabad
<input type="checkbox" name="city[]" value="Gurgaon" <?php for($i=0;$i<4;$i++) `if($result[$i]=='Gurgaon') echo 'checked';?>/>Gurgaon`
<input type="checkbox" name="city[]" value="Noida"<?php for($i=0;$i<4;$i++) if($result[$i]=='Noida') echo 'checked'; ?>/>Noida
<input type="checkbox" name="city[]" value="Gaziabad" <?php for($i=0;$i<4;$i++) if($result[$i]=='Gaziabad') echo 'checked'; ?>/>Gaziabad
</form>
</body>
</html>

1 Comment

Thanks for posting an answer! While a code snippet could answer the question it's still great to add some addition information around, like explain, etc ..
1

I suggest you serialize the form.

1 Comment

Thank you. That's the function I was looking for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.