0

I would like to post data in checkboxes, as like

<?php foreach($data as $foreignid => $id): ?>
<input type="checkbox" 
       name="photoids[<?php echo $foreignid; ?>]" 
       value="<?php echo $id;?>"
                       />

If I go this way only one key-value pair remains in my $_POST array.

If I leave the photoids[] array empty, (not echoing out the $foreignid) all of the key-value pairs remains in $_POST array, but then i don't have access to the $foreignid variable which that I need in my code.

What is the best workaround for this problem?

2 Answers 2

1

Use this:

<?php foreach($data as $foreignid => $id): ?>
<input type="checkbox" 
       name="photoids_<?php echo $foreignid; ?>" 
       value="<?php echo $id;?>"
                   />

And then parse keys in $_POST array that begin with photoids to obtain foreignid.

Or use this:

<?php foreach($data as $foreignid => $id): ?>
<input type="checkbox" 
       name="photoids[]" 
       value="<?php echo $foreignid . '_' . $id;?>"
                   />

And then parse values to obtain foreignid and id.

Of course, assuming that underscore '_' will not show up in foreignid or id.

You can parse name or value using:

$src = '123_987';
$arr = explode('_', $src);

$arr[0] will contain 123 and $arr[1] will contain 987.

Sign up to request clarification or add additional context in comments.

Comments

0

Maybe something like this:

<?php foreach($data as $foreignid => $id): ?>
<input type="checkbox" 
name="photoids[<?php echo $foreignid; ?>]" 
value="<?php echo $foreignid."/".$id."/".$data;?>"
/>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.