检索表单内的DIV内容

I have been searching for help from various forums and similar posts, but without any progress. I have three pages, one that lets me insert information about projects into my database, a second that show the images and names of every project in the database, and a third page which I want to have a function that shows the image and name of the selected project in the second page.

Code on the second page(dashboardadmin.php):

<?php

ini_set('display_errors',1);
error_reporting(E_ALL);

$conn = mysqli_connect("localhost","root","","wildfire");

if(mysqli_connect_errno())
{
    echo mysqli_connect_error();
}

$sql= "SELECT pid, project_name, image, image_type FROM project";   

$result = $conn->query($sql);

if ($result->num_rows > 0) {
    while($row = $result->fetch_array()) {


echo "<form action='omprojekt.php' method='post'>

<div id='comp' name='comp'>
    <img src=pic.php?pid=".$row['pid']." width=100xp height=100xp/>"." ".$row['project_name']."
</div>

    <input type='submit' name='submit' value='Choose' />
   </form>";


}
}

else {
    echo "0 results";
}

mysqli_close($conn);

        ?>

Code on the third page (omprojekt.php):

<?php
   /* Tried both of the $val variables but of course only one at a time. This is only to show you what I have tried. */

$val = isset($_POST['comp']) ? $_POST['comp'] : '';
$val = $_POST['comp']; 

if(isset($_POST['submit'])){

echo "$val";
}

?>

In the last code you can see that I have two $val variables, but I have only used one of them at a time in my codes. The purpose of showing both of them here is to show you that I have tried both of them.

What I want to do is to make the third page show the image and name of the selected project in the second page. As you see, I have tried to retrieve the content from the DIV using the same "name". The problem is that the third page(omprojekt.php) doesn't show any content at all, and not even any errors.

You're expecting the div to submit like an input, but it won't, because it's not an input. So put it in an input.

if ($result->num_rows > 0) {
    while($row = $result->fetch_array()) {
        // Don't use and id attribute because you're in a loop and you might have multiple id's with the same value.
        echo "<form action='omprojekt.php' method='post'>
            <div> 
                <img src=pic.php?pid=".$row['pid']." width=100xp height=100xp/>"." ".$row['project_name']."
            </div>
            <input type='hidden' name='pid' value='".$row['pid']."'>
            <input type='hidden' name='project_name' value='".$row['project_name']."'>
            <input type='submit' name='submit' value='Choose' />
       </form>";
    }
}

Then on the next page,

$val = (isset($_POST['pid']) && isset($_POST['project_name'])) ? 
    "<img src=pic.php?pid={$_POST['pid']} width=100xp height=100xp/> {$_POST['project_name']}" : '';

For the sake of completeness, there are a few other things wrong with your code.

1) The width and height attributes on the iamge should have quotes, and do not accept "px", they are just numbers. If you want to use "px" you should use style instead. <img src='' style='width:20px; height:20px;' />

2) You should be escaping user input before running it through your query.

Data will only be sent from a <form> to the action script if it exists in an <input...> HTML tag. You cannot pick data out of randon <DIV> tags etc.

So you could do this by using a hidden input field like this ( this is only one way )

if ($result->num_rows > 0) {
    while($row = $result->fetch_array()) {


       echo "<form action='omprojekt.php' method='post'>

             <div>
                <img src=pic.php?pid=".$row['pid'] .
                  " style="width:100px;height:100px" /> " .
                  $row['project_name']."
             </div>

             <input type='hidden' name='comp' value='" . $row['pid'] . "' />
             <input type='submit' name='submit' value='Choose' />
             </form>";


    }
}

Now when you get to omprojekt.php the $_POST['comp'] variable will exist.

You can have as many hidden input fields as you like so if you want to pass the project_name as well just add another hidden field.