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

标签 javascript php mysql 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>

最佳答案

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

"select all data from html table using javascript"

会让我相信您想将实际的 html 保存到 mySQL 吗?

您的问题可能需要针对您的问题表进行特定编码,或者对于更通用的方法...这取决于您的具体需求。

一般来说,您可以遍历每个 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 });
});

关于javascript - 如何选择html表中的所有数据并使用ajax将其发送到我的数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23024849/

相关文章:

javascript - 带有 RX.Js 5.0.0 拉取 'Not Defined' 错误的 JSBIN 项目

php - 使用 'mysql_connect()' 和不使用它的性能差异

python - 高效地将数据结构添加到数据库中

mysql - 如何解决 mysql 线程堆栈溢出问题?

javascript - 选择框计数器

javascript - 没有 JS 的 CSS 默认选择

javascript - 发送 Angular POST 请求

php - 多维数组中的最小值和最大值

javascript - 如何将 javascript 变量从 .js 文件导入到 html 文档

php - 登录系统密码和用户名