0

I have a table with 3 columns like so in index.php:

ID   |   StName       |   Inspect
_________________________________
1     Wisconsin           INSPECT
2     Alabama             INSPECT 
3     Nebraska            INSPECT

The right most column is a submit button which takes the user to a page called inspect.php. I want to send the corresponding ID column value to inspect.php when a user clicks the INSPECT button so that I can use that value inside inspect.php, how can I do this?

Currently I have:

//index.php

      echo "<table border = 1>
           <tr>
                <td>ID</td>
            <td>StName</td>
               </tr>";

         // go into a loop to create more rows -- omitted
         echo "<tr>";
            echo "<td>" . $resultArr[0] . "</td>";
            echo "<td>" . $resultArr[1] . "</td>";
            echo "<td>  <form action='inspect.php' method='get'>
                    <input type='Submit' value='Inspect'>
                    </form>
                 </td>";
            echo "</tr>";
      echo "</table>";

$resultArr[0] contains the ID value I want to send to inspect.php

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Isaac Perez
  • 570
  • 2
  • 15
  • 31

2 Answers2

6

So add an input field inside the form with a name attribute which will be an index key name on inspect.php page

<form action='inspect.php' method='get'>
   <input type='hidden' name="id" value='<?php echo $resultArr[0]; ?>'>
   <input type='submit' value='Inspect'>
</form>

A better and simple way to go for this is without a submit, just use a hyperlink like

<a href="inspect.php?id=<?php echo $resultArr[0]; ?>">Inspect</a>

And later process this id on inspect.php page

Mr. Alien
  • 153,751
  • 34
  • 298
  • 278
  • Thanks so much! How would I process it inside my inspect.php page if I used the second method you mentioned? Right now I have this inside inspect.php: $stateId = $_GET[$resultArr[0]]; But I'm not even sure what that's trying to do... – Isaac Perez May 04 '13 at 04:45
  • @IsaacPerez Use an `if` condition like `if(isset($_GET['id']) && $_GET['id'] != '') {/* Process Here */}` – Mr. Alien May 04 '13 at 04:47
  • @IsaacPerez You should accept correct answers for the questions you ask, this will help future visitors to refer – Mr. Alien May 04 '13 at 04:50
  • Thanks for the answer and advice. I am trying to print out the ID by using your bit of code and in the "Process Here" portion of it I try echoing my result like so: echo $_GET['id'] But nothing happens. What's the correct way of getting the ID and storing it in a variable? – Isaac Perez May 04 '13 at 05:00
1

I think I got what you wanted to work. I am using different data in the array, but you should be able to change to your needs anyway.

On index.php:

$resultArr = array("dave","fff","erere");

 echo "<table border = 1>
           <tr>
                <td>ID</td>
            <td>StName</td>
               </tr>";

         // go into a loop to create more rows -- omitted
         echo "<tr>";
            echo "<td>" . $resultArr[0] . "</td>";
            echo "<td>" . $resultArr[1] . "</td>";
            echo "<td>  <form action='inspect.php' method='get'>
   <input type='hidden' name='id' value=' $resultArr[0] '>
   <input type='submit' name='inspect' value='Inspect'> </td>";
            echo "</tr>";
      echo "</table>";

And on inspect.php:

if( $_GET['inspect']){

    $name = $_GET['id'];
        echo $name;

}

This outputs the value of $resultArr[0], which I think you wanted.

J0e3gan
  • 8,740
  • 10
  • 53
  • 80
05beckga
  • 95
  • 2
  • 9