简体   繁体   English

Javascript 无法与我的表单一起正常工作

[英]Javascript won't work properly with my form

So I am currently doing a project for our class, in which I have to display something that I previously stored in a database on a separate page.所以我目前正在为我们的 class 做一个项目,我必须在一个单独的页面上显示我以前存储在数据库中的一些东西。 I chose to display every dataset, as it is the easiest to do.我选择显示每个数据集,因为它是最容易做到的。 With PHP I implemented a foreach loop which is supposed to show each dataset with a button "EDIT" underneath it.使用 PHP 我实现了一个 foreach 循环,该循环应该显示每个数据集,其下方有一个“编辑”按钮。 When I don't use javascript and just have the class of the edit form set to show, it works, and I can edit all my entries but the form is always visible.当我不使用 javascript 并且仅将编辑表单的 class 设置为显示时,它可以工作,并且我可以编辑所有条目,但表单始终可见。 If I try to hide the form with javascript, all the entries are still shown, but if I press the button "EDIT" which has a seprete function to set the class to "show" it only gives me the option to edit the first entry in the database.如果我尝试使用 javascript 隐藏表单,所有条目仍会显示,但如果我按下具有单独 function 的按钮“编辑”以将 class 设置为“编辑”选项,则它只给我“第一个条目”在数据库中。

Here is my code for the php/html:这是我的 php/html 代码:

foreach ($resultArray as $row) {
      echo '<h1 class="title">' . $row['title'] . '</h1>';
      echo '<img classe="headerImage" src="images/' . $row['picture'] . '" alt="' . $row['alternativetext'] . '" width="500px">';
      echo '<p class="countryDisplay"> We were in ' . $row['countryName'] . '</p>';
      echo '<p class="startDateDisplay">Started on: ' . $row['startDate'] . '</p>';
      echo '<p class="endDateDisplay">Ended on: ' . $row['endDate'] . '</p>';
      echo '<p class="storyContent">' . $row['blogContent'] . '</p>';

      echo '<button class="edit-button" onclick="showContent()">EDIT</button>';

      echo '<section class="hide" id="edit-form">
                <form action="' . $_SERVER['PHP_SELF'] . '" method="POST" accept-charset="utf-8" enctype="multipart/form-data">
                <p class="hide">
                    <input type="hidden" name="id" id="id" value="' . $row['blogID'] . '">
                </p>
                <p class="title-par">
                  <label for="title" class="title">Title:</label><p class="required">*</p><br>
                  <input type="text" name="title" id="title" value="' . $row['title'] . '"><br>
                </p>
                <div class="picture-div">
                  <label for="picture" class="picture">Upload a picture:</label><br>
                  <input type="file" name="picture" id="picture" value="' . $row['picture'] . '"><br>
                </div>
                <p class="alttext-par">
                  <label for="alttext">Give your Picture a fitting Name:</label><br>
                  <input type="text" name="alternativetext" id="alternativetext" value="' . $row['alternativetext'] . '"><br>
                </p>
                <div class="country-div">
                  <label for="country" class="country" require>Select Country:</label><p class="required">*</p><br>
                  <select name="country" id="country">';
                // Connect to Database:
                // Database Query
                $queryCountries = 'SELECT countryID, countryName FROM countries ORDER BY countryName ASC';

                //Usee $queryCountries
                $resultArrayCountries = $db->query($queryCountries);
                // Jede Zeile ist ein eigenes Land
                foreach ($resultArrayCountries as $rowCountries) {
                  echo "<option value=\"$rowCountries[countryID]\">$rowCountries[countryName]</option>";
                }
                echo '</select><br>
                </div>
                <p id="date-par">
                  <label for="startDate" class="startDate">Start Date:</label><p class="required">*</p>
                  <input type="date" name="startDate" id="startDate" value="' . $row['startDate'] . '">
                  <label for="endDate" class="endDate">End Date:</label><p class="required">*</p>
                  <input type="date" name="endDate" id="endDate" value="' . $row['endDate'] . '"><br>
                </p>
                <p class="story-par">
                  <label for="story" class="story">Say Something:</label><p class="required">*</p><br>
                  <textarea name="story" id="story" cols="60" rows="30">'.$row['blogContent'].'</textarea><br>
                </p>
                <p id="action-buttons">
                  <input class="save-button" type="submit" name="save" id="save" value="SAVE">
                  <input class="cancel-button" type="submit" name="cancel" id="cancel" value="CANCEL" onclick="hideContent()">
                </p>
                </form>
                </section>
                </section>';
    };

and here is the javascript (it is kept very simple, as we haven't had a javascript instruction yet)这是 javascript (它保持非常简单,因为我们还没有 javascript 指令)

function resetForm(){
    document.getElementById("newForm").resetForm();
}
function showContent(){
    document.getElementById("edit-form").classList.add("show");
    document.getElementById("edit-form").classList.remove("hide");
}
function hideContent(){
    document.getElementById('edit-form').classList.remove("show");
    document.getElementById('edit-form').classList.add("hide");
}

(I am sorry for all grammatical mistakes, I am not native) (对不起所有语法错误,我不是本地人)

I found a solution that worked for me... I don't know why it didn't work before but I had to work with variables.我找到了一个对我有用的解决方案......我不知道为什么它以前不起作用,但我必须使用变量。

function showContent(x){
    const editFormElement = document.getElementById('edit-form'+x);
    const editFormElementClassList = editFormElement.classList;
    editFormElementClassList.add('show');
    editFormElementClassList.remove('hide');
}
function hideContent(x){
    const editFormElement = document.getElementById('edit-form'+x);
    const editFormElementClassList = editFormElement.classList;
    editFormElementClassList.add('hide');
    editFormElementClassList.remove('show');
}

IDs need to be unique on your HTML document. ID 在您的 HTML 文档上必须是唯一的。 document.getElementById('edit-form') will return the first element on the page with the id edit-form . document.getElementById('edit-form')将返回页面上 ID edit-form的第一个元素。 You could for an example append the row number to your id, and then pass it as a parameter to your javascript functions.例如,您可以将 append 行号传递给您的 id,然后将其作为参数传递给您的 javascript 函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM