Feralheart
Feralheart

Reputation: 1920

Creating a button what pass a variable to the next page

I building an inventory management solutions in php for my father's shop. This is the code what lists the inventory in table from MySQL database.

<?php 
    $sql = "SELECT * FROM inventory";
    $res = mysqli_query($conn, $sql) or die ('Wrong statement!');
    echo '<table border=1>';
    echo '<tr>';
    echo '<th>ID</th>';
    echo '<th>Type</th>';
    echo '<th>Name</th>';
    echo '<th>Wholesaleprice</th>';
    echo '<th>Price</th>';
    echo '<th>Supplier</th>';
    echo '<th>Place</th>';
    echo '<th>Place2</th>';
    echo '<th>Count</th>';
    echo '<th>Barcode</th>';
    echo '<th>Last Change</th>';
    echo '<th>Details</th>
    echo '</tr>';       

    while ( ($current_row = mysqli_fetch_assoc($res))!= null) {
            echo '<tr>';
            echo '<td>' . $current_row["ID"] . '</td>';
            echo '<td>' . $current_row["type"] . '</td>';
            echo '<td>' . $current_row["name"] . '</td>';
            echo '<td>' . $current_row["wholeprice"] . '</td>';
            echo '<td>' . $current_row["price"] . '</td>';
            echo '<td>' . $current_row["wholesaler"] . '</td>';
            echo '<td>' . $current_row["whereis"] . '</td>';
            echo '<td>' . $current_row["whereis2"] . '</td>';
            echo '<td>' . $current_row["count"] . ' ' .   $current_row["dimension"] . '</td>';
            echo '<td>' . $current_row["barcode"] . '</td>';
            echo '<td>' . $current_row["lastchange"] . '</td>';
            echo '<td> HERE GOES THE BUTTON </td>';
            echo '</tr>';
    }
    echo '</table>';                                
    mysqli_free_result($res);?>

How I can put a button (form or button class), what navigates to the "productdetails.php" and pass the current row's barcode as a variable, too?

I tried:

<form action="productdetails.php" method="get">
    <button type="submit" class="button" value="' . $current_row["barcode"] . '">Product Details</button> 
</form>

Upvotes: 1

Views: 46

Answers (2)

Feralheart
Feralheart

Reputation: 1920

I tinkered with it a bit with @Florian's answer and I ended up with this working code snippet:

while ( ($current_row = mysqli_fetch_assoc($res))!= null) {
$barcode= $current_row["barcode"];

 echo '<tr>';
 echo '<td>' . $current_row["ID"] . '</td>';
 echo '<td>' . $current_row["type"] . '</td>';
 echo '<td>' . $current_row["name"] . '</td>';
 echo '<td>' . $current_row["wholeprice"] . '</td>';
 echo '<td>' . $current_row["price"] . '</td>';
 echo '<td>' . $current_row["wholesaler"] . '</td>';
 echo '<td>' . $current_row["whereis"] . '</td>';
 echo '<td>' . $current_row["whereis2"] . '</td>';
 echo '<td>' . $current_row["count"] . ' ' . $current_row["dimension"] . '</td>';
 echo '<td>' . $current_row["barcode"] . '</td>';
 echo '<td>' . $current_row["lastchange"] . '</td>';?>
 <td> <form method="post" action='productdetails.php'>
    <input type="hidden" name="barcode" value="<?php echo "$barcode"?>"/>
    <button type="submit" class="button">More>></button> 
    </form></td>
 <?php echo '</tr>';
}

Upvotes: 0

Florian Sch&#246;ffl
Florian Sch&#246;ffl

Reputation: 499

You were pretty close actualy. Try it with this code snippet;

<form action="productdetails.php?barcode=' . $current_row["barcode"] . '" method="get">
    <button type="submit" class="button">Product Details</button> 
</form>

You will then be able to get it in the productdetails.php with $_GET['barcode'].

Upvotes: 1

Related Questions