How to show echo "checked" edit in the checkbox? - php

I'm writing a system and have a checkbox in a form.
My problem is, when i press the edit button, the checkbox cannot show me the tick in the checkbox, I have tried to change checkbox type to text, it can show me the value. I have set the value if value = 1 is tick, if value = 0 is no tick. How can shows up tick in the form? Anyone can guide me to solve it?
Below is my coding:
Checkbox
<div class="form-group col-lg-6">
<label class="control-label col-lg-4">Pricing<span style="color:red;"> </span></label>
<div class="col-lg-8">
<input type="text" name="rm_option" id="rm_option" value="1" <?php if(_POST[$value]=='1'){echo "checked='checked'";} ?> ><strong> RM </strong></input>
<input type="text" name="point_option" id="point_option" value="1" <?php if(_POST[$value]=='1'){echo "checked='checked'";} ?>><strong> Full Point </strong></input>
<input type="text" name="partial_option" id="partial_option" value="1" <?php if(_POST[$value]=='1'){echo "checked='checked'";} ?>><strong> Partial Point + RM </strong></input>
</div>
</div>
Checkbox function
<?php
$sql = "select * from promotion_list where id=" . $_GET['id'];
$arr_sql = db_conn_select($sql);
foreach ($arr_sql as $rs_sql) {
foreach ($rs_sql as $key => $value) {
?>
$("#<?php echo $key ?>").val("<?php echo $value?>");
<?php
}
?>
When I press the edit button, other column can show up in the form, only the checkbox cannot show me the tick. Below is the output picture:
Output
If I change Checkbox type to text, below is the output (Prove inside the checkbox got value)
Output 2
I have stuck in this problem already 1 week, hope someone coding hero can guide me to solve this problem. Thanks a lot.

Add below code exactly after this line:
$("#<?php echo $key ?>").val("<?php echo $value?>");
it became:
$("#<?php echo $key ?>").val("<?php echo $value?>");
<?php if($value == 1){ ?>
$("#<?php echo $key ?>").attr("checked", true).prop("checked", true);
<?php } ?>
its clear that you are using jquery to work on the checkbox.

Related

Checking all checkboxes are checked using PHP

I have few checkboxes, I want to find whether all the checkboxes are checked and if yes return a message.
<label class="control-label col-md-3">L4 Deliverables</label>
<?php
while($subd_row=$subd_result->fetch_assoc()){
if($sub_row['selected'] == 1)
{
?>
<input class="flat" type="checkbox" name="L4d[]" value="<?php echo $subd_row['d_name'];?>" checked><?php echo $subd_row['d_name'];?></input>
}
Using the above code the checkboxes are displayed. The message could be for example: " 14 checkboxes are checked".
You can use $i to increment when it goes in that if statement it will increment,
<label class="control-label col-md-3">L4 Deliverables</label>
<?php $i = 0;
while ($subd_row = $subd_result->fetch_assoc()) {
if ($sub_row['selected'] == 1) {
$i++;
?>
<input class="flat" type="checkbox" name="L4d[]" value="<?php echo $subd_row['d_name']; ?>" checked><?php echo $subd_row['d_name']; ?></input>
<?php
}
}
?>
<label><?php echo ($i <= 1 ? "$i checkbox is ": "$i checkboxes are ")."checked"; ?></label>
On the backend, you can always check the length of the variables/array passed by a form.
L4d[] will have the values of checked checkboxes only.
You can simply check as:
if(count($_POST['L4d']))== 14) {...}
If you want something like an alert box to be popped up when all the checkboxes are checked, then you may call a javascript function 'onChange' of your checkbox field

PHP:getting checkbox value and text field value in on form submit

I am building form with checkbox and texfield like this
$sector = [
['name'=>'Electrical','id'=>1],
['name'=>'Automobile','id'=>2],
['name'=>'Repairing','id'=>3],
['name'=>'Blacksmith','id'=>4]
]
?>
<?php for each($sector as $value){ ?>
<input type="checkbox" name="work_experience_sector[]" value=" <?php echo $value['id'] ?>">
<input type="text" name="work_experience_sector_experience[]" class="form-control border-input">
<?php }?>
I would like to checked category value along with experience year something like that after form submit
eg:
category-1 and experience-2
category-2 and experience-4
I've tried this
foreach($_POST['work_experience_sector_experience'] as $key=>$value){
echo 'category'.$_POST['work_experience_sector'][$key].'expe'.$value.'<br>';
}
But the problem is not getting the values in proper order

Input checkbox when i edit user no change

When i want to edit my user on inactive is it no working, but when i editing my users to inactive to active, it is working.
This is my PHP code: https://pastebin.com/iBaDxH2u
<input class="form-control" type="checkbox" name="actif" id="actif" value="<?php echo $userinfo['actif']; ?>" <?php if ($userinfo['actif'] == "1") { echo "checked"; } ?>>
<input type="hidden" name="actif" value="1" />
I dont solve the problem...
Thx
This sounds like it has something to so with how checkboxes work. When you submit the form, if the checkbox is ticked then the page you submit the form to will receive [actif] => on. If you submit with the checkbox unticked then the page will not receive [actif] => off, it will receive an empty array []. actif will not be set. Something like this might make it more obvious what is going on.
<?php
if (isset($_GET['actif']) && $_GET['actif']=="on")
{
echo ("The box was ticked");
$ticked = 'checked';
}
else
{
echo ("The box was not ticked");
$ticked = '';
}
echo ("<pre>");
print_r($_GET);
?>
<form>
Actif <input type='checkbox' name='actif' id='actif' <?=$ticked?>>
<button type='submit'>Click me</button>
</form>

Show whether dynamic radio button is checked or not

<?php $result2 = db_getsub( array('campaigns_id' => $SYS_campaign) );
if (!is_null($result1)){
while ( $row = $result2->fetch_object() )
{
?>
<div class="item">
<div style="text-align: left;">
<div class="choicetitle">
<input type="radio" id="Radio1" checked="checked" title="" name="subtype" value="<?php echo $row->subid; ?>" /><?php echo $row->sub_name; ?></div>
<div class="choicedesc descrip">
<?php echo $row->gavdescrep; ?> </div>
<!-- <div class="choicelesmer">
Les mer
</div>-->
<div class="choicedesc1">
<?php echo $row->gavdescrep; ?> </div>
</div>
<span><?php
$value=$row->price;
if($value != 0)
{
echo $row->price; ?>,-
<?php }else { echo ""; } ?> </span>
<span class="bgcolor discprice">
<?php echo $row->pricediscount; ?>,-
</span>
</div>
<?php } } ?>
I want to show default 1st radio button checked now its showing last button checked as it is coming dynamically any help???
You are using checked="checked" for all your radio buttons in the loop. So, last assigned radio button retains the checked property. To solve the problem, Conditionally set checked="checked" only for the first looping
if(loopingFirstTime){
$checked = ' checked="checked" ';
}else{
$checked = ' ';
}
and then ....
<input type="radio" name="radio1" '.$checked.' />
Not tested this .... please check the syntax
Use checked=checked property to make a radio button checked by default:-
Blue
http://www.w3schools.com/jsref/prop_radio_defaultchecked.asp
And to handle that with jquery:-
jQuery("#radio_1").attr('checked', 'checked');
How to check a radio button with jQuery ?
Like the other guys have already said, you have only implemented one radio button.
You can try jsfiddle.net for example to try out you code and quickly see your changes.
Btw: I'm not quite sure what you're trying to do in you last row: Is that a leftover or those this belong to the wrapped-around code?

How to remember checkbox input in PHP Forms

For usability purposes I like to set up my form fields this way:
<?php
$username = $_POST['username'];
$message = $_POST['message'];
?>
<input type="text" name="username" value="<?php echo $username; ?>" />
<textarea name="message"><?php echo $message; ?></textarea>
This way if the user fails validation, the form input he entered previously will still be there and there would be no need to start from scratch.
My problem is I can't seem to keep check boxes selected with the option that the user had chosen before (when the page refreshes after validation fails). How to do this?
My first suggestion would be to use some client-side validation first. Maybe an AJAX call that performs the validation checks before continuing.
If that is not an option, then try this:
<input type="checkbox" name="subscribe" <?php echo (isset($_POST['subscribe'])?'checked="checked"':'') ?> />
So if subscribe is = 1, then it should select the box for you.
For Example, consider the following code for checkbox :-
<label for="course">Course:</label>
PHP<input type="checkbox" name="course[]" id="course" <?php if ((!empty($_POST["course"]) && in_array("PHP", $_POST["course"]))) {
echo "checked";
} ?> value="PHP" />
Then, this would remember the checkbox of "PHP" if it is checked, even if the validation for the page fails and so on for "n" number of checkboxes as shown below:-
<label for="course">Course:</label>
PHP<input type="checkbox" name="course[]" id="course" <?php if ((!empty($_POST["course"]) && in_array("PHP", $_POST["course"]))) {
echo "checked";
} ?> value="PHP" />
HTML<input type="checkbox" name="course[]" id="course" <?php if ((!empty($_POST["course"]) && in_array("HTML", $_POST["course"]))) {
echo "checked";
} ?> value="HTML" />
CSS<input type="checkbox" name="course[]" id="course" <?php if ((!empty($_POST["course"]) && in_array("CSS", $_POST["course"]))) {
echo "checked";
} ?> value="CSS" />
Javascript<input type="checkbox" name="course[]" id="course" <?php if ((!empty($_POST["course"]) && in_array("Javascript", $_POST["course"]))) {
echo "checked";
} ?> value="Javascript" />
And most importantly, do not forget to declare the "course" variable as an array at the start of the code as shown below :-
$course = array();
I have been battling how to create sticky check box (that is able to remember checked items any time you visit the page). Originally, I get my values from a database table. This means that my check box value is entered to a column on my db table.
I created the following code and it works just fine. I did not want to go through that whole css and deep coding, so...
CODE IN PHP
$arrival = ""; //focus here.. down
if($row['new_arrival']==1) /*new_arrival is the name of a column on my table that keeps the value of check box*/
{$arrival="checked";}// $arrival is a variable
else
{$arrival="";};
echo $arrival;
<b><label for ="checkbox">New Arrival</label></b>
<input type="checkbox" name ="$new_arrival" value="on" '.$arrival.' /> (Tick box if product is new) <BR><BR>
<input type="checkbox" name="somevar" value="1" <?php echo $somevar ? 'checked="checked"' : ''; ?>/>
Also, please consider sanitising your inputs, so instead of:
$somevar = $_POST['somevar'];
...it is better to use:
$somevar = htmlspecialchars($_POST['somevar']);
When the browser submits a form with a checked checkbox, it sends a variable with the name from the name attribute and a value from the value attribute. If the checkbox is not checked, the browser submits nothing for the checkbox. On the server side, you can handle this situation with array_key_exists(). For example:
<?php
$checkedText = array_key_exists('myCheckbox', $_POST) ? ' checked="checked"' : '';
?>
<input type="checkbox" name="myCheckbox" value="1"<?php echo $checkedText; ?> />
Using array_key_exist() avoids a potential array index undefined warning that would be issued if one tried to access $_POST['myCheckbox'] and it didn't exist.
You may add this to your form:
<input type="checkbox" name="mycheckbox" <?php echo isset($_POST['mycheckbox']) ? "checked='checked'" : "" ?> />
isset checks if a variable is set and is not null. So in this code, checked will be added to your checkbox only if the corresponding $_POST variable has a value..
My array has name="radioselection" and value="1", value="2", and value="3" respectively and is a radio button array... how to I check if the radio value is selected using this code
I tried:
<?php echo (isset($_POST['radioselection']) == '1'?'checked="checked"':'') ?> />

Categories