亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將表的動態生成值發送回服務器?

如何將表的動態生成值發送回服務器?

PHP
九州編程 2021-11-05 12:29:34
我有我的 index.php,我將在其中向服務器發出 Ajax 請求并顯示響應(作為表格)。<div id="result"></div><script>    $('#loading-image').show();    function load_data()    {        $.ajax({            url:"fetch.php",            method:"POST",            success:function(data)            {                $('#result').html(data);            }        });    }    load_data();</script>結果將具有 $output 的值:<?phpinclude '../../variables.php';include '../../_Database.php';$db = Database::getInstance(); $minDuration;$maxDuration;$tableRowCounter;$output =     '<table class="table">          <thead>            <tr>              <th scope="col">#</th>              <th scope="col">Artikel</th>              <th scope="col">Durchlaufzeit</th>              <th scope="col">Neue Durchlaufzeit</th>              <th scope="col">Best?tigen</th>            </tr>          </thead>          <tbody>        ';        $result = mysqli_query($db->link, "SELECT * FROM `Person_changes_Article`");        while ($row = mysqli_fetch_assoc($result))        {            $result2 = mysqli_query($db->link, "SELECT * FROM `Article`");            while ($row2 = mysqli_fetch_assoc($result2))            {                if ($row['Article_idArticle'] == $row2['idArticle'])                {                    $minDuration = ($row2['duration'] / 10) * 9;                    $maxDuration = ($row2['duration'] / 10) * 11;                    }                }            }          }$output .= '        </tbody></table>';echo $output;?>我打算為表中的每一行添加復選框。用戶只需選擇應更改哪些行并將其發送回服務器即可。我不知道如何從 td 中獲取值。我嘗試使用 $tableRowCounter 為每個復選框分配一個自動生成的 id。但是,當我嘗試使用 jquery 訪問讓我們說 #t1 的 id 時,它不起作用。像這樣:$(document).ready(function(){  $('#t1').prop('checked', true);});畢竟,在后端準備自動生成的 id 值感覺很糟糕。我想要做的是(或用戶)選中任何行的復選框,然后點擊一個按鈕,將該特定行的表中的值發送到服務器。服務器如何接收數據甚至都無關緊要。我完全可以使用表中所有行的所有值的數組(盡管只是選中的值)如何?請幫忙。
查看完整描述

2 回答

?
慕神8447489

TA貢獻1780條經驗 獲得超1個贊

首先,對于第三個代碼片段,$('#t1')由于在文檔準備好時未呈現表格,因此不可用。該表是在 AJAX 請求響應之后插入的。如果要初始化某些內容,請在 AJAX 請求success回調中進行。


對于提交選中的行,建議使用屬性選擇器。


html輸出:


$output .= '

  <tr>

    <th scope="row">'.$tableRowCounter.'</th>

    <td>'. $db->getArticleString($row2["idArticle"]) ." - " . $row2["idArticle"]. '</td>

    <td>'. $row2["duration"] .'</td>

    <td>'. $row["newDuration"] .'</td>

    <td><input type="checkbox" data-id="'. $row['id'] .'" name="name" value="value"></td>

  </tr>';

然后按鈕處理程序:


$("#submit").onclick(function() {

  var selectedIds = []

  $("table input[type=checkbox]").forEach(function(i, elem) {

    var cb = $(elem)

    if (cb.prop('checked')) {

      selectedIds.push(cb.attr('data-id'))

    }

    // send selectedIds to server

  })

})


查看完整回答
反對 回復 2021-11-05
?
一只萌萌小番薯

TA貢獻1795條經驗 獲得超7個贊

我無法完全將您的解決方案應用于我的問題。但是,我看到了它是如何制作的,并在我對 JavaScript 的理解水平上再次編寫了它。


我知道不應該這樣做,但我沒有看到任何原因為什么我會遇到沒有唯一 ID 的問題


我已經給了我的 tds ID。像這樣:


<tr>

 <th scope="row">'.$tableRowCounter.'</th>

   <td>'. $db->getArticleString($row2["idArticle"]) .'</td>

   <td id="article">'. $row2["idArticle"] .'</td>

   <td id="duration">'. $row2["duration"] .'</td>

   <td id="newDuration">'. $row["newDuration"] .'</td>

   <td>'. $db->getName($row["Person_idPerson"]) . " ".$db->getLastName($row["Person_idPerson"]) .'</td>

   <td id="check"><input type="checkbox" id="check" name="name" value="value"></td>

</tr>

然后我可以輕松地遍歷 tds 并將值推送到數組中:


    var article = [];

    var duration = [];

    var newDuration = [];

    var check = [];


    $("td[id='article']").each(function(){

        article.push($(this).text());

    })

    $("td[id='duration']").each(function(){

        duration.push($(this).text());

    })

    $("td[id='newDuration']").each(function(){

        newDuration.push($(this).text());

    })

    $("input[id='check']").each(function(){

        if ($(this).is(':checked')){

            check.push(1);

        }else{

            check.push(0);

        }   

    })

并使用 Ajax 發布數組


$.ajax({

        type:'POST',

        url:'storeData.php',

        data: { article: article,

                duration: duration,

                newDuration: newDuration,

                check: check

                },

        success:function(data){

          //alert(data)

        },

    });

在 storeData.php 中,我可以使用這些數組并做任何我想做的事


$article = $_POST['article'];

$duration = $_POST['duration'];

$newDuration = $_POST['newDuration'];

$check = $_POST['check'];


print_r($article);

print_r($duration);

print_r($newDuration);

print_r($check);


查看完整回答
反對 回復 2021-11-05
  • 2 回答
  • 0 關注
  • 178 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號