简体   繁体   中英

Passing ID in codeigniter form

How can I pass a row ID in codeigniter form? I have this Code

if(isset($records)) : foreach($records as $row) : ?>
    <?php echo form_open('site/update/'); ?>
    <td><?php echo form_input('solution_time', $row->solution_time); ?></td>
    <td><textarea><?php echo $row->note; ?></textarea></td>
    <td><?php echo form_submit('update', 'Update'); ?></td>
    <?php form_close(); ?>
    </tr>
    <?php endforeach; ?>
    <?php else : ?>
    <h2>No Records</h2>
    <?php endif; ?>

But it returns 1 for every row's ID like that every time:

localhost/it/index.php/site/update/1

Update: I have added a new line (form_hidden) and it's not working too

if(isset($records)) : foreach($records as $row) : ?>
        <?php echo form_open('site/update/'); ?>
    <?php echo form_hidden('id', $row->id); ?>
        <td><?php echo form_input('solution_time', $row->solution_time); ?></td>
        <td><textarea><?php echo $row->note; ?></textarea></td>
        <td><?php echo form_submit('update', 'Update'); ?></td>
        <?php form_close(); ?>
        </tr>
        <?php endforeach; ?>
        <?php else : ?>
        <h2>No Records</h2>
        <?php endif; ?>

Try passing the row id to the form open. What you're doing is creating a bunch of forms with the same action and the submit is only passing the first one back to the controller. Passing the ID in the form action should fix that.

 <?php echo form_open('site/update/'.$row->id); ?>

The other option is to use jQuery to do the updates.

        $(document).on("click", ".updateLink", function() {
    $row = $(this).closest('tr');

    $.ajax({
        type: "GET",
        url: "<?=base_url()?>site/update/",
        data: $row.find('input').serialize(),
  dataType: "json",
        success: function(content) {
            if (content.status == "success") {
                $row.stop().css("background-color", "#99FF66").animate({backgroundColor: "#FFF"}, 1500);
            } else {

            $("#error").html('<p>'+content.message+'</p>');
            }
        }
    });
    return false;
});

In your form change the submit buttons to links:

<td><?php echo <a href="#" class="updateLink">Update</a></td>

Then in your controller you just have to echo back a success message on success.

if(update successfull)
{
    print json_encode(array("status"=>"success","message"=>""));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM