mardi 2 novembre 2021

How to save the value of checkbox from a loop table in database

It does save the value of the checkbox but not in the correct row. It saves to the first rows. For example, when I checked the second checkbox, but not the first, the value will be saved to the first row, not to the second.

When I do this: enter image description here

This is what happens: enter image description here

<?php
$con = mysqli_connect("localhost","root","","mtc");
  $sql = "SELECT * FROM `june`";
  $result = mysqli_query($con,$sql);

$count=mysqli_num_rows($result);
?>

<?php
if(isset($_POST['Submit']))
{
    $count=count($_POST["id"]);
    
for($i=0;$i<$count;$i++){
    $checkbox = 0;
    if(isset($_POST['claim1'][$i]))
        $checkbox = 1;
$sql1="UPDATE june SET
        name='" . $_POST['name'][$i] . "',
        pay1='" . $_POST['pay1'][$i] . "',
        claim1='" . $checkbox . "'
        WHERE id='" . $_POST['id'][$i] . "'";
$result1=mysqli_query($con, $sql1);
}
}

?>
<main>
    <div class="content-wrapper">
        <div class="container-fluid">
            <div class="row">
<table width="500" border="0" cellspacing="1" cellpadding="0">
<form method="post" >
<tr>
<td>
<table width="500" border="0" cellspacing="1" cellpadding="0">

<tr>
<td align="center"><strong>ID</strong></td>
<td align="center"><strong>Name</strong></td>
<td align="center"><strong>Pay</strong></td>
<td align="center"><strong>Claim</strong></td>
</tr>

<?php
while($rows=mysqli_fetch_array($result, MYSQLI_ASSOC)){
?>


<tr>
<td align="center">
<input name="id[]" type="text" id="id" value="<?php echo $rows['id']; ?>">
</td>

<td align="center">
<input name="name[]" type="text" id="name" value="<?php echo $rows['name']; ?>">
</td>

<td align="center">
<input name="pay1[]" type="text" id="pay1" value="<?php echo $rows['pay1']; ?>">
</td>
<td align="center">
<input class="form-check-input" type="checkbox" id="claim1" name="claim1[]" <?php if($rows['claim1']==1) echo 'checked' ?>>
</td>
</tr>

<?php
}
?>

</table>
<input type="submit" name="Submit" value="Submit">

</td>
</tr>
</form>
</table>

</div>
</div>
</div>
</main>



show error if none of the checkboxes are selected

I have a bunch of checkbox, and need to show a warning message if none of them are selected. checkbox screenshot

Like this, WARINING message

The only rule is that, if none of the checkbox is selected it must show the warning message like above when submitting, and no warning if anyone of them is selected.

my code:

             <Form.Item
                    name="checkboxes"
                    valuePropName="checked"
                    rules={[
                      {
                        required: true,
                        message: "Please select atleast one working day",
                      },
                    ]}
                  >
                    <Checkbox onChange={this.sun}>Sun</Checkbox>
                    <Checkbox onChange={this.mon}>Mon</Checkbox>
                    <Checkbox onChange={this.tue}>Tue</Checkbox>
                    <Checkbox onChange={this.wed}>Wed</Checkbox>
                    <Checkbox onChange={this.thu}>Thu</Checkbox>
                    <Checkbox onChange={this.fri}>Fri</Checkbox>
                    <Checkbox onChange={this.sat}>Sat</Checkbox>
            </Form.Item>

In the above code, it shows the warning even if one of any checkbox is clicked.

So, I tired changing the rules to,

            rules={[
              {
                required:
                  this.state.mon &&
                  this.state.tue &&
                  this.state.wed &&
                  this.state.thu &&
                  this.state.fri &&
                  this.state.sat &&
                  this.state.sun === false,
                message: "Please select atleast one working day",
              },

but in above case, it not even showing any warning message if none of the checkbox is selected.

I use antd for UI, and checkbox is import from it. (for ref: https://ant.design/components/checkbox/ )




lundi 1 novembre 2021

Update the status of checkboxes in html via js

I want a checkbox to be updated when a value changes. In this short example, if the second checkbox is checked, then the first should be update and checked, too. The functionality of the first checkbox should still stay intact. How can I achieve this?

function onoff() {
  currentvalue = document.getElementById('onoff').value;
  if (currentvalue == "Off") {
    document.getElementById("onoff").value = "On";
    console.debug("OFF");
  } else {
    document.getElementById("onoff").value = "Off";
    console.debug("ON");
  }
}

function xy() {
  document.getElementById('onoff').value = "On";
  console.debug("xy");
}
<input type="checkbox" value="On" id="onoff" onclick="onoff();">
<input type="checkbox" value="Off" id="xy" onclick="xy();">



Js: Function that Hides Divs According to an array?

I have divs ('items') with checkboxes inside each. How can I hide the items that doesn't have the checkboxes with the selected values checked?

<div class='item'> <input type='checkbox' value='green' checked></input> <input type='checkbox' value='yelllow'></input> </div>
<div class='item'> <input type='checkbox' value='red'></input> <input type='checkbox' value='orange'></input> </div>


function MyFunction () {

const values = ['green', 'red'];
let items = document.querySelectorAll('.item');
items.forEach(items => {
    if (values.every(v => 

        items.querySelectorAll('input[type="checkbox"]:checked')[0].value.includes(v) // working (only for the first checked checkbox)

        items.querySelectorAll('input[type="checkbox"]').forEach((cb, i) => // not working
              cb[i].value.includes(v)
        )
        
    ))
    {
        items.style.display = 'block'
    }
    else {
        items.style.display = 'none'
    }
})

}

In the example above, only the first item should be shown after firing myFunction(), because it has the value green from values, and it is checked.

Thanks!




android, button and checkboxes results

Just want to start off saying I am fairly new to coding for android but I am trying my best.

What I'm trying to do is have multiple results happen when multiple check boxes are checked. its kind of hard to explain the code might help a bit more

public class MainActivity extends AppCompatActivity{

    Checkbox cb1, cb2;
    Button b1, b2, b3, b4, b5;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    cb1 = (findViewById(R.id.cb1);
    cb2 = (findViewById(R.id.cb2);
    b1 = (findViewById(R.id.b1);
    b2 = (findViewById(R.id.b2);
    b3 = (findViewById(R.id.b3);
    b4 = (findViewById(R.id.b4);
    b5 = (findViewById(R.id.b5);
}

@Override
    public void onCheckedChanged (CompoundButton buttonView,boolean isChecked) {

if (cb1.isChecked()) {
    b1.setBackgroundColor(ContextCompat.getColor(this, R.color.grey));
    b2.setBackgroundColor(ContextCompat.getColor(this, R.color.grey));
    b3.setBackgroundColor(ContextCompat.getColor(this, R.color.grey));
}
if (cb2.isChecked()) {
    b3.setBackgroundColor(ContextCompat.getColor(this, R.color.grey));
    b4.setBackgroundColor(ContextCompat.getColor(this, R.color.grey));
    b5.setBackgroundColor(ContextCompat.getColor(this, R.color.grey));
}
else {
    b1.setBackgroundColor(ContextCompat.getColor(this, R.color.green));
    b2.setBackgroundColor(ContextCompat.getColor(this, R.color.green));
    b3.setBackgroundColor(ContextCompat.getColor(this, R.color.green));
    b4.setBackgroundColor(ContextCompat.getColor(this, R.color.green));
    b5.setBackgroundColor(ContextCompat.getColor(this, R.color.green));
}
}

so basically I'm trying to get it so if only Checkbox 1 is checked, we see the background of buttons 1-3 change and if only checkbox 2 is pushed then buttons 3-5 are changed, and if both are pushed then all buttons are changed. This I am achieving no issue.

The problem I'm having is trying to get them to revert. so for example say I have both cb1&2 checked then I uncheck cb2 the field stays as if cb1&2 are pushed, I'm trying to get it so when cb2 is unchecked the result for only having cb1 checked comes back (buttons 4 & 5 would change back to green)

I've checked all over the internet for this and cant find a answer or at lease a path to point me in the right direction.

I tried to use a "switch/case" set up which didn't work (found out why after) then I tried to loop the switch, again didn't work. I tried using a else if statement then I found out that's basically the same thing as a switch I thought about maybe using a string array but I'm not 100% sure if that would have the desired result.

I still have my code from the failed switch/case attempt if that is needed




Oracle Apex Checkboxes doexn't update with Dynamic Actions

I'm using Apex 19.1 on an 18.c database. I have a page with three page items and three corresponding Dynamic Actions (DA's). When either P273_START_DATE or P273_END_DATE are changed, their dynamic actions perform as expected. They refresh a chart region. I also have a third page item - P273_WEEKDAY_SELECTOR. The DA associated with it is the problem. The page item has the following characteristics:

Identification / Type = checkbox.
List of Values / Type = Static Values
List of Values / Static Values = Mon, Tue, Wed, Thu, Fri
Source / Used = Only when current value in session state is null
Source / Maintain Session State = Per Session (Disk)

The page item P273_WEEKDAY_SELECTOR is contained in the region From / To Date Selection along with the two date fields.
The Chart region, Weekday Attendance has the following notable characteristic

Source / Page Items to Submit = P273_WEEKDAY_SELECTOR

The Dynamic Action has the following Characteristics

Identification / Name = P273_WEEKDAY_SELECTOR_CHANGE
When / Event = Change
When / Selection Type = Item(s)
When / Items = P273_WEEKDAY_SELECTOR

True Action #1

Identification / Action = Execute PL/SQL Code
Settings / PL/SQL Code = null;
Settings / Items to Submit = P273_WEEKDAY_SELECTOR
Settings / Items to Return = P273_WEEKDAY_SELECTOR
Settings / Suppress Change Event = No
Execution Options / Event = P273_WEEKDAY_SELECTOR_CHANGE

True Action #2

Identification / Action = Refresh
Affected Elements / Selection Type = Region
Affected Elements / Region = Weekday Attendance
Execution Options / Event = P273_WEEKDAY_SELECTOR_CHANGE 

In trying to solve this problem, I created a temporary region TEST_REGION with a page item P273_WEEKDAY_TEST. I added a temporary True Action #3 to update TEST_REGION. The page item P273_WEEKDAY_TEST has the following characteristics:

Source / Type = SQL Query (return colon separated value)
Source / SQL Query = Select '''' || regexp_replace(:P273_weekday_selector, ':', ''', ''' ) || '''' from dual;

When I change the selected checkboxes the page item P273_WEEKDAY_TEST doesn't get updated with the DA for the checkbox. However, if I press the browser's refresh button, the item P273_WEEKDAY_TEST gets updated with the proper values e.g. 'Mon', 'Tue', 'Fri'

This setup doesn't seem to fire on change of the checkbox, yet the other DA's fire when either date is changed. How do I made a DA work for change of a checkbox item?




how to save the checkbox state in sqlite android

I have a dialog containing a dynamic set of checkboxes. I have set up the db, the only problem is that whenever I select a checkbox, it works fine but it does not stay checked. When I open the dialog, that checkbox is not checked anymore.

    case R.id.dialog:

       dialog = new Dialog(this);
       dialog.setContentView(R.layout.dialog);

       dialog.show();
                
       for (int IndexNo = 0; IndexNo <  tgroup.get(groupIndex).length; IndexNo++){ 
                        
           checkBox1 = new CheckBox(this);
           checkBox1.setId(IndexNo);
           checkBox1.setText(IndexNo);
                        
           checkBox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                 @Override
                 public void onCheckedChanged(CompoundButton buttonView, boolean 
                               isChecked) {
                   if (isChecked) {
                       checkbox_position = buttonView.getId();
                       boolean success = db.updateCheckboxPosition(
                                            getId(),
                                            String.valueOf(checkbox_position), 1);
                                    
                       if (success) {
                          setItem();
                       }
                  }  else {
                       boolean success = db.updateCheckboxPosition(getId(),
                               String.valueOf(checkbox_position), 0);
                  }
                            
              });                
          }

 }

this is the update in db

public boolean updateCheckboxPosition(String _id,String checkbox_pos, int isChecked){
    SQLiteDatabase database = this.getWritableDatabase();
    String selection = "_id = ?";
    ContentValues contentValues = new ContentValues();

    contentValues.put(_ID,_id);
    contentValues.put(CHECKBOX_POS,checkbox_pos);
    contentValues.put(CHECKBOX_STATUS,isChecked);

    long result = database.update(TABLE_NAME , contentValues , _ID + " =? " ,
            new String[]{_id});
    if (result == -1){
        return false;
    } else {
        return true;
    }

}

By default, there is no checkbox selected. The above works fine but does not keep the selected box checked. When you select a checkbox, it checks the clicked one and last checkbox. When you open the dialog after no checkbox is checked.