简体   繁体   中英

Passing html table data to function

I've used this site a ton but this is the first time posting. Please be kind. I'm trying to pass data from an HTML table to a function using onclick. I'm able to pass the entire table using onclick="markPaid(transaction.innterText) , but I want to be able to pass the data from the current data row only. My best attempt is onclick="markPaid(thistransaction.innterText)" but results in an undefined variable when passed to the function "markPaid". Hopefully this is a clear explanation of the issue.

            <table id ="transaction" border="1">
            <style>th, td {padding: 10px;}</style>
            <thead>
                <tr>
                    <th><?php echo implode('</th><th>', array_keys(current($transactions))); ?></th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($transactions as $transaction): array_map('htmlentities', $transaction); ?>
                <tr>
                    <td id = "thistransaction"><?php echo implode('</td><td>', $transaction); ?></td>
                    <td><a class ="btn btn-default" href="invoice.php?ID=<?php echo $transaction['transid']; ?>" target = "_blank">Print Invoice</a> </td>
                    <?php if($transaction['paid'] == false): ?>
                        <td><button class = "btn btn-default" onclick="markPaid(thistransaction.innterText)">Mark as Paid</button></td>
                    <?php endif; ?>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>

I think you are getting undefined with onclick="markPaid(thistransaction.innterText)" because thistransaction is not unique. You are setting thistransaction as an id on every row of your table in your foreach loop. Try using something unique for each row, maybe the transid you echo to the invoice, $transaction[transid]

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