简体   繁体   中英

How to click on HTML table row and send row's data to PHP (as POST)?

I need to make a table with clickable rows (for this I found some Javascript solutions). When I click one row, a form will be filled with data from the clicked row.

But I don't know how to send the data from HTML table to PHP, when I click on that row. Data must be sent as POST, as I mentioned in title.

I have no idea how to start, so I cannot show you nothing. Using multiple forms inside one table is not allowed, <a> tag around <td> is not allowed either, so I'm stuck. From what I searched on web, the solution would be AJAX, but I'm not familiarized with it, never used AJAX, so if possible, a non-AJAX solution would be much appreciated. If not, please show me a functional example.

EDIT solved

In the end I did it without any AJAX, thanks to ideas from emartel and Guerra . I made a single form with a hidden input field and I submitted it programatically, after I collected the required data from the row I clicked on.

I echoed the tr tags like this: <tr data-id=$id_from_db onclick="submit_id(this)"> .

function submit_id(tableRow) {
        //get ID
        var myID = tableRow.dataset.id;

        //set ID
        var setID       = document.getElementById('order_id');
            setID.value = myID;

        //submit
        var form = document.getElementById('myform');
        form.submit();
    };

PS I tried to embed some PHP code, but I failed, it didn't shown on page.

After filling your form ( name="myform" and method="post" ) from JavaScript, simply call:

document.myform.submit();

This should do the work

You can put some kind of input hidden on each row with an ID. So you can use just 1 form passing the ID to submit post and recovery the entire register on server.

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