猿问

如何将添加的数据从 HTML 表保存到 SQL (PHPMYADMIN)

所以这是我的情况;我正在尝试保存通过填写表格添加的表中的数据,我不知道如何使用 javascript 或 ajax 将其保存到 phpmyadmin 中的数据库中。一个示例代码非常感谢。谢谢!PS:我是一个初学者,我只是在网上搜索代码并尝试利用我得到的东西。请帮助:D


<html>

<body>

<div class = "inputfield">

    <label>First Name:</label>

    <input type="text" id="fname"><br>

    <label>Last Name:</label>

    <input type="text" id="lname"><br>

    <label>Gender:</label>

    <select name="gender" id="gender">

        <option>Male</option>

        <option>Female</option>

    </select><br>

    <label>HOURS:</label>

    <input type="number" min=".5" max="12" step=".5" name="hours" id="hours" placeholder="--.5 to 12--"> <br>

</div>

<div class = "tablefield">

    <table class="mtable"  id="mtable">

        <tr>

            <th width="27%">First Name</th>

            <th width="27%">Last Name</th>

            <th width="15%">Gender</th>

            <th width="15%">Hour</th>

            <th width="16%">Edit</th>

        </tr>

        

    </table>

    <table class="sumtable">

        <tr><b>

            <td class="sum">TOTAL HOURS</td>

            <td class="sum1" id="sumtd"></td>

        </b></tr>

    </table>

</div>

<div class="buttons">

        <button type = "button" onclick="add1('mtable')">ADD</button>

        <button type = "reset" name="reset" class="btnclr">CLEAR</button>

        <button type = "button" name="save" onclick="savefunc()">SAVE</button>

</div>

<input type = "hidden" name="hid1" id="hid1">

<input type = "hidden" name="hid2" id="hid2">

<input type = "hidden" name="hid3" id="hid3">

<input type = "hidden" name="hid4" id="hid4">


<script>

var sum = 0;


慕妹3146593
浏览 104回答 1
1回答

慕运维8079593

我已经弄清楚了。我只是将它包含在我的循环语句的底部并添加了一个 save.php 文件。将此用作参考https://www.studentstutorial.com/ajax/insert-data&nbsp; &nbsp;&nbsp;$.ajax({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; url: "save.php",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type: "POST",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fname: hid1,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lname: hid2,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gender: hid3,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hour: hid4&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cache: false,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; success: function(dataResult){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var dataResult = JSON.parse(dataResult);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(dataResult.statusCode==200){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert("Save Successful!");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if(dataResult.statusCode==201){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;alert("Error occured !");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });
随时随地看视频慕课网APP
我要回答