dpdhsq0783 2015-12-07 13:05
浏览 74
已采纳

检索表单内的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.

  • 写回答

2条回答 默认 最新

  • doumie6223 2015-12-07 13:18
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算