Reputation: 23
I have multiple checkboxes that go through validation.. so i need the checkboxes that were picked to remain there..
this are some of them
<div class="col-sm-3 col-xs-6 padding-4-tb">
<input type="checkbox" name="skills[]" value="PHP"> PHP
</div>
<div class="col-sm-3 col-xs-6 padding-4-tb">
<input type="checkbox" name="skills[]" value="Python"> Python
</div>
<div class="col-sm-3 col-xs-6 padding-4-tb">
<input type="checkbox" name="skills[]" value="Ruby"> Ruby
</div>
<div class="col-sm-3 col-xs-6 padding-4-tb">
<input type="checkbox" name="skills[]" value="Rails"> Rails
</div>
as you can see the value doesn't contain numbers.. so how can i do it ??
Upvotes: 0
Views: 72
Reputation: 3707
Your question is not completely clear, but I assume you are submitting a form and then rending the form with validation errors, and you don't want to lose the user's selections. If you're just using old-school PHP and HTML in the same file, you would do something like this:
<div class="col-sm-3 col-xs-6 padding-4-tb">
<input type="checkbox" name="skills[]" value="PHP"<?php if(in_array('PHP', $skills))?> checked<?php } ?>> PHP
</div>
<div class="col-sm-3 col-xs-6 padding-4-tb">
<input type="checkbox" name="skills[]" value="Python"<?php if(in_array('Python', $skills))?> checked<?php } ?>> Python
</div>
<div class="col-sm-3 col-xs-6 padding-4-tb">
<input type="checkbox" name="skills[]" value="Ruby"<?php if(in_array('Ruby', $skills))?> checked<?php } ?>> Ruby
</div>
<div class="col-sm-3 col-xs-6 padding-4-tb">
<input type="checkbox" name="skills[]" value="Rails"<?php if(in_array('Rails', $skills))?> checked<?php } ?>> Rails
</div>
The net is that you need to add the "checked" attribute to each previously selected checkbox. You determine whether or not each box was selected by checking for the presence of it's value in the array of skills values sent to the server on submit.
Upvotes: 1