Show/hide input after click in checkbox - php

I'm trying to Show/hide my input after check/uncheck a checkbox but I getting this info from DB so, I'm using PHP to add 'checked' attribute to my input.
jQuery code is working, but when I refresh the page, my input doesn't show even with attribute checked "enabled".
PHP code
<label>Send cash?</label>
<input class="reg_cash" type="checkbox" name="reg_cash" value="1"
<?php echo ($configs->reg_cash) ? 'checked' : '' ?>>
<div class="reg_cash_amount">
<label>Amount of cash</label>
<input type="text" name="reg_cash_amount" id="coupon_field"/>
</div>
Jsfiddle: https://jsfiddle.net/qmmg3qwo/

try this to hide/show the input field based on database values.
php code
<label>Send cash?</label>
<input class="reg_cash" type="checkbox" name="reg_cash" value="1"
<?php echo ($configs->reg_cash) ? 'checked' : '' ?>>
<?php if($configs->reg_cash) { ?>
<div class="reg_cash_amount">
<label>Amount of cash</label>
<input type="text" name="reg_cash_amount" id="coupon_field"/>
</div>
<?php } ?>
Jquery code
$(".reg_cash").click(function () {
if ($(this).is(":checked")) {
$(".reg_cash_amount").show();
} else {
$(".reg_cash_amount").hide();
}
});

Related

jquery appending all the values

Actually I want to append to div only checked values but it is appending unchecked values also. I used in_array function to check conditions if value is checked means append only checked value not all but it is appending all the values but its checking only value please somebody help me out..
Below is HTML and PHP code:-
<div class="flDrop">
<div class="flDropDiv">
<?php
foreach ($filter_group['filter'] as $filter) { ?>
<?php
if(in_array($filter['filter_id'],$filter_category))
{ ?>
<input name="filter[]" type="checkbox" value="<?php echo $filter['filter_id']; ?>" checked> <?php echo $filter['name'];?>
<?php } else {?>
<input name="filter[]" type="checkbox" value="<?php echo $filter['filter_id']; ?>"> <?php echo $filter['name'];?>
<?php }?>
<?php }?>
</div>
</div>
Jquery code:-
setTimeout(function(){
var filter_len = $('input[name^=\'filter\']:checked').length
// alert(filter_len);return false;
$("#auto_filter_values").empty();
if(filter_len>1){
/*$("#auto_filter_values").append('<div class="afr clearall">Clear All filters</div>');*/
}
$('input[name^=\'filter\']:checked').each(function(element) {
$("#auto_filter_values").append('<div class="afr fSbtn" id="fSfilter'+this.value+'" data-val="'+this.value+'">'+$(this).parent().text().replace(/\(([A-Za-z0-9 ]+?)\)/, '')+'<span class="fSc"></span></div>');
});
}, 100);
Working fine with some small changes (an example code):-
setTimeout(function(){
$.each($("input[name='filter[]']:checked"), function(){ //change here
$("#auto_filter_values").append('<div class="afr fSbtn" id="fSfilter'+this.value+'" data-val="'+this.value+'">'+$(this).attr('data-id')+'<span class="fSc"></span></div>'); // changes here
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="flDrop">
<div class="flDropDiv">
<input name="filter[]" type="checkbox" value="1" data-id ="Black" checked> Black <br>
<input name="filter[]" type="checkbox" value="2" data-id ="Black">Red<br/>
<input name="filter[]" type="checkbox" value="3" data-id ="Pink" checked>Pink<br/>
<input name="filter[]" type="checkbox" value="4" data-id ="Blue">Blue<br/>
</div>
</div>
<div id = "auto_filter_values" style ="margin-top:30px;"></div>
Note:- you need to add data-id attribute with you color name on check-boxes and then you can get easily

Checkbox is checked when go to Edit page - PHP HTML

I have a situation where I have create a new information after submit a new info, the information is a success into the database. But when I click on the 'Edit' page, all other information is displayed. But for the input type checkbox, the information does not display at all.
<tr>
<td>Transaction</td>
<td>
<div class="checkbox check-default check-success">
<input id="f1" type="checkbox" value="1" name="Tbox" <?= ( $modules['transaction']=='1'? "checked" : "") ?>>
<label for="f1"></label>
</div>
</td>
</tr>
I wonder what I am missing. Please help, thanks.
Remove the value=1 in your code.
Use this code below
<input id="f1" type="checkbox" name="Tbox" <?= ( $modules['transaction']=='1'? "checked" : "") ?>>
You specified the value of a default input value = 1, removes value = 1
try
<input id="f1" type="checkbox" name="Tbox" <?= ( $modules['transaction']=='1'? "checked" : "") ?>>

storing checkbox on multi step form with php session

i have a form in three steps, from step 1 to step two there is a checkbox, i would like to keep the checkbox value in a session and also to show if it's checked or unchecked even if the user goes to step 3 of the form then comes back to step two,
right now on form submit i have all the $_POST vars in $_SESSION vars but i cant make it work with checkboxes, this is what i got right now:
<input type="checkbox" value="<?php echo isset($_POST['afficher_ddn'])? "1":"0"; ?>"
style="margin-left: 20px;" name="afficher_ddn" id="afficher_ddn"
<?php echo $_SESSION['afficher_ddn']=="1" ? "checked" : ""; ?> />
but this doesnt work.
<input type="checkbox" value="1" style="margin-left: 20px;" name="afficher_ddn" id="afficher_ddn"
<?php echo isset($_SESSION['afficher_ddn']) && $_SESSION['afficher_ddn'] == "1" ? 'checked="checked"' : ''; ?> />
For one, you don't change the value of the checkbox. If it is not checked it won't pass to the post vars. See here: Does <input type="checkbox" /> only post data if it's checked?
Now, the current standard for checked is checked="checked" I have no idea where or why this standard became one at the firm I work for, but it's what we do so I relay that here.
If the code provided here does not work for you, I would var_dump($_SESSION) to make sure it's set as well as check the actual HTML in something like firebug (firefox) or developer tools in chrome. Sometimes goofiness happens and checked="checked" is actually set in the html but doesn't display in browser. In those times I usually yank out some hair and clear caches. then it usually clears up.
Your code is fine.Possibly there is some problem in passing the the value in session as i am checking the code manually it is working perfectly:
use print_r($_SESSION) for testing and post the output;If possible provide all the forms:
Try Below Code:
<input type="checkbox" value="1"
style="margin-left: 20px;" name="afficher_ddn" id="ddn"
<?php echo $_SESSION['afficher_ddn']=="1" ? "checked" : ""; ?> />
try this example separetly
Make a page test.php as:
<?php
session_start();
?>
<form methos='post' method='post' action='test1.php'>
<input type="checkbox" value="1"
style="margin-left: 20px;" name="afficher_ddn" id="ddn"
<?php echo $_SESSION['afficher_ddn']=="1" ? "checked" : ""; ?> /><br>
Name:<input type='text' name='f1' value='<?php echo $_SESSION['f1'] ?>' ><br>
Select:<select name='s1'>
<option value="" <?php echo $_SESSION['s1']=="" ? "selected" : ""; ?>></option>
<option value="1" <?php echo $_SESSION['s1']=="1" ? "selected" : ""; ?>>1</option>
<option value="2" <?php echo $_SESSION['s1']=="1" ? "selected" : ""; ?>>2</option>
</select><br>
Test:
<textarea name='tex1'><?php echo $_SESSION['tex1']; ?></textarea>
<input type='submit' value='submit'>
</form>
Make a second page test1.php as:
<?php
session_start();
print_r($_POST);
echo $_SESSION['afficher_ddn']=$_POST['afficher_ddn'];
echo $_SESSION['f1']=$_POST['f1'];
echo $_SESSION['s1']=$_POST['s1'];
echo $_SESSION['tex1']=$_POST['tex1'];
?>
<form methos='post' action='third.php'>
<a href='test.php'>Step1</a>
</form>
and Check

Radio button checked issue

My question was about the browser matter as far as I see.
I asked is there any solution to tell browser to move checked="checked" between radio buttons. I thought there will be a quick solution for this but this case is just confusing. I changed my code now. I use select list element now.
I have a form, and I get the actual settings with php, but the matter is this:
When the radio button 1 comes checked="checked" if that is actual, after it when I change to other option in post I get checked="checked"'s value, I was wondering is there a solution for this or Javascript is needed something like onclick"this.checked otherone uncheck" ?
if($type==1) { $like = 'checked="checked"'; } else { $ref='checked="checked"'; }
<form method="post" action="process.php" id="fbform" name="fbform">
<div class="radio">
<p>Like button: </p>
<input type="radio" name="tipi" <?=$like?> value="1" />
</div>
<div class="radio1">
<p>Link:</p>
<input type="radio" name="tipi" <?=$ref?> value="2" />
</div>
</form>
Process.php file code
<?php
$mysql->update('settings'," tipi='".$_POST['tipi]."' ");
?>
checked is a marker tag - it does not care , as long if the word 'checked' is there.
Here is what I would do:
<div class="radio">
<p>Like button: </p>
<input type="radio" name="tipi" id="tipi1" value="1"<?php if ($type == 1) echo ' checked="checked"'; ?> />
</div>
<div class="radio1">
<p>Link:</p>
<input type="radio" name="tipi" id="tipi2" value="2"<?php if ($type != 1) echo ' checked="checked"'; ?> />
</div>
I have changed the id's because you had two elements with the same id, which will never work, even if those elements are part of the same group.
You can do better with this. Also, IDs are used only for CSS or JS, and moreover must be UNIQUE.
<div class="radio">
<p>Like button: </p>
<input type="radio" name="tipi" id="tipi" <?php echo ($type == '1')?'checked="checked"' : '';?> value="1" />
</div>
<div class="radio1">
<p>Link:</p>
<input type="radio" name="tipi" id="tipi2" <?php echo ($type=='2')?'checked="checked"' : '';?> value="2" />
</div>
Then you access them in php
$tipi = $_POST['tipi']; //either 1 or 2
you need very simple changes in your code..See below code:
first of all you need to define variables at very top of the page/Code:
$like = "";
$ref = "";
Now your code will working good as per my knowledge..
Thanks.
Ghostology: Not sure about your query ... But all I can do is explain you the working of the radio button.
suppose there are two radio buttons inside a form:
<input type="radio" name="radioTest" value="1" checked />
<input type="radio" name="radioTest" value="0" />
Here checked specifies the button which will be checked(or turned on) during page load.
Now suppose we check the second button and submit this form, now we get the value 0 against the parameter 'radioTest' as radio button with value 0 is checked.
Now when page loads again with some value of radio buttons. You would want the proper radio button to be checked.
For this case I guess u can use if/else condition like:
<% if(radioTest.value == 1){ %>
<% }else{ %>
<% } %>

keeping radio button value after post

HI
i'm using a php page and i need to keep the value of and check box and radio button (checked or not checked) after post page.
how could i make it?
thanks
First get the radio button value.
$radiobuttonvalue = $_POST['radiobuttoname']
Then for each radio button with the same name, do this
<input type="radio" name="radiobuttonname" value="value" id="radiobuttonname" <?php if($radiobuttonvalue == "value") { echo 'checked="checked"';} ?>
You need something like:-
<?php
$postCheckboxName = '';
if (isset($_POST['checkbox_name']) || 'any_value' == $_POST['checkbox_name']) {
$postCheckboxName = ' checked="checked"';
}
?>
<input type="checkbox" name="checkbox_name" value="any_value"<?php echo $postCheckboxName;?> />
<?php
$postRadioName = '';
if (isset($_POST['radio_name']) || 'any_other_value' == $_POST['radio_name']) {
$postRadioName = ' checked="checked"';
}
?>
<input type="checkbox" name="radio_name" value="any_other_value"<?php echo $postRadioName;?> />
This code should get you going. I'm basically checking whether the POST value of either the checkbox / radio element is set or not & whether the corresponding element's value matches with my respective element's value or not.
Hope it helps.
Something like this:
<?php if (isset($_POST['checkbox_name']))?>
<input type="checkbox" checked="checked" value="<?php echo $_POST['checkbox_name'];?>" />
<?php} ?>
<?php if (isset($_POST['radio_name']))?>
<input type="radio" checked="checked" value="<?php echo $_POST['radio_name'];?>" />
<?php} ?>
What happens is that you check if the input variables are in the $_POST and if so you add checked="checked" to the input fields to make them checked.
This worked for me, and is self explanatory
sample code usage:
<div class="form-group">
<label class="radio-inline">
<input type="radio" name="time" value="lunch" <?php if (isset($_POST[ 'time']) && $_POST[ 'time']=='lunch' ){echo ' checked="checked"';}?>>Lunch</label>
<label class="radio-inline">
<input type="radio" name="time" value="dinner" <?php if (isset($_POST[ 'time']) && $_POST[ 'time']=='dinner' ){echo ' checked="checked"';}?>>Dinner</label>
</div>

Categories