douda5706 2018-07-20 21:25
浏览 79

隐藏的输入仅从循环Data-php获取最后的值

I am trying to make a small image/quiz application that allows users to upload a question, while other users answer those questions. The other users are allowed to select up to three checkboxes for their answers. Once selected, this data is to be posted to a script that checks the guesses against a database that has the correct answers. Many questions are on the same page, and more are echoed onto the page via a php script as users upload data. To ensure that each question has a unique identifier, I have a hidden input for the quiz form that takes on the value given from a loop. The first question works great! However, the other questions are unanswerable, because the hidden input seems to only take on the value of the last element in the loop(DESCENDING order). Also, the POST mechanism only seems to care about the most recent item as well.

Here is the code which loops out images and questions to the page:

$sql = "SELECT * FROM gallery1 ORDER BY idGallery DESC";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)) {
echo "SQL Statment Failed";
}else {
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);

while ($row = mysqli_fetch_assoc($result)){

echo '<div class="maincontainer">

    <div class="thecard">

        <div class="thefront" style="background-image:url(gallery/'.$row["imageFullNameGallery"].'); background-size:100% 100%;">
        </div>

        <div class="theback">
            <p class="heading"> The Story</p>
            <p class="desc">
              '.$row["descriptionGallery"].'
              '.$row["imageFullNameGallery"].'
            </p>


        <form action="includes/survey.php" id="form1" method="POST">

        <label> <img src="img/IMAGE1.png" class="radio">
        <input type="checkbox" name="food" value="1">
        </label>

        <label> <img src="img/IMAGE2.png" class="radio">
        <input type="checkbox" name="water" value="1">
        </label>

        <label> <img src="img/IMAGE3.png" class="radio">
        <input type="checkbox" name="shelter" value="1">
        </label>

        <input type="hidden" name="image" value='.$row["imageFullNameGallery"].'> 


        </form>
    <button type="submit" form="form1" name="submit">Submit </button>

        </div>

    </div>

</div>';

}
}

?>

Notice that the '.$row["imageFullNameGallery"].' is under the <p class="desc"> tag in the above code. This properly gives me the full name of each image. I did this just to test if there were errors here. There are none though.

This is the code that handles the hidden input once it has been received(survey.php):

<?php
if (isset ($_POST['submit'])) {

include_once "dbh.php";

$food=$_POST['food'];
$water=$_POST['water'];
$shelter=$_POST['shelter'];
$image=$_POST['image'];

$survey = array("foodGallery"=>$food, "waterGallery"=>$water, "shelterGallery"=>$shelter);

$sql= "SELECT foodGallery, waterGallery, shelterGallery FROM gallery1 WHERE imageFullNameGallery = '$image';";

$stmt = mysqli_stmt_init($conn);
mysqli_stmt_prepare($stmt, $sql);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);

$row = mysqli_fetch_assoc($result);

print_r($row);

if ($survey !== $row){
    echo "These are not the same";

} else{
    echo "These have the same values!";

}


?>

I have done some research, and some suggest that it could be because all of the names for the hidden inputs would be the same, therefore it only goes with the final value passed from the loop. If this is the case, how could this be solved, so that each item has the proper value for its hidden input? Also, any thoughts as to why only the first form's input is being posted? (Remember the order is descending, so it is the form with the true value as being correct from the hidden input; the last value given from the loop). I appreciate you for taking your time to read (and hopefully aid me in my search for truth) my wordy question. THANK YOU!

  • 写回答

1条回答 默认 最新

  • dongmo20030416 2018-07-21 07:15
    关注

    So I solved the issue! All of the forms had an id="form1" . Each submit button also had the element form="form1". By changing both of these fields to be both id='.$row["imageFullNameGallery"].' and form='.$row["imageFullNameGallery"].' respectively, the solution was found! Each form matches with the correct information in the database, and all forms are workable! I hope this helps someone in the future!

    评论

报告相同问题?

悬赏问题

  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写