0
votes

I have a select option in foreach loop. In every loop I'm creating a div and inside div a select option. But problem is when I send select's value with post method I can't get option's correct posted value. For foreach first 2,3 item button is working and submitting form but when I try to submit other items' form button is not working I can't create unique name for select because I must assign post variable to a variable in action page.

I think problem is about unique select name="", I can't give an unique name to select because I need it's name in action page as you know. How can I solve this stupid thing.?

<?php
foreach($a as keywords_arr){
?>

<form name="<?= $keywords_arr["keyword_id"]?>" method="post" action="actions/myaction.php">
    <div class="modal-body">
        <label for="tagler">Bla bla</label>

        <select  name="tagler" style="width: 100%;" class="form-control">
            <?php
            $all_tags = $db->query("SELECT *****");
            $all_tags->setFetchMode(PDO::FETCH_ASSOC);
            $all_tags_arr = $all_tags->fetchAll();

            foreach ($all_tags_arr as $tag) {?>
                <option value="<?= $tag["tag_id"] ?>"><?= $tag["tag_name"] ?></option>
            <?php } ?>
        </select>

        <input type="hidden" name="kw_id_label" value="<?= $keywords_arr["keyword_id"] ?>"/>
        <br><br>
        <span id="result_lbl_"></span>
    </div>
    <div class="modal-footer">
        <button id ="btn_<?= $keywords_arr["keyword_id"] ?>" type="submit">Save</button>
    </div>
</form>

<?php}?>

myaction.php page

<?php 

$tag = $_POST["tagler"];
$keyword_id = $_POST["kw_id_label"];


?>

Fiddle example:See example

3

3 Answers

0
votes

You should declare select as array like tagler[]

<select name="tagler[]" style="width: 100%;" class="form-control">

Now in your php file use print_r($_POST), you will get selected options in array.

0
votes

You can create dynamic select names in loop but in addition to that create hidden field as array which will save names of all select value generated. so in POST you will read hidden value to identify select boxes.

<?php
foreach($a as keywords_arr){
?>

<form name="<?= $keywords_arr["keyword_id"]?>" method="post" action="actions/myaction.php">
  <select  name="tagler<?php echo $keywords_arr["keyword_id"]?>" style="width: 100%;" class="form-control">
  ---
  ---
  <input type = "hidden" name="select_boxes_name[]" value="<?php echo tagler<?php echo $keywords_arr["keyword_id"]?>">

after form post read select_boxes_name[] array to identify select boxes posted.

0
votes

The unique id for select name="" is not your problem. The only problem I found is that you have the last php tag too close to the closing bracket and the intepreter doesn't recognize it. Please, try to create space between the closing bracket.

Change this:

<?php}?>

to this:

<?php } ?>

(UPDATE) Use the below code for myaction.php:

<?php
$post_name = "uniquename";
$post_len = strlen($post_name);

foreach ($_POST as $key => $value)
{
    //if the name of the current post begins with "uniquename"
    if (substr($key, 0, $post_len) == "uniquename") 
    {
      $index = $post_len;

      $post_id = (int)substr($key, $index);

      echo "post_id = " . $post_id . "<br/>key = " . $key;

      // do your stuff here...


      break;
    }
}
?>

Here, we filter the POST variables and when we find the right one, we extract the ID from it (the ID in our case is the numeric part after the "uniquename". e.g. From uniquename5) the ID (post_id) will be 5.