0

I want to check or uncheck the checkbox using jquery after getting details from database in edit page using php. I am putting following code at the top of file.

$edit_data = DB::queryFirstRow("select * from  items where id='".$_GET[id]."' "); 

And html code is:

    <div class="form-group col-lg-2" >
        <label>Exempted Tax</label>   
        <label style="cursor:pointer">
           <input type="checkbox" name="not_applicable[]" value="1" />   Yes                         
        </label>                                                                                                                             
    </div>               
    <div class="form-group col-lg-3" >
        <label>Status </label><br />
        <label style="cursor:pointer">
           <input type="checkbox" name="active_status[]" value="1"  /> 
        Inactive</label>
    </div>
                                             

At the end of file, I added the code to check the checkbox based on condition:

echo $edit_data[not_applicable]=="1"?'$(\'input[name="not_applicable"]\').attr("checked", "checked");':'$(\'input[name="not_applicable"][value="'.$edit_data[not_applicable].'"]\').removeAttr("checked");';
echo $edit_data[active_status]=="1"?'$(\'input[name="active_status"]\').attr("checked", "checked");':'$(\'input[name="active_status"][value="'.$edit_data[active_status].'"]\').removeAttr("checked");';

And I also tried prop(). It also gives same output. It doesn't show checkmark if condition is true, after page load.it seems like unchecked if even condition is true. Please help me to resolve this.

1 Answer 1

1

I would suggest you do not wrap with a <script></script>.

<script>
    echo $edit_data['not_applicable']=="1"?'$(\'input[name="not_applicable"]\').attr("checked", "checked");':'$(\'input[name="not_applicable"][value="'.$edit_data['not_applicable'].'"]\').removeAttr("checked");';
    echo $edit_data['active_status']=="1"?'$(\'input[name="active_status"]\').attr("checked", "checked");':'$(\'input[name="active_status"][value="'.$edit_data['active_status'].'"]\').removeAttr("checked");';
</script>

But I will change to check the condition inline instead of calling js at the end of the page.

    <div class="form-group col-lg-2" >
        <label>Exempted Tax</label>   
        <label style="cursor:pointer">
           <input type="checkbox" name="not_applicable[]" value="1" <?php (($edit_data['not_applicable']=="1") && ($edit_data['active_status']!="1")) ? 'checked=checked' :'' ?> />   Yes                         
        </label>                                                                                                                             
    </div>               
    <div class="form-group col-lg-3" >
        <label>Status </label><br />
        <label style="cursor:pointer">
           <input type="checkbox" name="active_status[]" value="1" <?php (($edit_data['not_applicable']=="0") && ($edit_data['active_status']!="1")) ? 'checked=checked' :'' ?>  /> 
        Inactive</label>
    </div>
Sign up to request clarification or add additional context in comments.

7 Comments

still it is not working. <div class="form-group col-lg-3" > <label>Status </label><br /> <label style="cursor:pointer"><input type="checkbox" name="active_status[]" value="1" <?php ($edit_data[active_status]=="1") ? 'checked=checked' :'' ?> /> Inactive</label> </div>
Sorry that I missed some Quotation mark in the array. I have edited the answer.
<input type="checkbox" name="active_status[]" value="1" checked=checked /> will check the box. I would suggest the $edit_data not giving the correct value. May you provide the result of var_dump($edit_data);?
This is the output of $edit_data value. I print it on the screen. Array ( [id] => 2 [type] => 1 [name] => Seagate Backup Plus Slim 2 TB External HDD [hsn] => [unit] => Pc [rate] => 4820.54 [mrp] => 7999 [description] => [tax] => 12 [percentage] => [weight] => [code] => Seagate 2 TB HDD [local_language] => Seagate 2 TB External HDD [rate_inc] => 5399 [serial] => 0 [stock] => 10 [ledger] => 2 [rack] => [min_alert] => 5 [company] => 5 [prate] => 4700 [not_applicable] => 1 [whole_rate_inc] => 4999 [whole_rate] => 4463.39 [active_status] => ) ;
Sir , where you are specifying that quotation mark is missed?
|

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.