1

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.

4
  • You don't have to use AJAX as what you're seeking is a database operation. How do you associate a case with a user? Is there a table having a case id paired with a user id? You will have to make a round trip: click button->update case/user table->return and reload display Commented Sep 11, 2018 at 14:07
  • You can use $_SESSION to get the id of the logged in user and add the id to the case so when you submit it would be sumitted for that logged in id Commented Sep 11, 2018 at 14:34
  • @JayBlanchard The case table has a user id foreign key assigned to each case. It is not null and the open cases have assigned a default user. My problem begins when I am going to write the php function to update the selected case (displayed by the while loop). How do I write 1 function to prcess any case? I can just display the 10 first, put the ids in an array and write 10 functions, but that doesn't seem like good practice... Commented Sep 12, 2018 at 6:46
  • @OkeTega I use the $_SESSION for displaying cases related to the "my cases" pages, so that's all good :) Commented Sep 12, 2018 at 6:48

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.