0

I am having a problem in retrieving checkboxes from the database. Actually I am making edit form by using the input form which insert the values in database. I am facing problem with checkboxes and other inputs works fine.

My code looks like

<input type="checkbox" name="proizvodi[]" value="0" <?if ($row['PROIZVODI'] == '0') $checked = 'checked="checked"'; ?> checked="checked"><b>product1<b/>                                    <input type="checkbox" name="proizvodi[]" value="1" <?if ($row['PROIZVODI'] == '1') $checked = 'checked="checked"'; ?> checked="checked"><b>product2<b/> <br/> 
<input type="checkbox" name="proizvodi[]" value="2" <?if ($row['PROIZVODI'] == '2') $checked = 'checked="checked"'; ?> checked="checked"><b>product3</b> <br/>

To make it more clear, I want only checkboes that are stored in db to be checked. for ex if value 1 is stored in db, product1 checkbox will be checked, if value 1 is no stored then check box will be not checked.

I appreciate any help or advice.

Thanks in advance!

2
  • How do these textbox get generated? In php, or just hard coded in HTML? Commented May 19, 2014 at 8:32
  • the form is created in html and I am inserting these checkboxes in database by using implode function Commented May 19, 2014 at 9:08

2 Answers 2

1

How about this? You need to echo the 'checked="checked"' and do not assign it to a variable

    <input type="checkbox" name="proizvodi[]" value="0" <?php if (strpos($row['PROIZVODI'],'0') !== false) echo 'checked="checked"'; ?>><b>product1<b/>                                    
    <input type="checkbox" name="proizvodi[]" value="1" <?php if (strpos($row['PROIZVODI'],'1') !== false) echo 'checked="checked"'; ?>><b>product2<b/> <br/> 
    <input type="checkbox" name="proizvodi[]" value="2" <?php if (strpos($row['PROIZVODI'],'2') !== false) echo 'checked="checked"'; ?>><b>product3</b> <br/>
Sign up to request clarification or add additional context in comments.

4 Comments

@user3651819, What do you mean by not working? Error message? If this code is not working then there is something else on your code that does not work.
I mean when tried like you suggested, checkboxes still that are stored in db does not get checked. there is no chek mark in the box
@user3651819, if the db is from implode function, you should not use == in your if. Please check my updated answer.
How many checkboxes do you have? If it is 10 or more.. It is advisable to pad it with 0.. like 01,02,03 etc
0

Write your rows like this don't make it any harder than it is:

<input type="checkbox" name="proizvodi[]" value="0" <?php if ($row['PROIZVODI'] == '0') { ?> checked="checked" <?php } ?> /><b>product1<b/>    

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.