0

I'm using the bootstrap accordion. Each row can be expanded to show a 'details section' which includes some additional information and a button which should delete that row from the database. The row id and name gets assigned using PHP and this seems to be causing my issue. Using a hidden field, I can echo a name but it is always the name of the last row in the accordion.

By using PHP, how can I detect which row is expanded and get the name of that div?

PHP/HTML

<?php
if (isset($_POST['btnDelete'])) {
    $idnbr = $_POST['hid'];
    echo $idnbr;            // No matter which row is selected, always echoes last row's name
}
?>

<div id="div1">
    <?php
    // SQL and SQL connection stuff
    while ($row = odbc_fetch_array($data)) {
    ?>
        <div class="accordion-heading">
            <a class="accordion-toggle" data-toggle="collapse" data-parent="#div1" 
                href="#<?php echo $row['idnbr']; ?>">Delete</a>
        </div>
        <div id="<?php echo $row['idnbr']; ?>" name="<?php echo $row['idnbr']; ?>" 
            class="accordion-body collapse">
            <input type="hidden" id="hid" name="hid" 
                value="<?php echo $row['idnbr']; ?>">
        </div>
        <!-- Accordion body -->
        <div>
            <input class="btn" type="submit" name="btnDelete" 
                id="btnDelete" value="Delete">
        </div>
    <?php
    }
    ?>
</div>


JS

// Hides/shows correct row
$('.accordion-toggle').click(function() {
    var $acc = $('#div1');
    $acc.on('show', '.collapse', function() {
        $acc.find('.collapse.in').collapse('hide');
    });
});

$('input#btnDelete').click(function() {
    // Get the id of the selected row
    var $selrow = $('#div1').find('.collapse.in');
    var $attrid = $selrow.attr('name');
    if (!confirm("Are you sure you want to Delete?")) {
        event.preventDefault();
        return;
    } else {
        $('#hid').val($attrid);
        alert($attrid');            // Able to alert the correct row
    }
});


In my jQuery, after I set the hidden field's value, if I prevent the page from posting and check the value in the console, it is correct. When I let the page post and check the HFs value in the console, it is always the first row's name, no matter which row I had clicked Delete. So, the last row's name gets echoed and the first row's name gets set to the hidden field.

DB

Desc    |    idnbr
-------------------
Desc 1  |    10
Desc 2  |    20
Desc 3  |    30


So using the example DB above, you would see 3 accordion rows on the page (Desc 1, Desc 2, Desc 3). If I expanded the second row (Desc 2) and clicked Delete this is what I get right now:

Alert:           20
Hidden Field:    10
Echo:            30


What do I need to do in order to echo the correct value or am I going about this completely wrong? (in this case 20)

4

1 回答 1

1

使您的输入提交的名称属性唯一,例如
<input class="btn" type="submit" name="btnDelete<?php echo $row['idnbr']; ?>" id="btnDelete" value="Delete">

接着:

  if(!empty($_POST))
  {
     $idnr = str_replace('btnDelete','',array_search('Delete', $_POST));
  }
于 2013-06-03T18:16:01.313 回答