简体   繁体   English

获取除法PHP JavaScript中的信息

[英]getting information inside the division php javascript

When I clicked the button " click me ", the selected ID will print to the var divtest then fade out in the <div id="container"> and when I clicked the another button " click me " it only gets the first id but all I want is the second id in the database to show. 当我单击按钮“ 单击我 ”时,选定的ID将打印到var divtest然后在<div id="container">淡出,当我单击另一个按钮“ 单击我 ”时,它只会获得第一个ID,我想要的只是数据库中要显示的第二个ID。 If I try to remove the selected description it will go back to the container of ID description. 如果我尝试删除选定的描述,它将返回到ID描述的容器。

My database sample. 我的数据库样本。

ID Description    
1    Hello
2    Hi
3    Thank You

This is my sample code. 这是我的示例代码。

<?php 
    $conn = mysqli_connect("localhost", "root", "","dblaquaresort") or die("Failed to Connect");
    $sql = "SELECT * FROM tblsample";
    $query =  mysqli_query($conn, $sql);
    while ($sample = mysqli_fetch_array($query)) 
    {
    ?><div id="container">
    <label id="ID">ID: <?php echo $sample['ID']; ?></label>
    <label id="Description">Description: <?php echo $sample['Description']; ?></label>
    <button type="button" id="more_fields" onclick="add_fields();">Click me</button> </div>
    <?php
    }
    ?>

    <div id="room_fileds">
     <div>
       <div class="content">
         <?php
              function not_empty_string($s) 
              {
                return $s !== " ";
              }
              $pieces = array_filter(explode(",", "Test"),'not_empty_string');
              foreach ($pieces as $piece) 
              {
               $test_stinga = array_filter(explode("-", $piece),'not_empty_string');
               $test_stinga=preg_replace('/\s+/', '', $test_stinga);
              }
             ?>
      </div>
     </div>
    </div>
            <script>
                var room = 1;
                var wrapper = $(".testtt");
                function add_fields() 
                {
                room++;
                var objTo = document.getElementById('room_fileds');
                var ID = document.getElementById('ID').innerHTML;
                var Desc = document.getElementById('Description').innerHTML;
                var divtest = document.createElement("div");
                 divtest.innerHTML = '<div class="label">TEST ' + room + ':</div> <div class="content"><span><p class="form-inline">Testing: '+ ID +'</span><span>Description: ' + Desc + '</span><button class="remove"> Remove</button></p></div>';
                   objTo.appendChild(divtest)
                        }
                     $('#room_fileds').on("click",".remove", function(e)
                     {
                     $(this).parents().eq(5).remove();
                     })
                </script>

This is the output I want. 这是我想要的输出。

Div Container
ID: 1 Description: Hello <button>click me</button>
ID: 2 Description: Hi <button>click me</button>
ID: 3 Description: Thank You <button>click me</button>

If i click the ID = 2. 如果我单击ID = 2。

Div Container
ID: 1 Description: Hello <button>click me</button>
ID: 3 Description: Thank You <button>click me</button>

Divtest
ID: 2 Description: Hi <button>Remove</button>

If I Click the remove it will go back in Div Container . 如果单击删除,它将返回到Div Container中

ID: 1 Description: Hello <button>click me</button>
ID: 2 Description: Hi <button>click me</button>
ID: 3 Description: Thank You <button>click me</button>

You're creating the same ID for the HTML elements the 3 times in this loop: 您将在此循环中为HTML元素创建3次相同的ID

while ($sample = mysqli_fetch_array($query)) 

This element <div id="container"> will be 3 times according to your example, you need to make each id different. 根据您的示例,此元素<div id="container">将是3次,您需要使每个id不同。 You can use the Id of the row your fetching from the database so you can do something like: 您可以使用从数据库中获取的行的Id ,以便执行以下操作:

 while ($sample = mysqli_fetch_array($query)) 
    {
    ?><div id="container_<?php echo $sample['ID']; ?>">
    <label id="ID_<?php echo $sample['ID']; ?>">ID: <?php echo $sample['ID']; ?></label>
    ......

so when you add this <?php echo $sample['ID']; ?> 因此,当您添加此<?php echo $sample['ID']; ?> <?php echo $sample['ID']; ?> to your html ids like: id="container_<?php echo $sample['ID']; ?>" you create unique ids for the elements, you need to do this to have unique html ids. <?php echo $sample['ID']; ?>到您的html ID,例如: id="container_<?php echo $sample['ID']; ?>"您将为元素创建唯一的ID,需要执行此操作以具有唯一的html ID。

Now you need to modify your javascript function: 现在,您需要修改javascript函数:

function add_fields() 

you shall make it gets a parameter, like: 您应该使它获得一个参数,例如:

function add_fields(id)

and you can pass the id of the row, so it'll be: 您可以传递该行的ID,因此它将是:

<button type="button" id="more_fields_<?php echo $sample['ID']; ?>" onclick="add_fields(\"<?php echo $sample['ID']; ?>\");">Click me</button> </div>

and you'll need to modify the content of the function, to work with that id : 并且您需要修改函数的内容,以使用该id

  function add_fields(id) 
  {
     room++;
     var objTo = document.getElementById('room_fileds_'+id);
     .....

I hope this helps. 我希望这有帮助。

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

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