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
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
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
Share on other sites

Figured it out

 

HTML:

<textarea name="description[<?php echo "2"; ?>]" id="description[]" cols="" rows="" style="width:300px;">Content Here</textarea>

 

PHP:

foreach ($_REQUEST['description'] as $key=>$checkbox)
{
echo $key;
echo "<br />";
echo $checkbox;	
}

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.