如何使用提交按钮

时间:2016-06-04 18:26:56

标签: javascript php jquery mysql ajax

当我点击更新功能时,如何更新mysql数据库。我想要更新的字段位于我创建的文本字段中。

<script>

     function yourfunction() 
     {
         document.getElementById("name").readOnly = true;
         document.getElementById("number").readOnly = true;
         document.getElementById("status").readOnly = true;
         document.getElementById("expertise").readOnly = true;
         document.getElementById("remark").readOnly = true;

     }

    window.onload = yourfunction;

    function myfunction()
    {
        document.getElementById("name").readOnly = false;
        document.getElementById("number").readOnly = false;
        document.getElementById("status").readOnly = false;
        document.getElementById("expertise").readOnly = false;
        document.getElementById("remark").readOnly = false;
        document.getElementById("edit").style.visibility='hidden';
    }

所以首先我可以点击编辑按钮,这样我的文本字段就可以编辑了。一旦我编辑了它们,我就可以点击更新按钮来更新mysql数据库中的那些字段。

    <?php
    $name = $_GET['name'];

    $connect = mysqli_connect("localhost", "root", "","test1");
    $output='';
    $sql = "SELECT Name, Number, Expertise, Status, Remarks FROM particulars WHERE Name ='". $name."'";

    $result = mysqli_query($connect, $sql);

    while($row= mysqli_fetch_array($result))
{
    $name = $row['Name'];
    $number =$row['Number'];
    $Expertise =$row['Expertise'];
    $status =$row['Status'];
    $remarks =$row['Remarks'];
} 

    echo "<label>Name</label> <input id = 'name' type='text' value='" .$name. "'/> <br>";
    echo "<label>Number</label><input id = 'number' type='text' value='" .$number. "'/><br>";
    echo "<label>Expertise</label><input id = 'expertise' type='text' value='" .$Expertise. "'/><br>";
    echo "<label>Status</label><input id = 'status' type='text' value='" .$status. "'/><br>";
    echo "<label>Remarks</label><input id = 'remark' type='text' value='" .$remarks. "'/><br>";
    ?>

<br><br><br>
<input type="button" id="edit" value = "Edit" onclick="myfunction()">
<input type="submit" id="update" value = "Update">

我不介意使用提交按钮或使用ajax来解决此问题。非常感谢!:)

2 个答案:

答案 0 :(得分:0)

首先关注你的while循环,它会遍历所有行。在你的情况下,它应该只是一个。这是有效的,因为只有一行,但你应该使用一个不应该使用while循环的方法。

回到你的任务。你需要一个表格(最简单的方法)

echo "<form action='mypage.php' method='POST'>"; // Start Form (Forms use names not ids btw)
echo "<label>Name</label> <input name = 'name' type='text' value='" .$name. "'/> <br>";
echo "<label>Number</label><input name = 'number' type='text' value='" .$number. "'/><br>";
echo "<label>Expertise</label><input name = 'expertise' type='text' value='" .$Expertise. "'/><br>";
echo "<label>Status</label><input name = 'status' type='text' value='" .$status. "'/><br>";
echo "<label>Remarks</label><input name = 'remark' type='text' value='" .$remarks. "'/><br>";
echo "<input type='submit' id='update' value = 'Update'>"; // This will send the data
echo "</form>"; // End form

mypage.php

$sql = "Update particulars  set Number = " .$_POST['number'] etc....

将Name作为主键非常无效。数字很​​容易处理,更不用说多个人可以有相同的名字了。

将Id作为主键并将其设置为auto_increment。

您可以像这样将

添加到表单中
<input name = 'id' type='hidden' value='" .$id. "'/>

答案 1 :(得分:-1)

试试这个:

UPDATE particulars SET Name = $name, Number = $number, Expertise = $expertise, Status = $status, Remarks = $remarks WHERE Name = $name;