javascript - 使用 onClick 更新数据库(NewB)

标签 javascript php jquery mysql ajax

感谢强大的 Johan 的帮助。但我无法让我的数据库更新。如果你有时间请查看我的两个文件并告诉我 F 有什么问题 我的 hire_staff.php

<?php
session_start();
include("header.php");
?>
<?php
$chief_aerodynamicist = $staff['chief_aerodynamicist'];
$chief_designer = $staff['chief_designer'];
$commercial_director = $staff['commercial_director'];
$pit_crew = $staff['pit_crew'];
$technical_director = $staff['technical_director'];




?>
<head>
<script>
function bye(){
    alert('bye');
}
$(document).ready(function(){
    function hire(){
        $.ajax({
            url: "update.php",
            type: "POST",
            data: {uid: 12341234}, //this sends the user-id to php as a post variable, in php it can be accessed as $_POST['uid']
            success: function(data){
                data = JSON.parse(data);
                //update some fields with the updated data
                //you can access the data like 'data["driver"]'
            }
        });
    }
});
</script>
</head>

<center><h2>You can hire new staff here</h2></center>

<table cellpadding="3" cellspacing="5">
        <tr>
            <td>Chief Aerodynamicist:</td>
            <td><i><?php echo "Level $chief_aerodynamicist"; ?></i></td>
            <td><form><input type="button" value="Hire!" onClick="hire();"</form></td>
        </tr>
          <tr>   
            <td>Chief Designer:</td>
            <td><i><?php echo "Level $chief_designer"; ?></i></td>
        </tr>
          <tr>               
            <td>Commercial Director:</td>
            <td><i><?php echo "Level $commercial_director"; ?></i></td>
        </tr>
          <tr>  
            <td>Pit Crew:</td>
            <td><i><?php echo "Level $pit_crew"; ?></i></td>
        </tr>
        <tr>  
            <td>Technical Director:</td>
            <td><i><?php echo "Level $technical_director"; ?></i></td>
        </tr>


</table>



<?php
include("footer.php");
?>

我的test1.php文件

<?php
include("functions.php");
connect();
if(isset($_POST['uid'])){
     connect();
    mysql_query("UPDATE  `staff` SET  `driver` =  '3' WHERE `id`='".$_SESSION['uid']."'") or die(mysql_error());
$query = mysql_query("SELECT FROM `staff` WHERE `id`='".$_POST['uid']."'")or die(mysql_error());
    $results = mysql_fetch_assoc($query);
    echo json_encode($results);

}

?>

它实际上是我要更新的 中的人员,但司机也在同一张表中

最佳答案

使用jQueryAjax :

$(document).ready(function(){
    $("#button").click(function(){
        $.ajax({
            url: "update.php",
            type: "POST",
            data: {uid: 12341234}, //this sends the user-id to php as a post variable, in php it can be accessed as $_POST['uid']
            success: function(data){
                data = JSON.parse(data);
                //update some fields with the updated data
                //you can access the data like 'data["driver"]'
            }
        });
    });
});

//the button
<input type="button" id="button" value="Hire!"/>

//update.php
if(isset($_POST['uid'])){
    //connect to the db etc...
    mysql_query("UPDATE  `staff` SET  `driver` =  '3' WHERE `id`='".$_POST['uid']."'") or die(mysql_error());

    //this'll send the new statistics to the jquery code
    $query = mysql_query("SELECT FROM `staff` WHERE `id`='".$_POST['uid']."'")or die(mysql_error());
    $results = mysql_fetch_assoc($query);
    echo json_encode($results);
}

在ajax调用的success函数中使用“update some fields”,我的意思是你可以在数据库中更新你刚刚更改的页面的统计信息,这样统计网页在数据库和页面本身中自动更改

编辑
把它放在标题中:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
    function bye(){
        alert('bye');
    }
    $(document).ready(function(){
        $("#button").click(function(){
            $.ajax({
                url: "test1.php",
                type: "POST",
                data: {uid: 12341234}, //this sends the user-id to php as a post variable, in php it can be accessed as $_POST['uid']
                success: function(data){
                    data = JSON.parse(data);
                    //update some fields with the updated data
                    //you can access the data like 'data["driver"]'
                }
            });
        });
    });
</script>

然后像这样给你的按钮 id 'button':

<form><input type="button" id="button" value="Hire!"/></form>

确保 ajax 请求的 url 参数 (url: "test1.php",) 指向正确的 php 页面。

希望对您有所帮助!

编辑
如果你想选择一个驱动程序,这是代码:

<form><input type="button" class="button" id="3" value="Hire!"/></form> //the id is different for every guy

JavaScript 代码:

$(document).ready(function(){
    $(".button").click(function(e){
        $.ajax({
            url: "test1.php",
            type: "POST",
            data: {colID: e.target.id}, //this sends the user-id to php as a post variable, in php it can be accessed as $_POST['uid']
            success: function(data){
                data = JSON.parse(data);
                //update some fields with the updated data
                //you can access the data like 'data["driver"]'
            }
        });
    });
});

如您所见,我将 # 更改为 .,我将参数 e 添加到回调函数中,并添加了colID 到数据对象。
这将是新的 PHP 代码:

if(isset($_POST['colID'])){
    //connect to the db etc...

    $colID = mysql_real_escape($_POST['colID']); //for security
    $userID = mysql_real_escape($_SESSION['uid']);
    mysql_query("UPDATE  `staff` SET  `$colID` =  'newValue' WHERE `id`='$userID'") or die(mysql_error());

    //this'll send the new statistics to the jquery code
    $query = mysql_query("SELECT FROM `staff` WHERE `id`='$userID'")or die(mysql_error());
    $results = mysql_fetch_assoc($query);
    echo json_encode($results);
}

我希望这就是你的意思:)

关于javascript - 使用 onClick 更新数据库(NewB),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24753319/

相关文章:

php - WordPress:update_post_meta 不工作

c# - 图像源标记中的空白导致 MVC 4 View 中的 URL 不正确

javascript - 在 href click 上发送 ajax 帖子

javascript - WinJS DOM 加载

javascript - 画一个漂亮整齐的弧线,没有大的直 Angular ,而是有漂亮的 Angular

javascript - MeteorJS 和 x-editable-reactive-template

javascript - 在jsp中将值从servlet传递到javascript

php - 同时更新两个表

php - 如何在html中隐藏不需要的空白

javascript - AngularJS - 动态添加表行中的所有值