Viewed   75 times

I noticed that PHP seems to return only values of checked checkboxes. I would like to see a list of checkboxes, not just values of checked checkboxes. Is there a way to detect variables of unchecked boxes?

I asked because I want to be able to update settings. For example, I have a few options that are already checked but if an user decides to uncheck an option, I need to know that unchecked value so I can update the option to be disabled.

 Answers

5

I just ran into this problem myself. I solved it by adding a duplicate hidden field with the same name. When the browser sends this information, the second field overrides the first (so ensure that the hidden field comes first).

<input type="hidden" name="foo" value="">
<input type="checkbox" name="foo" value="bar">

If the checkbox is not checked you get:

$_REQUEST[ 'foo' ] == ""

If the checkbox is checked you get:

$_REQUEST[ 'foo' ] == "bar"
Monday, August 29, 2022
1

Give the checkboxes names as array like

<input type = "checkbox" value = "box" name = "checkbox[]"/>

And after submit try like

$checked_arr = $_POST['checkbox'];
$count = count($checked_arr);
echo "There are ".$count." checkboxe(s) are checked";

Note : And based on the method that your form submit using...whether it is $_GET or $_POST you need to use $_POST['checkbox'] for POST method and $_GET['checkbox'] for the GET method.

Saturday, December 24, 2022
 
sgeddes
 
1

change

<input type="checkbox" name="txtCheck" value="<?php echo $_POST['txtCheck'];?>"  /><br />

to

<input type="checkbox" name="txtCheck" value="your value" <?php if(isset($_POST['txtCheck'])) echo "checked='checked'"; ?>  /><br />

this will keep checkbox checked..

Friday, December 23, 2022
4

The below code working fine for me so you can try this

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/select_btn"
    android:text="Select"/>

<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/listview"/>

list_row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="4dp"
android:orientation="horizontal" >

<CheckBox
    android:id="@+id/cbBox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical" >
</CheckBox>

<TextView
    android:layout_height="wrap_content"
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_gravity="center_vertical"
    android:ellipsize="marquee" />

Service

import java.io.Serializable;
public class Service implements Serializable
{

private static final long serialVersionUID = 1L;

private String name;
private boolean selected;

public Service() {
    // TODO Auto-generated constructor stub
}

public Service(String name) {
    super();
    this.name = name;
}


public String getName() {
    return name;
}

public boolean isSelected() {
    return selected;
}

public void setSelected(boolean selected) {
    this.selected = selected;
}

public void setName(String name) {
    this.name = name;

}

}

ServiceAdapter

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
public class ServiceAdapter extends BaseAdapter {
ArrayList<Service> actorList;
LayoutInflater vi;
Context context;

public ServiceAdapter(Context context,ArrayList<Service> objects) {
    this.context= context;
    this.vi = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    this.actorList = objects;
}


@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // convert view = design
    final ViewHolder holder;
    if (convertView == null) {
        holder = new ViewHolder();
        convertView = vi.inflate(R.layout.list_row, null);

        holder.tvName = (TextView) convertView.findViewById(R.id.textView1);
        holder.checkBox = (CheckBox) convertView.findViewById(R.id.cbBox);

        convertView.setTag(holder);
    } else
        holder = (ViewHolder) convertView.getTag();

    holder.tvName.setText(actorList.get(position).getName());
    holder.checkBox.setChecked(actorList.get(position).isSelected());

    holder.checkBox.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            boolean isSelected = ((CheckBox)v).isChecked();
            actorList.get(position).setSelected(isSelected);
        }
    });

    return convertView;

}


static class ViewHolder {

    public TextView tvName;
    public CheckBox checkBox;

}


@Override
public int getCount() {
    // TODO Auto-generated method stub
    return actorList.size();
}


@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return actorList.get(position);
}


@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

public ArrayList<Service> getSelectActorList(){
    ArrayList<Service> list = new ArrayList<>();
    for(int i=0;i<actorList.size();i++){
        if(actorList.get(i).isSelected())
            list.add(actorList.get(i));
    }
    return list;
}

}

MainActivity

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

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

    final ListView listView = (ListView)findViewById(R.id.listview);
    listView.setAdapter(new ServiceAdapter(this,sampleData()));

    Button btn = (Button)findViewById(R.id.select_btn);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ArrayList<Service> actorList = ((ServiceAdapter)listView.getAdapter()).getSelectActorList();
            Toast.makeText(MainActivity.this,""+actorList.size(),Toast.LENGTH_LONG).show();
        }
    });

}


public ArrayList<Service> sampleData(){
    ArrayList<Service> dataList = new ArrayList<>();
    for(int i=0;i<30;i++){
        Service servic = new Service();
        servic.setName("Test"+i);
        dataList.add(servic);
    }

    return dataList;
}

}

Tuesday, October 18, 2022
 
4

tag.find(text=True) would return the first matching text node. Use .get_text() instead:

>>> from bs4 import BeautifulSoup
>>> data = '<td rowspan="2" style="text-align: center;"><a href="/wiki/Glenn_Miller" title="Glenn Miller">Glenn Miller</a> &amp; His Orchestra</td>'
>>> soup = BeautifulSoup(data, "html.parser")
>>> tag = soup.td
>>> tag.get_text()
'Glenn Miller & His Orchestra'
Thursday, October 27, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :