获取从下拉菜单中选择的每个用户要更改的输出数据


Getting outputted data to change per user selected from drop-down

我正处于最困难的时刻,我确实需要弄清楚,因为我计划在未来经常使用它。

我有一个下拉框,在那个选项框中,我有我想要显示的所有用户。然后在下面有输入框,输出用户的输赢次数。

到目前为止,我想要在选项框中显示的所有用户的输出和输赢输出,但是如果我尝试选择一个新用户来查看他们的数据,输出不会改变。我不知道是否把它放在这个类别或Javascript,因为我确信我将需要AJAX来做到这一点,这样我就不需要重新加载页面,但我应该从这里去哪里使这个工作?

try {
//Prepare
 $con = mysqli_connect("localhost", "", "", "");
if ($stmt = $con->prepare("SELECT * FROM team_rankings WHERE user_id=user_id")) {
    $stmt->execute();
    $stmt->bind_result($ranking_id, $ranking_user_id, $ranking_firstname, $ranking_username, $ranking_division, $ranking_wins, $ranking_losses); 
    //var_dump($stmt);
    if (!$stmt) {
        throw new Exception($con->error);
    }
$stmt->store_result();
    echo "<select name = 'member'>";
    while ($row = $stmt->fetch()) {
     echo "<option value = '{$ranking_user_id}'";
        echo ">{$ranking_firstname}</option>";
    }
    echo "</select>";

    }   else {
        echo "<p>There are not any team players yet.</p>";
        }
}
catch (Exception $e)
{
    echo "Error: " . $e->getMessage();
}
?><br><br>
    <label>Wins
        <input type="text" value="<?php echo $ranking_wins; ?>">
    </label>
    <label>Loss
        <input type="text" value="<?php echo $ranking_losses; ?>">
    </label>

你的php文件应该是这样的:你需要制作一个新的文件,然后你用来加载第一页。如果其他因素影响了这段代码,请注释。

<?php
if(isset($_POST['userID'])){
    try {
    //Prepare
     $con = mysqli_connect("localhost", "", "", "");
    if ($stmt = $con->prepare("SELECT * FROM team_rankings WHERE user_id=".$_POST['userID'])) {
        $stmt->execute();
        $stmt->bind_result($ranking_id, $ranking_user_id, $ranking_firstname, $ranking_username, $ranking_division, $ranking_wins, $ranking_losses); 

        if (!$stmt) {
            throw new Exception($con->error);
        }
    $stmt->store_result();
    $result = array();
        while ($row = $stmt->fetch()) {
        $result["win"] = $ranking_wins;
        $result["losses"] = $ranking_losses;         
        }

        }   
    }
    catch (Exception $e)
    {
        echo "Error: " . $e->getMessage();
    }
echo json_encode($result);
}
?>

Html部分,包括jquery代码。在这里,您可以触发select的更改函数,它将发送userID到php文件并接收JSON数据。然后你可以把这些值赋给你的输入。您需要包含jquery库来处理此操作。

<script>
$(document).ready(function(){
    $("select[name='member']").on("change",function(){
        $.post("PHP file Path",{userID:$("select[name='member']").val()},function(data){
            var result = JSON.parse(data);
            $("#win").val(result.win);
            $("#losse").val(result.losse);
            });
        }); 
});
</script>
<label>Wins
<input type="text" value="<?php echo $ranking_wins; ?>" id="win">
</label>
<label>Loss
<input type="text" value="<?php echo $ranking_losses; ?>" id="losse">
</label>