Jump to content

PHP checkbox array


AE117

Recommended Posts

I have this.

 

<input name="delete[]" type="checkbox" id="delete" value="1" />

 

What I want to is get the value from this which is the "1"

AND

Get the id which would be "delete"

 

I can get the value but i cant seem to get the id

 

This is what I have been using

foreach($_REQUEST['delete'] as $index=>$val){
echo $index;
echo $val;
}

 

echo $index just gives me the num of array I am on and the $val gives the actually value. Can anyone help me out with getting the id or how I can specify a id.

 

THanks

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/222952-php-checkbox-array/
Share on other sites

Couple of ideas:

Use AJAX to submit the form to include both of them in a get statement

 

Or:

As the value put something like value="1&delete"

 

Then you can explode it to form an array

 

Eg:

 

<input name="delete[]" type="checkbox" value="1&delete" /> 
$array = explode("&", $val);
echo $array[0]; //Value
echo array[1]; //ID

 

Hope you get the jist

Link to comment
https://forums.phpfreaks.com/topic/222952-php-checkbox-array/#findComment-1152793
Share on other sites

name each checkbox with the same name, set the value = to the id. then just delete the id's that are checked.

 

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
print_r($_POST['delete']);
}
?>
<html>
<body>
<form method='post' action=''>
<input type='checkbox' name='delete[]' value='22'>
<input type='checkbox' name='delete[]' value='44'>
<input type='checkbox' name='delete[]' value='777'>
<input type='checkbox' name='delete[]' value='your mom'>
<input type='submit'>
</form>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/222952-php-checkbox-array/#findComment-1152801
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.