1
   <form action="book.php" method="post">
   <table>
    <thead>
    <tr>
    <td>FlightID</td>
    <td>From</td>
    <td>Destination</td>
    </tr>
    </thead>
    <tbody>
    <tr>
    <td name="flightID" value="1">1</td>
    <td name="From" value="Sydney">Sydney</td>
    <td name="Destination" value="Bali">Bali</td>
    <td class="tdBook"><button class="btnBook" type=submit name="booking"> Book </button>
    </tr>

    <tr>
    <td name="flightID" value="2">2</td>
    <td name="From" value="London">London</td>
    <td name="Destination" value="HongKong">Hong Kong</td>
    <td class="tdBook"><button class="btnBook" type=submit name="booking"> Book </button>
    </tr>

    </tbody>
</table>
</form>

I created a table like this. At the end of each row, it has a book button.

What I am trying to do is when the user clicked the button, the selected row data(ID,From,Des) will pass to the 'book.php', then the PHP file will do the rest of the job. But I tried to catch the value using $_POST['name'] in 'book.php', like this

<?php
    if(isset($_POST['booking'])){
    $ID = $_POST['flightID'];
    $From = $_POST['From'];
    $To = $_POST['Destination'];
}
?>

It shows all of those values are undefined. Any help would be appreciated.

Daniel Choi
  • 105
  • 1
  • 2
  • 9
  • 4
    td is a data cell, not a form field. Form fields (like input, select, textarea) are submitted when you submit a form. Further, you need a separate form for every row to make it work easily. – kainaw May 02 '16 at 14:24
  • `` do not bear the name attribute, inputs do. – Funk Forty Niner May 02 '16 at 14:26
  • Read about [HTML input fields](http://www.w3schools.com/tags/tag_input.asp) and [HTML textarea fields](http://www.w3schools.com/tags/tag_textarea.asp) – Martin May 02 '16 at 14:26
  • You'll likely need to just package the POST data yourself via Javascript on click. That, and drop the form tag / name attributes. If that's not an option, your entire structure will need to change drastically. Even if this *did* function, it would not yield desired results. You'd literally just be submitting all rows of data, all the time, all with the same field names. – Nate I May 02 '16 at 14:27
  • Here, start with the manual on forms http://php.net/manual/en/tutorial.forms.php – Funk Forty Niner May 02 '16 at 14:27
  • put hidden inputs next to the tables or use javascript to extract contents of the table – Daniel W. May 02 '16 at 14:30
  • Thanks kainaw, but Is there any way that I can get the data from the data cell? The main purpose of doing this is I want to have a button next to each flight ticket so users can book. – Daniel Choi May 02 '16 at 14:30
  • possible duplicate of [PHP: “Notice: Undefined variable” and “Notice: Undefined index”](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – Funk Forty Niner May 02 '16 at 14:33
  • @DanielChoi, yes you can use javascript to get the `td` contents and serialize it for a form submission – Jeff Puckett May 02 '16 at 14:34

2 Answers2

3

The problem is that the values in <td> cannot be passed from the form to your PHP file by themselves. You could use hidden inputs for this. Additionally, each row in the table should be its own form to assure that all data is not submitted at the same time.

Try this:

<table>
<thead>
    <tr>
        <td>FlightID</td>
        <td>From</td>
        <td>Destination</td>
    </tr>
</thead>
<tbody>
    <tr>
        <form action="book.php" method="post">
            <td><input type="hidden" name="flightID" value="1">1</td>
            <td><input type="hidden" name="From" value="Sydney">Sydney</td>
            <td><input type="hidden" name="Destination" value="Bali">Bali</td>
            <td class="tdBook"><button class="btnBook" type=submit name="booking"> Book </button>
        </form>
    </tr>

    <tr>
        <form action="book.php" method="post">
            <td><input type="hidden" name="flightID" value="2">2</td>
            <td><input type="hidden" name="From" value="London">London</td>
            <td><input type="hidden" name="Destination" value="HongKong">Hong Kong</td>
            <td class="tdBook"><button class="btnBook" type=submit name="booking"> Book </button>
        </form>
    </tr>

</tbody>

Jon
  • 86
  • 10
0

i have the same problem as yours and tried to create an answer so i came up with this code to indicate each row in an HTML table with a special name using loops, i can now take the specified row and do as much PHP operations as i can with it without disturbing the table as a whole and it was well synchronized with my database, hope it helps!

and btw the whole "marking each row with a special name" code is in usersTable.php

users.sql

create table users(
    id int,
    username varchar(50),
    password varchar(50)
);

users.php

<?php
$host = "localhost";
$username = "root";
$password = "";
$database = "wdl2hw4db";

$conn = mysqli_connect($host, $username, $password, $database);

if (mysqli_connect_errno()){
    die("can't connect to the Database" . mysqli_connect_errno());
}else{
    echo "Database is connected" . "<br>";
}

if (isset($_POST['insert'])){
    $idN1= $_POST['id'];
    $usernameN1 = $_POST['username'];
    $passwordN1 = $_POST['password'];
    $query = "insert into users(id, username, pass) values ('".$idN1."' , '".$usernameN1."' , '".$passwordN1."' )";
    $result = mysqli_query($conn, $query);
}else if (isset($_POST['update'])){
    $idN2 = $_POST['id'];
    $usernameN2 = $_POST['username'];
    $passwordN2 = $_POST['password'];
    $query = "update users set pass = '". $passwordN2 ."'where id = " . $idN2;
    $result = mysqli_query($conn, $query);
}else if (isset($_POST['Display'])){
    header('Location: usersTable.php');
}
echo "<br>";
?>

<form method="post">
    ID: <input type="text" name="id" ><br><br>
    username: <input type="text" name="username" ><br><br>
    password: <input type="password" name="password" ><br><br>
    <input type="submit" name="insert" value="insert">
    <input type="submit" name="Display" value="Display">
</form>

userTable.php

<?php
$host = "localhost";
$username = "root";
$password = "";
$database = "wdl2hw4db";

$conn = mysqli_connect($host, $username, $password, $database);
$query = "select * from users";
$result = mysqli_query($conn, $query);
echo "<table border=\"6px\"><thead><tr><th>ID</th><th>username</th><th>password</th><th>Delete</th><th>Update</th></tr></thead>";
$i = 1;
while ($row = mysqli_fetch_assoc($result)) {
    echo "<tr><form method='post'><td>" . $row['id'] . "</td><td>" . $row['username'] . "</td><td>" . $row['pass'] . "</td><td><input type='submit' name='Delete" . $i . "' value='Delete'></td><td><input type='submit' name='Update" . $i . "' value='Update'><input type='text' name='UpdateText" . $i . "' placeholder='insert new password here'></td></form></tr>";
    $i++;
}
echo "</table>";
$i = 1;
$result2 = mysqli_query($conn, $query);
while ($row2 = mysqli_fetch_assoc($result2)) {
    if (isset($_POST['Delete' . $i])) {
        $usernameN4 = $row2['username'];
        $query2 = "delete from users where username ='" . $usernameN4 . "'";
        $result2 = mysqli_query($conn, $query2);
        header("Refresh:0");
        break;
    }
    $i++;
};

$i = 1;
$result3 = mysqli_query($conn, $query);
while ($row3 = mysqli_fetch_assoc($result3)) {
    if (isset($_POST['Update' . $i]) && $_POST['UpdateText' . $i] != null ) {
        $id4 = $row3['id'];
        $Utext = $_POST['UpdateText' . $i];
        $query3 = "update users set pass ='" . $Utext . "' where id = " . $id4;
        $result3 = mysqli_query($conn, $query3);
        header("Refresh:0");
        break;
    }
    $i++;
};
mysqli_free_result($result);

ayayabood
  • 11
  • 6
  • This is a very bad practice, wide open to sql injections, you should use prepared statments https://www.php.net/manual/en/mysqli.quickstart.prepared-statements.php –  May 08 '20 at 05:56