-3

I was actually trying to retrieve the input submit button value. But I don't know why it does not work. Can anyone help me?

When the user click the buttons, the button's value will be send to the next page.

<?php
include('connect.php');

// Create connection
$conn = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT * FROM userauth";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while ($row = mysqli_fetch_assoc($result)) {
        ?>

        <html>
            <head>
                <title>GAF APPS</title>  
            </head>
            <body>
                <form method="post" action="branch.php">
                    <input type="submit" name="submit" value="<?php echo $row["Company"]; ?>">
                </form>
            </body>
        </html>

        <?php
    }
} else {
    echo "0 results";
}
$conn->close();
?>

Here is where I was going to retrieve the value:

<?php
if (isset($_POST['action'])) {
    echo '<br />The ' . $_POST['submit'] . ' submit button was pressed<br />';
}
?>
Pankaj Makwana
  • 3,030
  • 6
  • 31
  • 47

2 Answers2

1

You don't have an input named "action", therefore the isset() will never happen which is why you did not get an error for it

Having added an else condition for it, would have shown you that instead.

user229044
  • 232,980
  • 40
  • 330
  • 338
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
-1

When debuging in PHP I tend the use the shotgun approach and output everything that could be remotely interesting and then narrow it down.

So when looking at parsing form variables use echo var_export($_GET, true) or vardump($_GET).

Not sure if GET or POST? Use _REQUEST which has both in 1 variable.

Use the HTML tag to make it more readable and htmlspecialchars() to convert characters that would normally be invisible because of your browser.

Using those will make it far easier to see what you are doing with your form.

So to answer the above question: Look at the the mentioned request variables and determine by looking at the HTML and the code if the expected variables should be parsed and send by the browser when the submit button is pressed.

AND

See if the values actually received by PHP will have the expected outcome when handled.

Try to keep those 2 things separate, because what is in your HTML now does not mean it was there when you parsed the form. That's a bit of a bind when developing with PHP/HTML and forms, when you change the code and do not fully reload, but just press Submit on the form: the code that will parse the current form will be changed, but the contents of the form parsed are the ones that where loaded in your browser and might be out dated.

RC NL
  • 88
  • 4