Lexicon
Lexicon

Reputation: 145

Passing php variables

I am creating a user interface for managing a database. Each entry has two radio buttons, one for validation (and subsequent emailing to concerned authorities) and one for deleting the entry. The code is given below, and the bold parts are the ones which concern this functionality. A couple of questions: 1) Is it possible to do so without the form attribute? If so, how would I do it?

2) With the form attribute, I use 'handle.php' to delete each entry. The syntax in the 'handle.php' includes this line

$sql="DELETE FROM rti WHERE ID=x"; //x here is the ID number to delete.

Now, how do I pass the value of the ID as well from my interface, so that the above line of code deletes the entry corresponding to the button that was pressed?

<html>
    <head>
        <style>
        * {
            margin: 0px;
            padding: 0px;
        }

        div.topbar {
            position: relative;
            background-color: black;
            height: 45px;
            text-align: center;
            font-family: Calibri;
            font-size: 30px;
            padding-top: 10px;
        }

        div.topbar img {
            position: absolute;
            left: 10px;
            top: 0px;
        }

        div.topbar span {
            color: white; 
        }

        div.container {
            background-color: #a5a5a5;
            width: 100%;
            height: 100%;
            color: white;
        }

        div.data {
            padding: 20px;
        }

        table {
            background-color: #b1b1b1;
            border-collapse: collapse;
        }


        th, td {
            text-align: center;
            border: solid 1px black;
            margin: 0px;
        }

        th.one {
            width: 50px;
        }

        th.two{
            width: 150px;
        }

        th.three {
            width: 75px;
        }

        th.four {
            width: 200px;
        }

        div.validation {
            background-color: #b5b5b5;
            width: 200px;
            height: 100%;
            position: relative;
            left: 1100px;
            bottom: 300%;
            text-align: center;
            border-left: solid 1px black;
        }
        </style>
    </head>
    <body>
        <div class = "topbar"><img src="logo.png"><span>RTI DATABASE</span></div> 
            <div class = "container">
                <div class ="data"> 
                    <?php 
                    $servername="localhost";
                    $username="root";
                    $password='';
                    $conn=mysql_connect($servername, $username, $password) or die ("Error connecting to mysql server: ".mysql_error());
                    $dbname = 'bsp';   
                    mysql_select_db($dbname, $conn) or die ("Error selecting specified database on mysql server: ".mysql_error());
                    $sql="SELECT * FROM rti";
                    $result=mysql_query($sql) or die ("Query to get data from firsttable failed: ".mysql_error());   
                    echo "<table>";
                    echo "<tr>";
                    echo '<th class="one">ID</th>
                        <th class="two">Name</th>
                        <th class="three">Board</th>
                        <th class="four">Query</th>
                        <th class="five">Validate</th>
                        <th class="six">Delete</th>
                        <th class="seven">Submit</th>';

                    echo "</tr>";
                    while ($row=mysql_fetch_array($result)) {
                        $id=$row["ID"];
                        $name=$row["name"];
                        $board=$row["board"];
                        $query=$row["query"];
                        echo "<tr>";
                        echo "<td>$id</td>
                            <td>$name</td>
                            <td>$board</td>
                            <td>$query</td>
                           **<form action='handle.php' method='POST'>
                                <td><input type='radio' name='option' value='validate'></td>
                                <td><input type='radio' name='option' value='delete'></td>
                                 <td><input type='submit' value='Submit' name='submit'>
                            </form></td>";
                        echo "</tr>";**
                    }
                echo "</table><br>";
                ?>
            </div>
        </div>
    </body>
</html>

Upvotes: 2

Views: 45

Answers (3)

Muhammad Shahzad
Muhammad Shahzad

Reputation: 9652

**<form action='handle.php' method='POST'><td><input type='radio' name='option' value='validate'></td>
   <td><input type='radio' name='option' value='delete'>
  <input type='hidden' name="id" value='".$id."'> 
  </td>
   <td><input type='submit' value='Submit' name='submit'></form></td>";

In handle.php:

$id = $_POST['id'];

Upvotes: 1

arisalsaila
arisalsaila

Reputation: 429

inside your form add an hidden type input with the id value for each item , so each time user click the button the id sent. just like this :

 <form action='handle.php' method='POST'><td><input type='radio' name='option' value='validate'></td>

<input type = 'hidden' name="id" value='".$row['id']."'><td>
            <input type='radio' name='option' value='delete'></td>
        <td><input type='submit' value='Submit' name='submit'></form></td>";

Upvotes: 1

Denis Gianne
Denis Gianne

Reputation: 1

use something like <input type='radio' name='option[your_id]' value='validate'> to identify the item. Like: <input type='radio' name='option[$id]' value='validate'>

Upvotes: 0

Related Questions