Jump to content

Handling multiple checkboxes checkboxes?


DCM

Recommended Posts

Hi, my PHP code currently outputs the results from a users search by querying a backend postgresql database.

 

As result/row is retuned to the user i would like to be able to detect whether or not they have checked a checkbox at the end of each rown in which case multiple rows can then be deleted from the database upon the user clicking a 'delete multiple records' button.

 

I have no problem in being able to display the checkboxes on the webpage but i am a little unsure as to how to refernce them and detect which ones the user has checked.

 

Given my database stores hostnames/IP addresses would it be best to name each checkbox to reflect the hostname or name the value it returns when checked to reflect the hostname.

 

Given the above and that I also store the results of a users search in a

$_SESSION['hosts'][][] array how can I then tie the two together?

 

Thanks for reading.

Link to comment
Share on other sites

I still cant seem to get this working, could anyone please point out what i'm doing wrong.

 

Given i have a session array along the lines of:

 


$_SESSION['hosts'][$x][0] = "HostA" // stores hostname 
$_SESSION['hosts'][$x][1] = "IP address" //stores IP address
$_SESSION['hosts'][$x][2] = "VLAN" // stores VLAN

// in reality the data is loaded into the above array from a postgresql DB.


// When cylcing through to output the users search results I tag a check box onto the end
// of each row of data returned in a for loop as follows

for ($x=0; $x < $result_count; $x++)
{
//other code....
echo "<td><input type='checkbox' name='chbox_delete_host_record[]' value='".$_SESSION['hosts'][$x][0]."'></td>";

}


//Upon the user clicking a delete_multiple_hosts button i then try to detect which checkboxes have been aciviated with:


if (isset ($_POST['delete_multiple_hosts']))
{

for ($x=0; $x < $result_count; $x++)
{
	if (isset ($_POST['chbox_delete_host_record[$x]']))
	{
		echo "checkbox has been activiated for host".$_SESSION['hosts'][$x][0]; 

		//obviously other code would go here i'm just trying to detect which boxes have been checked at present
	}

}

}


Link to comment
Share on other sites

You really should have a unique id for each record in your table.  If so, then use that as the value of the checkbox array and delete using that id.  If not, then if the IP or hostname is unique to the table use that.  Make sure to use a checkbox array [].

 

<input type="checkbox" name="host[]" value="5">

or

 

<input type="checkbox" name="host[]" value="127.0.0.1">

 

Then you can implode the $_POST['host'] array and use that in your delete:

 

$hosts = array_map('pg_escape_string', $_POST['host'] );
$list = "'" . implode("','", $hosts) . "'";
$query = "DELETE FROM table_name WHERE id IN ($list)"

 

 

Link to comment
Share on other sites

Thanks for the reply.

 

At the moment i cannot even seem to get the contents of my $_POST['chbox_delete_host_record']

 

I have tried various ways, such as testing if it isset, doing a foreach loop but all the time no results are returned.

 

Is there a flaw in the way i am trying to detect which checkboxes have been ticked?

 

The unique value I am assigning is the hostname which is stored in a $?S_SESSION['hosts'][][] array where S_SESSION['hosts'][$x][0] is the hostname for each device.

 

Its this i use when setting the value attribute of the checkbox:

 

<input type='checkbox' name='chbox_delete_host_record[]' value='".$_SESSION['hosts'][$x][0]."'>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.