doushi1960 2014-01-25 22:01
浏览 30

html表行获取单元格值[关闭]

Can anyone help me here about getting the data inside the table cell to be inserted to the MySQL Database?

my code to display tables in html is this. i want now to get the values inside a row by clicking the 'Insert' and submit to submituser.php. help pls.

<?php
$sql=mysql_query("select * from tbluser");
while($row=mysql_fetch_array($sql)){
echo'<td>'.$row['name'].'</td>
<td>'.$row['address'].'</td>
<td>'.$row['$contact'].'</td>
<td><a href="submituser.php">Insert</a></td>
';
}

?>

Output is like this:

| Name | Address | Contact | Action|
|myname|anywhere | 090231  | Insert|

Insert code in html

<td><a href="submitdata.php">Insert</a></td>

submitdata.php Must have this code

$query = insert into tbluser(name, address, contact) values('cellvalue1','cellvalue2','cellvalue3');

Does anyone know how to?

  • 写回答

1条回答 默认 最新

  • dtnqbre7980007 2014-01-25 22:18
    关注

    Solution using JQuery:

    Change table cells to:

    <td class="nameCell">myname</td>
    <td class="addressCell">anywhere</td>
    <td class="contactCell">090231</td>
    <td><a href="#" class="insertButton">Insert</a></td>
    

    Attach event handler to handle click event in javascript/jquery:

    $(".insertButton").click(function () {
        var name = $(this).parent().siblings(".nameCell").text();
        var address = $(this).parent().siblings(".addressCell").text();
        var contact = $(this).parent().siblings(".contactCell").text();
    
        //now call your api and pass the data
        $.post("submitdata.php", {
            name: name,
            address: address,
            contact: contact
        });
    
    });
    
    评论

报告相同问题?