繁体   English   中英

如何在html表中选择所有数据并使用ajax将其发送到我的数据库

[英]how to select all data at html table and send it to my database with ajax

我需要帮助,我想使用javascript从html表中选择所有数据,并使用php使用ajax将其发送到我的数据库中,我使用具有使html表中存储数据的形式的代码,然后从表中获取数据以进行发送他们到我的数据库

<html>
<head>
<title>Order</title>
<script type="text/javascript">


function updateForm() {

var tablename = document.getElementById("tablen").value;
 document.getElementById("demo").innerHTML=tablename;


    var fieldname = document.getElementById("fieldn").value;

    var fieldtype = document.getElementById("fieldt").value;


    var table=document.getElementById("results");
    var row=table.insertRow(-1);

    var cell1=row.insertCell(0);
    var cell2=row.insertCell(1);


    cell1.innerHTML=fieldname;
    cell2.innerHTML=fieldtype;        


   }
</script>
</head>
<body>
<form name="order" method="post" id="frm1">
<table>
    <tr>
        <td>
            <label for="tablename">Table Name </label>
        </td>
        <td>
            <input id="tablen" name="tablename" title="Please enter only alphabetic characters" type="text" size="28" />
        </td>
        <td></td>
    </tr>
    <tr>
        <td>
            <label for="fieldname">Field Name</label>
        </td>
        <td>
            <input id="fieldn" name="fieldname" title="Enter item quantity" width="196px" />
        </td>
    </tr>
    <tr>
        <td>
           <label for="fieldtype">Field Type</label>

        </td>
        <td>
            <select name="fieldtype" id="fieldt" required="required">

            <option value="">Select</option>
            <option value="int(11) NOT NULL AUTO_INCREMENT">int(11)</option>
            <option value="decimal NOT NULL">decimal</option>
            <option value="varchar(50) NOT NULL">varchar(50)</option>
            <option value="text NOT NULL">text</option>
            <option value="char NOT NULL">char</option>
            <option value="longtext NOT NULL">longtext</option>
            <option value="year NOT NULL">year</option>
            <option value="date NOT NULL">date</option>
            <option value="time NOT NULL">time</option>
            <option value="binary NOT NULL">binary</option>
            <option value="float NOT NULL">float</option>
   </select>
        </td>

    </tr>
</table>
<input type="reset" name="reset" id="resetbtn" class="resetbtn" value="Reset" />
<button type="button" onClick="updateForm();"/>Add To Table</button>
</form>
<br>



 <form  method="post">

<table id="results" border="2" width="360">
<thead>
<tr>
    <th scope="col" width="120">Table Name</th>
     <th scope="col" width="120">
<p id="demo"></p>
</th>
</tr>
<tr>

    <th scope="col" width="120" id="fieldname1">Field Name</th>
    <th scope="col" width="120" id="fieldtype1">Field Type</th>

</tr>
</thead>
</table>
<button type="button" id="btn"/>Add To DB</button>
</form>
</body>
</html>

恐怕您需要提供更多详细信息才能做出准确的答复,显然您在问什么

“使用javascript从html表中选择所有数据”

是否会让我相信您要将实际的html保存到mySQL?

您的问题可能需要针对所涉及表的特定编码,或者采用更通用的方法……这取决于您的确切需求。

一般而言,您可以遍历每个TR,并对每个TR进行ajax请求...

$("#yourtableselector tr").each(function() {
var postData = {};
//use $(this).find or similar to get the data you need for each td and add it to the postData variable
$.ajax({ post the data here to a php file of your chosing, that adds the "data" in the database });
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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