1

I have one form which contain product information and one field which have two(2) check box for status(want to post advertise for that particular product or not.)and i have one field which is for "add more" it will clone whole div and also check boxes so i have more then one output but I don't have getting single value.

<div>
     <label>I want to Advertise This Item</label>
     <input type="checkbox" value="1" name="chkyes[]" id="chkyes[]"/>
     Yes
     <input type="checkbox" value="0" name="chkyes[]" id="chkyes[]"/>
     No 
</div>

Above code is for selecting checkbox, and below code which echo value in array but I don't get the value of checkbox.

if(count($_POST)){
    $len = count($_POST['producttitle']);
    for ($i=1; $i < $len; $i++){
        echo $_POST['chkyes'][$i];
    }
}
3
  • you can't give id's like chkyes[] give it as chkyes1, chkyes2 Commented Jan 6, 2014 at 9:27
  • Where exactly is the form element with name of producttitle? Commented Jan 6, 2014 at 9:28
  • Oops, right. I think HTML5 allows these characters in IDs, but the real problem is that he has duplicate IDs. IDs must be unique. Commented Jan 6, 2014 at 9:33

5 Answers 5

2

why you dont use foreach?

for eg

if (isset($_POST['chkyes'])) {
    foreach ($_POST['chkyes'] as $value) {
        echo $value;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

The first approach:

<input type='checkbox' name='chkyes[1]'>
<input type='checkbox' name='chkyes[2]'>
<input type='checkbox' name='chkyes[3]'>

This way you could access them in PHP with

foreach ($_POST['chkyes'] as $id=>$checked){
   if ($checked =='on')
    //process your id
 }

The second approach:Set value's attributes on checkboxes:

<input type='checkbox' name='chkyes[]' value='1'>
<input type='checkbox' name='chkyes[]' value='2'>
<input type='checkbox' name='chkyes[]' value='3'>

With this you'll receive only checked values:

foreach ($_POST['chkyes'] as $id){
    //process your id
 }

Comments

1

Try Below code

<?php
if($_POST['submit'] == 'submit'){
    $len = count($_POST['chkyes']);
    for ($i=0; $i < $len; $i++){
        echo $_POST['chkyes'][$i];
    }
}
?>


<form name="test" method="POST">
<div>
     <label>I want to Advertise This Item</label>
     <input type="checkbox" value="1" name="chkyes[]" id="chkyes[]"/>
     Yes
     <input type="checkbox" value="0" name="chkyes[]" id="chkyes[]"/>
     No 
</div>
<input type="submit" name="submit" value="submit">
</form>

Comments

1

Array of input start from an index of 0, and use $_POST['chkyes'] instead of $_POST['producttitle']

 if(count($_POST)){
     $len = count($_POST['chkyes']);
     for ($i=0; $i < $len; $i++){
         echo $_POST['chkyes'][$i];
     }
 }

also you can't give id's like chkyes[] give it as chkyes1, chkyes2

9 Comments

using characters like [,] are valid. problem might be duplication of ids
yup i said for id only that it can be an array
You mentioned about chkyes[] being invalid.
for id, 'you can't give id's like chkyes[]'
if i give id like chkyes1[] and chkyes2[] then also it gives error Undefined offset: 2 and returns 0 as value.
|
1

Only checkboxes that are checked are submitted in the form. They'll be collected in an array $_POST['chkyes'], but the indexes won't be the same as corresponding text inputs. You need to process them with their own foreach loop, not the same loop as for other inputs.

For what you're doing, why aren't you using radio buttons or a single checkbox? What if the user checks both Yes and No?

Comments

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.