I have the following code that if a user is selected, I want their values to appear in an input box that eventually I will change the values for.
if ($stmt = $con->prepare("SELECT * FROM team_rankings")) {
$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>Win
<input type="text" value="<?php echo $ranking_wins; ?>">
</label>
<label>Loss
<input type="text" value="<?php echo $ranking_losses; ?>">
</label>
How can I structure this so that only the user's win and losses will show up from what was selected in the select input.
答案 0 :(得分:2)
Here's a how-to, but you'll need to fill in some details.
First of all, you need to either add onChange
even to your drop down and then implement the functionality to auto-submit the form OR (simpler) ad a submit button. That way, your page will require user to click "Submit" after changing the value in drop down, but that should be OK.
When this is done, you need to add something like this in the beginning of your script:
$selectedUser = intval($_POST['selected_user_id']); // or $_GET
$wins = 0;
$losses = 0;
(I did some basic input validation here by converting to int - not sure if this would work).
Then, add something like this to your while
loop:
while ($row = $stmt->fetch()) {
echo "<option value = '{$ranking_user_id}'";
if ($ranking_user_id == $selectedUser) {
echo 'selected';
$wins = $ranking_wins;
$losses = $ranking_losses;
}
echo ">{$ranking_firstname}</option>";
}
That will allow your selected user to stay selected after you click submit.
Lastly, you need to put wins and losses:
<label>Win
<input type="text" value="<?php echo $wins; ?>">
</label>
<label>Loss
<input type="text" value="<?php echo $losses; ?>">
</label>
Note that I used my own wins and losses, and not those values that were bind in each while-pass.
Note 2: your form should have something like this:
<form action=<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?> method="post">
This way when you submit you'll just send the data to the same script.