Reputation: 66
I have a table with info on different cases. I have set up a page that displays "open" cases that's unsolved. The open cases have a default user assigned, which means it's not dedicated to anyone. It also has a status value which determines if the case is solved. In the open cases page, only the cases with default user and case status "unsolved" are displayed. Another page displays "my cases". This page displays unsolved cases connected to the logged in user. For displaying the open cases I do this:
<?php
if ($result = mysqli_query($link, $sqlasget)) {
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_array($result)) {
echo "<div class='divelementlasse' id='" . $row['case_id'] . "'>";
echo "<h3 class='divphh3'>" . $row['kunde_navn'] . "</h3>";
echo "<h5 class='divphh5'>" . $row['case_adresse'] . "</h5>";
echo "<p>" . $row['case_kommentar'] . "</p>";
echo "<form action='" . htmlspecialchars($_SERVER['PHP_SELF']) . "' method='post'><input type='submit' class='tasak' name='" . $row['case_id'] . "' value='Ta sak" . $row['case_id'] . "'></input>";
echo "</div>";
}
mysqli_free_result($result);
} else {
echo "<div class='divelementlasse'>";
echo "<h3 class='divphh3'>Ingen elementer å vise.</h3>";
echo "</div>";
}
} else {
echo "ERROR: Could not be able to execute $sql. " . mysqli_error($link);
}
?>
This works for displaying the cases. The problem I have is the buttons. When a user hits "ta sak" I want to change the user of that specific case, to the logged in user. So basically: When a user hits the submit button for one of the cases displayed, that case is "transferred" to "my cases". I do not want to use Ajax, but if that's the ONLY solution so be it.
Upvotes: 1
Views: 48
Reputation: 66
So I fixed it myself. I changed the
<input type='submit' class='tasak' name='" . $row['case_id'] . "' value='Ta sak" . $row['case_id'] . "'></input>
to
<button type='submit' class='tasak' name='submit' value='".$row['case_id']."'>Ta sak</button>
The post handler is like so:
if($_SERVER["REQUEST_METHOD"] == "POST"){
if(isset($_POST['submit'])){
mysqli_query($link, "UPDATE case SET user_id='".$userid."' WHERE case_id='".$_POST['submit']."'")
}else{
$error = "ERROR: Could not execute $sql. " . mysqli_error($link);
}
}
}
When I changed the name on all the submit inputs created by the while loop to the same thing, I could use one function. I then tried to set the value of the inputs to the case_id, so that $_POST['submit']
returned that value. But then the text on the submit buttons changed to that value, so I used button instead and voila.
Upvotes: 1