简体   繁体   English

如何从循环内的表单文本字段中获取特定值?

[英]How to get a specific value from a form text field inside a loop?

I am busy with a school project that mainly focuses on showing data from a database. 我正在忙于一个学校项目,该项目主要致力于显示数据库中的数据。 We build a map with Google Maps API so we could show markers with specific data when you click on them. 我们使用Google Maps API构建地图,因此当您单击它们时可以显示带有特定数据的标记。 The data tagged to the marker are self-written JavaScript variables. 标记到标记的数据是自写的JavaScript变量。 It all works but now comes my real problem. 一切正常,但现在是我真正的问题。 We also have an overview of al datasets were you can easily select any dataset. 如果您可以轻松选择任何数据集,那么我们也对al数据集进行了概述。 When a dataset is clicked you will be redirected to the page with the map and the specific data. 单击数据集后,您将被重定向到包含地图和特定数据的页面。 My problem is that I need to convert the data I get from the database using PHP and MySQL to JavaScript variables that are equal to the variable from the map. 我的问题是我需要将使用PHP和MySQL从数据库中获得的数据转换为与映射中的变量相等的JavaScript变量。

My idea was to create an invisible form where there are six text fields that are filled with the database rows (this part works), next I have to get the specific value of the text field that belongs to the clicked dataset. 我的想法是创建一个不可见的表单,其中有六个用数据库行填充的文本字段(此部分有效),接下来,我必须获取属于单击的数据集的文本字段的特定值。 The only problem is that I am generating the overview of the datasets with a loop so data can be added to the database and the code doesn't need to be adjusted. 唯一的问题是,我正在使用循环生成数据集的概述,以便可以将数据添加到数据库中,而无需调整代码。 But I don't know how to get the specific value. 但是我不知道如何获得特定的价值。

I have tried several ways to do this including getElementbyClass (works but you always need to define the array number), getElementbyID (only grabs the first value because an ID can only be used once), getElementbyName (also only grabs the first value) and some stuff that has to do with jQuery: siblings, next, things that grab the closest value (but I got an empty value). 我尝试了几种方法来做到这一点,包括getElementbyClass(有效,但您始终需要定义数组编号),getElementbyID(仅获取第一个值,因为ID只能使用一次),getElementbyName(也仅获取第一个值)和与jQuery有关的一些东西:兄弟姐妹,接下来是那些获取最接近值的东西(但是我得到了一个空值)。

/ Code is below / /代码在/

HTML & PHP: HTML和PHP:

<?php
    $sql = "SELECT * FROM containers";
    $result = mysqli_query($connectie, $sql);
    if (mysqli_num_rows($result) > 0) {
        // output data of each row
        while($row = mysqli_fetch_assoc($result)) { ?>
           <div class="Border">

           <?php echo $row["container_address"]?>

        <form class="senddataform">

        <input type="text" name="addressField" id="addressField" value=" 
        =$row['container_address']?>">

        <input type="text" name="maxcontent" id="maxcontentField" value="<? 
        =$row['container_maxcontent']?>">

        <input type="text" name="name" id="nameField" value="<? 
        =$row['container_name']?>">

        <input type="text" name="beschikbaarheid" id="beschikbaarheidField" 
        value="<?=$row['container_beschikbaarheid']?>">

        <input type="text" name="gebruikt" id="gebruiktField" value="<? 
       =$row['container_gebruikt']?>">

       <input type="text" name="legingdagen" id="legingdagenField" value="<? 
       =$row['container_legingdagen']?>">

       </form>

<?php } ?>

My empty JavaScript function: 我的空JavaScript函数:

 function sendContainerData(){

 }

JavaScript variables that need to be filled with the data: 需要用数据填充的JavaScript变量:

var containerAddress =
var containerMaxcontent =
var containerName =
var containerBeschikbaarheid =
var containerGebruikt =
var containerLegingdagen =

You can think of it this way: Declare those variables that need to be filed with data as global variables. 您可以这样想:声明那些需要与数据一起作为全局变量提交的变量。 Then in a function, you can get the elements from the invisible text fields by class (which returns an array that that can be accessed by index). 然后在一个函数中,您可以按类从不可见的文本字段中获取元素(该类返回一个可以通过索引访问的数组)。 Now, since you know the order in which the invisible text fields are displayed apriori (from the PHP loop), you can assign the value of each text field to the correct variable. 现在,由于您知道了不可见文本字段的显示顺序(从PHP循环开始),因此可以将每个文本字段的值分配给正确的变量。 For example, the first text field value can be mapped to the correct variable, and so on. 例如,第一个文本字段值可以映射到正确的变量,依此类推。 Something like this: 像这样:

var containerAddress;
var containerMaxcontent;
var containerName;
var containerBeschikbaarheid;
var containerGebruikt;
var containerLegingdagen;

function sendContainerData(){
    var values = document.getElementByClass('someclass'); //returns an array
    //Now use your knowlege of the order of the textfields to map the values
    containerMaxcontent = values[0].value; //maps maxcontent to maxcontent variable
    containerName = values[1].values; //maps name text field to name variable
    containerBeschikbaarheid = values[2];//
    containerGebruikt = values[3]; //
    containerLegingdagen = values[4];
 }

Then you can use those variables as you wish. 然后,您可以根据需要使用这些变量。 Note that you can do this for IDs too. 请注意,您也可以为ID执行此操作。 Only you will need to make your IDs an array. 只有您需要将ID设置为数组。 All your IDs will look like this id="someID[]" . 您所有的ID都将看起来像是id="someID[]" Hope this is clear enough and it helps 希望这足够清楚并且对您有所帮助

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

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