Retrieve stored check box value from database - javascript

I have set of check boxes in my Privileges view.
<input type="checkbox" value="1" id="CheckBoxManageDevices"name="CheckBoxManageDevices" />Manage Tracking Devices
<input type="checkbox" value="2" id="CheckBoxMaps" name="CheckBoxMaps" />Manage Maps
I want to save the status of the check boxes (true or false) in the database and load the Privileges page with the updated values in the next time.
I was able to store the status of the check boxes using
$PriviledgeArray["BoxMaps"]=$this->input->post("CheckBoxMaps");
Now I want to fetch check box status to the Privileges page and show the check boxes are marked or unmarked depending on the database value. I have read that java script can be used to solve this but don't have a head start.
Any hint will be highly appreciated.

<?php
$checkedMap="";
$checkedDevices="";
if($CheckBoxMaps==2){ // CheckBoxMaps from database
$checked="checked";
}
if($CheckBoxManageDevices==1){ // CheckBoxManageDevices value from database
$checkedDevices="checked";
}
?>
<input type="checkbox" value="1" id="CheckBoxManageDevices"name="CheckBoxManageDevices" <?php echo $checkedDevices;?> />
<input type="checkbox" value="2" id="CheckBoxMaps" name="CheckBoxMaps" <?php echo $checkedMap;?> />

Related

how to keep correct checkbox value check array after submit/document.form[0] submit or refresh page SOLVED

<script>
function refreshPage()
{
document.forms[0].submit();
}
</script>
<?php
foreach($values as $value)
{
?>
<input type = "checkbox" name = "food[]" value = "<?php echo($value['dinner']);?>">
<?php
}
?>
<button name = "pass" onClick = "refreshPage()">refresh</button>
so I have n amount of check box (depending on how many values user has)I want to keep them the check box check after the page refresh (by clicking on refresh button). I tried searching and saw similar form but the answer did not work for me.
For example the most popular answer was:
<input type="checkbox" name="txtCheck" value="your value"
<?php if(isset($_POST['txtCheck']))
echo "checked='checked'"; ?>
/><br />
this had two problem after i hit the refresh button value would not be save until i hit it twice to save(i want it to save after one click. Also sometime it save after one click but if i hit the refresh 3 time values are lost I want it to be save no matter how many time user refresh)
the second problem was it check all of the box would be check. I only want to keep the one user has check to be save.
I looked at various other possible solution but nothing worked
so if you could please help much would be appreciated. Also I need the value to be kept. I am using the checkbox value somewhere else
Edit something like this but for my array food[]. This only works for invidiual values
<input type="checkbox" name="small" class="checkbox" <?php if
($_POST['small']) echo 'checked'; ?> /> Small

Save and Load Checkbox checked states to database

I have a "settings" page in my site and I would like the user to be able to either check or uncheck checkboxes in the settings page. The checkboxes' states should then load when the user logs in.
My idea is to assign ids to each checkbox like this:
<input type="checkbox" id="chk1" data-col='column1' value="0"/>
<input type="checkbox" id="chk2" data-col='column2' value="0"/>
and then use jQuery to set the values for each on the checked function like this on the "SAVE SETTINGS" button:
$( "input:checkbox" ).each(function() {
if $(this).prop('checked'){
$(this).prop('value','1')
}
});
and then have a column in my MySql table for each checkbox and then send the value (either 1 or 0) to my database via ajax/php.
Then when the user logs in, I can get the values (1 or 0) for each column and then based on that, I can set the checked state.
Is this a good way to go about it? I want to make sure I don't go down the wrong path if there is a simpler solution or approach.
Assume that $query_array is the container of your db query. Since you've included php in the post's tag you can insert a php tag inside input(assuming the file is a php too):
<input type="checkbox" id="chk1" data-col='column1' value="<?php echo $query[0][col_name];?>"/>
^the 0 above might be an id, and col_name is the column name which contains 0/1
When that is compiled, it should be similar to this:
<input type="checkbox" id="chk1" data-col='column1' value="0"/>
Instead of using jquery why don't you just render the checkboxes with their value already set:
<input type="checkbox" id="chk1" data-col='column1' value="<?php echo $chk1?>" <?php echo $checked?>/>
Edit
If you wanna keep it HTML only you could store the states in a JSON (where the property name is the id of the chekbox and the value it's state) and loop through it as:
$.each(jsonChkbxValues, function(index, val) {
var elem = $('#' + index);
elem.prop('value', val);
if (val) {
elem.attr('checked', 'checked');
}
});

check box value display in results page

i have two php file one input page another results page . input page have checkbox. code looks like this
<form action="" id="frmCalculators" method="post" name="frmCalculator">
<input id="chkUFtoLoan-1" name="S" value="S" type="checkbox" checked />
<button id="Button4" type="submit" onclick="GetPage()" class="dont-compare shadow-inset ">
Calculate Scenarios</button>
I have passed check box value next page php
<?php
session_start();
$checkbox1 = $_POST["S"];
?>
i have tired checked using echo value passed in results php.
Now i have question i have value in results page looks like
About your Loan :
<label id="FinancedUpfrontMIP" class="pull-left">Financed Upfront MIP</label>
<label id="financedmip" class="pull-right">$<?php echo round($arrFirstBox["upfrontmipamt"],2);?></label>
Buyer close to section:
<label id="FinancedUpfrontMIP" class="pull-left">Financed Upfront MIP</label>
<label id="financedmip" class="pull-right">$<?php echo round($arrFirstBox["upfrontmipamt"],2);?></label>
my question user select check box value display about your loan if user not select checkbox value display buyer close to section.. Now display same value both place. if one value one show another value need to show Zero.. please any idea about it?

How do I prevent a pre-set INPUT value from being changed?

I have a form where some fields are completed by loading information from a database. These fields can vary from one account to another.
When the form is submitted any database information will be sent along with new information entered in the form.
However, the user is able to change (on screen) the information entered from the database, and although these changes are not submitted (my $Xvariables from the database override the POSTed $variables), I would like them to revert the on-screen data back to the database value.
Here is an example of what I have at the moment, but it does not work ...
<input type="text" name="firstname" size="25" tabindex="4" onchange="if($xfirstname<>'') this.value=$xfirstname" value="<?php echo $firstname;?>"/>
or better still, if there is a way, how can I make them unchangable if they have a value loaded from the database ?
Just place readonly="readonly" within the input <> tags or place disabled="disabled"
Example
<input type="text" name="firstname" size="25" tabindex="4" onchange="if($xfirstname<>'') this.value=$xfirstname" value="<?php echo $firstname;?>" readonly="readonly" />
Keep this:
<input type="text" name="firstname" size="25" tabindex="4"
value="<?php echo $firstname;?>"/>
Add this: Edit: Sorry did not see it has no ID on the input element use this:
<script>
if($('input[name="firstname"]').val().length > 0)
{
$('input[name="firstname"]').attr("disabled", true);
}
</script>
Result :http://jsfiddle.net/4wt9r/6/
But keep in mind anything on the client can be changed, your true filtering/authenticating must be done on the server.

Submitting radio button values without page refresh

I'm currently having radio buttons to choose which payment the user want on the website, they can choose between (invoice, card, part payment(?)).
I'm using scripting to submit the radio buttons so no button is needed, however the page reloads every time they choose a different payment option, which is bad for the customer since they have to scroll down again to input the values of the form (phone number etc.) that appear when the radio button is selected.
So after a few complaints I've been trying to fix this issue to make the form appear without the page getting refreshed.
Is it possible to make a few changes in the scripting to make this possible?
Thanks in advance.
Here's the scripting i use:
function autoSubmit(){
var formObject = document.forms['choice_form'];
formObject.submit();
}
Here's the PHP: //I use the post values to show the form where you enter phone number and email
<?php
$value = '';
if(isset($_POST['choice'])) {
$value = $_POST['choice'];
}
?>
Here's the "radio" form:
<form name="choice_form" id="choice_form" method="post">
<table>
<tr>
<td>
<input type="radio" name="choice" <?php if ($value == 'faktura') { ?>checked='checked' <?php } ?> onChange="autoSubmit();" value="faktura"> Faktura
</td>
</tr>
<tr>
<td>
<input type="radio" name="choice" <?php if ($value == 'kort-direkt') { ?>checked='checked' <?php } ?> onChange="autoSubmit();" value="kort-direkt"> Kort / Direktbetalning
</td>
<tr>
<td>
<input type="radio" name="choice" <?php if ($value == 'delbet') { ?>checked='checked' <?php } ?> onChange="autoSubmit();" value="delbet"> Delbetalning
</td>
</tr>
</table>
</form>
If every time a payment option is selected a different form needs to appear underneath the radio buttons, you could render all forms in hidden state in individual <div>s, and then display the corresponding <div> every time an option is selected.
If you need to do something more complex than that, you will probably need to use AJAX.

Categories