2

Before i place this question i searched other similar problems like mine with tables and forms but i didn't manage to find solution. As you can see from my code i don't know why but this is not working.. I have a form and inside there is one form method='post'.. when i click the button Descending or Ascending nothing is happening and i don't know why ? any suggestions?

<form method="post">
<table class="table table-bordered table-striped">
    <thead>
    <tr>

        <th scope='col'>Id</th>
        <th scope='col'>Name
                            <button class="btn btn-info btn-sm dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                Sort by
            </button>
            <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
                <button name="nameDescending" type="submit" class="dropdown-item btn btn-info">Descending</button>
                <button name="nameAscending" type="submit" class="dropdown-item btn btn-info">Ascending</button>
            </div>
        </th><!-- ORDER BY "name" -->

        <th scope='col'>Last</th><!-- ORDER BY "lastname" -->
        <th scope='col'>Email</th><!-- ORDER BY "email" -->
        <th scope='col'>Telephone</th><!-- ORDER BY "telephone" -->
    </tr>


    </thead>
    <tbody>


    <?php
   

    if(isset($_POST['nameDescending'])){
        $sql = "SELECT * FROM cust_information ORDER BY name DESC";
    }

    if(isset($_POST['nameAscending'])){
        $sql .= "SELECT * FROM cust_information ORDER BY name ASC";
    }

    $result = mysqli_query($conn,$sql);
    $rowCount = mysqli_num_rows($result);

    if($rowCount > 0){
        while($row = mysqli_fetch_assoc($result)){
            echo "<tr>
                          <td scope='row'>{$row['auto_id']}</td>    <!--difference between class='row' / scope='row'-->
                          <td>{$row['name']}</td>
                          <td>{$row['lastname']}</td>
                          <td>{$row['email']}</td>
                          <td>{$row['telephone']}</td>
                  </tr> ";
        }
    } else{
        echo "<tr> <td colspan='5'>Nothing was found!</td> </tr> ";
    }
    ?>
    </tbody>
</table>

</form>
ThunderBoy
  • 391
  • 1
  • 3
  • 18
  • Does this answer your question? [Is it valid to have a html form inside another html form?](https://stackoverflow.com/questions/555928/is-it-valid-to-have-a-html-form-inside-another-html-form) – imposterSyndrome Nov 04 '20 at 12:35
  • Work fine for me, the page reload. Careful you made another form tag inside your form – Esteban MANSART Nov 04 '20 at 12:37
  • There's a `
    ` inside the `
    `
    – brombeer Nov 04 '20 at 12:40
  • u are right i delete the second `form` but still not working – ThunderBoy Nov 04 '20 at 12:41
  • not working like, prints out `Nothing was found` or not working like, `explodes my web server` – imposterSyndrome Nov 04 '20 at 12:43
  • have you checked your post? If you var_dump the post do you have both `$_POST['nameDescending']` and `$_POST['nameAscending']` set? – imposterSyndrome Nov 04 '20 at 12:45
  • @jameson2012 it returns the table with all the names...etc . Like i never clicked the button – ThunderBoy Nov 04 '20 at 12:48
  • 2
    presumably then @ThunderBoy that means that one or other of your queries is working, otherwise you wouldn't have a row count of ` > 0`. I have a feeling that your issue lies in the fact you check if, and then if again - rather than if/else. Have you done any debugging? What post values are being submitted, which query are you generating, what is the sql string when you are ready to execute the query........ – imposterSyndrome Nov 04 '20 at 12:56
  • @jameson2012 i think you are right with the if / else – ThunderBoy Nov 04 '20 at 13:02
  • "Holy Debug Batman, I think that's it!" :-) – imposterSyndrome Nov 04 '20 at 13:08
  • Neither of your buttons have a value. Not sure if that's a problem, but it's clearer to have some value submitted. I don't see how the 2 `if` statements could be causing a problem since you never have both POST variables sent at the same time. – miken32 Dec 23 '21 at 19:16

2 Answers2

0

I can't see anything wrong with your code that would prevent a form submission. But, I can see lots wrong with your coding style. It's a quiet day at work, so...

The biggest sin here is mixing PHP and HTML as much as you have. Always put as much code as you can at the top of file, separate from your HTML. (In a proper codebase these would be separate files, but I assume this is a learning exercise for you and not a professional project.) For database-driven pages like this, that means doing the query and gathering data into an array right away, so the only thing that comes into your HTML is some simple control structures and echos.

Using alternative syntax for control structures allows single line PHP intrusion into the HTML, no need for braces and certainly no echoing giant blocks of HTML where you have to worry about getting quotes right. Short echo tags make things cleaner as well. (For those rare instance where you absolutely have to echo a bunch of HTML, use a heredoc.)

According to convention, and RFCs, a POST request should implement some change on the server, such as storing or updating a record. Since that isn't the case here, you should really be using GET requests. As a bonus, this gets rid of your need for a form; the <button> elements are replaced with simple links. Looks like you're using Bootstrap so the styling makes them look identical.

Put that together and this is your code:

<?php
$sql = "SELECT * FROM cust_information ORDER BY name";

// default to empty string
$sort = $_GET["sort"] ?? "";

if($sort === "d") {
     $sql .= " DESC";
}

$result = $conn->query($sql);
// store the whole result into an associative array
$data = $result->fetch_all(MYSQLI_ASSOC);
?>

<table class="table table-bordered table-striped">
    <thead>
    <tr>

        <th scope='col'>Id</th>
        <th scope='col'>Name
            <button class="btn btn-info btn-sm dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                Sort by
            </button>
            <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
                <a href="?sort=d" class="dropdown-item btn btn-info">Descending</button>
                <a href="?sort=a" class="dropdown-item btn btn-info">Ascending</button>
            </div>
        </th><!-- ORDER BY "name" -->

        <th scope='col'>Last</th><!-- ORDER BY "lastname" -->
        <th scope='col'>Email</th><!-- ORDER BY "email" -->
        <th scope='col'>Telephone</th><!-- ORDER BY "telephone" -->
    </tr>


    </thead>
    <tbody>


    <?php if (count($data) === 0): ?>
        <tr> <td colspan='5'>Nothing was found!</td> </tr>
    <?php else: ?>
        <?php foreach ($data as $row): ?>
        <tr>
            <td scope='row'><?= $row['auto_id'] ?></td>    <!--difference between class='row' / scope='row'-->
            <td><?= $row['name'] ?></td>
            <td><?= $row['lastname'] ?></td>
            <td><?= $row['email'] ?></td>
            <td><?= $row['telephone'] ?></td>
        </tr>
        <?php endforeach ?>
    <?php endif ?>
    </tbody>
</table>
miken32
  • 42,008
  • 16
  • 111
  • 154
-1

I usually don't use form inside a table beacuse it will not work. If i want to redirect me somewhere I use the a tag.

Example:

    @foreach($data as $card)
        <tr style="text-align: center;">
            <td>{{ $card -> id }}</td>
            <td>{{ $card -> cardType }}</td>
            <td>{{ $card -> cardValue }} RON</td>
            <td>{{ $card -> createdBy }}</td>
            <td>{{ $card -> created_at }}</td>
            <td>
                <form action="to somewhere">
                    <button type="submit">Submit</button>                       
                </form>
            </td>
    @endforeach

Instead of this, use this :

   @foreach($data as $card)
        <tr style="text-align: center;">
            <td>{{ $card -> id }}</td>
            <td>{{ $card -> cardType }}</td>
            <td>{{ $card -> cardValue }} RON</td>
            <td>{{ $card -> createdBy }}</td>
            <td>{{ $card -> created_at }}</td>
            <td>
                <a class="icon pencil-lg-icon" href="{{ route ( 'admin.catalog.giftcards.edit', $card->id ) }}" style="color: white; padding: 8px 10px;"></a>
                <a class="icon trash-icon" href="{{ route ( 'admin.catalog.giftcards.delete', $card->id ) }}" style="color: white; padding: 8px 10px;"></a>
            </td>
    @endforeach

Or you can try this

 @foreach($data as $card)

    // Outside the table
    <form action="to somewhere" id="idform"></form>

    <tr style="text-align: center;">
        <td>{{ $card -> id }}</td>
        <td>{{ $card -> cardType }}</td>
        <td>{{ $card -> cardValue }} RON</td>
        <td>{{ $card -> createdBy }}</td>
        <td>{{ $card -> created_at }}</td>
        <td>
            <button type="submit" form="idform">Submit</button>         
        </td>
@endforeach

I hope i helped you.

  • 1
    "I usually don't use form inside a table beacuse it will not work" — There is no form inside a table in the code in the question, the form is around the whole table, which is fine. – Quentin Dec 23 '21 at 19:12
  • "Example" - Your example appears to use a **different programming language** to the one the question is about! – Quentin Dec 23 '21 at 19:13
  • 1
    " If i want to redirect me somewhere I use the a tag." – The question is about submitting a form using a POST request, not linking to somewhere with a regular GET request. – Quentin Dec 23 '21 at 19:13