使用Ajax / Json / jQuery将数据加载到输入字段中

时间:2017-10-25 08:42:04

标签: javascript php jquery json ajax

我正在使用jQuery,Ajax和PHP。 我想要实现的是点击(在php / html文档中)将数据加载到输入字段。

我的功能如下

 functions.js


       //Laden Kopfdaten
$("#tablek tbody tr").on("click", function(){
  var ladeid = $(this).attr('id');
  LoadK(ladeid);
 });

 function LoadK(id) {
    var ladeid = ladeid;
    try {
        req = new XMLHttpRequest();
    }       
    catch (e) {
        try {
            req = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) {
            try {
                req = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (failed) {
                req = null;
            }
        }
    }

    if (req == null) {
        alert("Error creating request object!");
    }
    req.open("GET", "laden.php?id="+id, true);

    // Nach Request auszuführende Aktion
    req.onreadystatechange = handleLoadK; 
    req.setRequestHeader("Content-Type","application/x-www-form-urlencoded");       
    req.send(null);
}

function handleLoadK() {
    switch (req.readyState) {
        case 4:
        if (req.status != 200) {
            alert("Fehler:" + req.status);
        }
        else {
            // Formularfelder
            id = $("#dataid");
            herst_name = $("#herst");
            herst_strasse = $("#herst1");
            herst_hausnr = $("#herst2");
            herst_plz = $("#herst3");
            herst_stadt = $("#herst4");
            firma = $("#firma");
            liefernr = $("#firma1");
            firma_strasse = $("#firma2");
            firma_hausnr = $("#firma3");
            firma_plz = $("#firma4");
            firma_stadt = $("#firma5");
            auftragsnr = $("#info");
            auftragsdt = $("#info1");
            ordernr = $("#info2");
            sachbearbeiter = $("#info3");
            kundennr = $("#info4");
            verladedt = $("#info5");
            spedition = $("#info6");
            lieferdt = $("#info7");
            lieferuz = $("#info8");
            lieferungab = $("#info9");
            lieferscheinnr = $("#info10");
            relation = $("#info11");

            // Rückgabe des Servers
            var resultK = JSON.parse(req.responseText);

            // Ergebnisverarbeitung
            id.val(resultK.id);
            herst_name.val(resultK.herst_name);
            herst_strasse.val(resultK.herst_strasse);
            herst_hausnr.val(resultK.herst_hausnr);
            herst_plz.val(resultK.herst_plz);
            herst_stadt.val(resultK.herst_stadt);
            firma.val(resultK.firma);
            liefernr.val(resultK.liefernr);
            firma_strasse.val(resultK.firma_strasse);
            firma_hausnr.val(resultK.firma_hausnr);
            firma_plz.val(resultK.firma_plz);
            firma_stadt.val(resultK.firma_stadt);
            auftragsnr.val(resultK.auftragsnr);
            auftragsdt.val(resultK.auftragsdt);
            ordernr.val(resultK.ordernr);
            sachbearbeiter.val(resultK.sachbearbeiter);
            kundennr.val(resultK.kundennr)
            verladedt.val(resultK.verladedt);
            spedition.val(resultK.spedition);
            lieferdt.val(resultK.lieferdt);
            lieferuz.val(resultK.lieferuz);
            lieferungab.val(resultK.lieferungab);
            lieferscheinnr.val(resultK.lieferscheinnr);
            relation.val(resultK.relation);
        }
    }
}

我的load.php

<?php
    if(isset($_GET['id'])){
        include('./inc/kopf.class.php');
        $kopf0->load($_GET['id']);
        $result = $kopf0->kopfdaten;
        $json=json_encode($result);
        print_r ($json);
    }

    else {
        include('./inc/position.class.php');
        $pos0->load($_GET['positionsid']);
        $result = $pos0->positionsdaten; 
        print(trim(json_encode($result)));
    }
?>

由于我有2个不同的文档,我正在为此处的if-else实现加载。现在它并不重要,我正在研究&#34; if&#34; -part。

部分来自使用的表格(通过ajax在div id中实现=&#34; tablek&#34;)

<?php   
        for($i=0;$i < $anzahl;$i++){
        ?>      
        <tr id=" <?php echo $kdaten[$i][0]; ?> ">
            <td class="t1 t4" width="15px">
                <?php 
                    echo $kdaten[$i][0];
                ?>
            </td>
            <td class="t1 t4" width="180px">
                <?php
                    echo $kdaten[$i][1];
                ?>
            </td>
            <td class="t1 t4" width="180px">
                <?php
                echo $kdaten[$i][2];
                ?>
            </td>
            <td class="t1 t4" width="180px">
                <?php
                    echo $kdaten[$i][3];
                ?>
            </td>
            <td class="t1 t4" width="180px">
                <?php
                    echo $kdaten[$i][4];
                ?>
            </td>
            <td class="t1 t4" width="180px">
                <?php
                    echo $kdaten[$i][5];
                ?>
            </td>
        </tr>
    <?php
    }
    ?>

这是我的主要问题。 我有一个ID,一个来自它的行,但如果我点击任何行,则没有任何内容加载到输入字段中。我没有任何错误,也没有任何反应。我桌子上的家伙在平等的事情上工作,我们有相同的代码。对他来说这很有效,这让我很困惑。

非常感谢任何帮助。 感谢

编辑:生成的table.php的源代码片段

<table cellspacing="0px" width=70%>
        <tr>
            <th class="t4">
                ID
            </th>
            <th class="t4">
                Hersteller
            </th>
            <th class="t4">
                Stadt
            </th>
            <th class="t4">
                Firma
            </th>
            <th class="t4">
                Stadt
            </th>
            <th class="t4">
                Kundennummer
            </th>
            <th class="t4">
                Löschen
            </th>
            <th class="t4">
                Positionen
            </th>
            <th class="t4">
                Druckvorschau
            </th>
            <th class="t4">
                Anzahl Positionen
            </th>
        </tr>


        <tr id=" 1 ">
            <td class="t1 t4" width="15px">
                1           </td>
            <td class="t1 t4" width="180px">
                Vallah          </td>
            <td class="t1 t4" width="180px">
                            </td>
            <td class="t1 t4" width="180px">
                            </td>
            <td class="t1 t4" width="180px">
                            </td>
            <td class="t1 t4" width="180px">
                00000000000         </td>
            <td class="t1 t4" width="15px">
                <form action="einfuegen.php?aktion=loeschen" method="post">
                <input type="hidden" name="id" id="id" value="1" >
                <input type="button" value="Löschen" name="1" onClick="deleteK(this.name)" style="background-color:Lavender">
                </form>
            <td class="t1 t4">
                <form action="positionen.php?aktion=anzeigen&id=1" method="post">
                <input type="submit" id="showpos" value="Positionen" style="background-color:Lavender">
                </form>
            </td>
            <td class="t1 t4">
                <a href="freixenet.php?id=1" style="background-color:lightblue">Rechnungsdruck</a>
            </td>
        </tr>

        <tr id=" 2 ">
            <td class="t1 t4" width="15px">
                2           </td>
            <td class="t1 t4" width="180px">
                BBBBAA          </td>
            <td class="t1 t4" width="180px">
                djaksdkjas          </td>
            <td class="t1 t4" width="180px">
                djaskdjksajdas          </td>
            <td class="t1 t4" width="180px">
                hwadjwadhjwa            </td>
            <td class="t1 t4" width="180px">
                00002190321         </td>
            <td class="t1 t4" width="15px">
                <form action="einfuegen.php?aktion=loeschen" method="post">
                <input type="hidden" name="id" id="id" value="2" >
                <input type="button" value="Löschen" name="2" onClick="deleteK(this.name)" style="background-color:Lavender">
                </form>
            <td class="t1 t4">
                <form action="positionen.php?aktion=anzeigen&id=2" method="post">
                <input type="submit" id="showpos" value="Positionen" style="background-color:Lavender">
                </form>
            </td>
            <td class="t1 t4">
                <a href="freixenet.php?id=2" style="background-color:lightblue">Rechnungsdruck</a>
            </td>
        </tr>

        <tr id=" 3 ">
            <td class="t1 t4" width="15px">
                3           </td>
            <td class="t1 t4" width="180px">
                xxxxxx          </td>
            <td class="t1 t4" width="180px">
                dwdadwa         </td>
            <td class="t1 t4" width="180px">
                dwadwadwa           </td>
            <td class="t1 t4" width="180px">
                dwadwadwa           </td>
            <td class="t1 t4" width="180px">
                00000121321         </td>
            <td class="t1 t4" width="15px">
                <form action="einfuegen.php?aktion=loeschen" method="post">
                <input type="hidden" name="id" id="id" value="3" >
                <input type="button" value="Löschen" name="3" onClick="deleteK(this.name)" style="background-color:Lavender">
                </form>
            <td class="t1 t4">
                <form action="positionen.php?aktion=anzeigen&id=3" method="post">
                <input type="submit" id="showpos" value="Positionen" style="background-color:Lavender">
                </form>
            </td>
            <td class="t1 t4">
                <a href="freixenet.php?id=3" style="background-color:lightblue">Rechnungsdruck</a>
            </td>
        </tr>

        <tr id=" 5 ">
            <td class="t1 t4" width="15px">
                5           </td>
            <td class="t1 t4" width="180px">
                jkejkwqk            </td>
            <td class="t1 t4" width="180px">
                jllkjew         </td>
            <td class="t1 t4" width="180px">
                jqwekwq         </td>
            <td class="t1 t4" width="180px">
                jkewqk          </td>
            <td class="t1 t4" width="180px">
                00000325322         </td>
            <td class="t1 t4" width="15px">
                <form action="einfuegen.php?aktion=loeschen" method="post">
                <input type="hidden" name="id" id="id" value="5" >
                <input type="button" value="Löschen" name="5" onClick="deleteK(this.name)" style="background-color:Lavender">
                </form>
            <td class="t1 t4">
                <form action="positionen.php?aktion=anzeigen&id=5" method="post">
                <input type="submit" id="showpos" value="Positionen" style="background-color:Lavender">
                </form>
            </td>
            <td class="t1 t4">
                <a href="freixenet.php?id=5" style="background-color:lightblue">Rechnungsdruck</a>
            </td>
        </tr>

        <tr id=" 6 ">
            <td class="t1 t4" width="15px">
                6           </td>
            <td class="t1 t4" width="180px">
                HERRU           </td>
            <td class="t1 t4" width="180px">
                Herrouuoou          </td>
            <td class="t1 t4" width="180px">
                Herruuuu            </td>
            <td class="t1 t4" width="180px">
                Herruouo            </td>
            <td class="t1 t4" width="180px">
                03216675454         </td>
            <td class="t1 t4" width="15px">
                <form action="einfuegen.php?aktion=loeschen" method="post">
                <input type="hidden" name="id" id="id" value="6" >
                <input type="button" value="Löschen" name="6" onClick="deleteK(this.name)" style="background-color:Lavender">
                </form>
            <td class="t1 t4">
                <form action="positionen.php?aktion=anzeigen&id=6" method="post">
                <input type="submit" id="showpos" value="Positionen" style="background-color:Lavender">
                </form>
            </td>
            <td class="t1 t4">
                <a href="freixenet.php?id=6" style="background-color:lightblue">Rechnungsdruck</a>
            </td>
        </tr>

        <tr id=" 10 ">
            <td class="t1 t4" width="15px">
                10          </td>
            <td class="t1 t4" width="180px">
                jujujuju            </td>
            <td class="t1 t4" width="180px">
                lololo          </td>
            <td class="t1 t4" width="180px">
                ololoollo           </td>
            <td class="t1 t4" width="180px">
                popopo          </td>
            <td class="t1 t4" width="180px">
                00008978076         </td>
            <td class="t1 t4" width="15px">
                <form action="einfuegen.php?aktion=loeschen" method="post">
                <input type="hidden" name="id" id="id" value="10" >
                <input type="button" value="Löschen" name="10" onClick="deleteK(this.name)" style="background-color:Lavender">
                </form>
            <td class="t1 t4">
                <form action="positionen.php?aktion=anzeigen&id=10" method="post">
                <input type="submit" id="showpos" value="Positionen" style="background-color:Lavender">
                </form>
            </td>
            <td class="t1 t4">
                <a href="freixenet.php?id=10" style="background-color:lightblue">Rechnungsdruck</a>
            </td>
        </tr>

        <tr id=" 11 ">
            <td class="t1 t4" width="15px">
                11          </td>
            <td class="t1 t4" width="180px">
                hehu            </td>
            <td class="t1 t4" width="180px">
                hetze           </td>
            <td class="t1 t4" width="180px">
                hueh            </td>
            <td class="t1 t4" width="180px">
                xoxo            </td>
            <td class="t1 t4" width="180px">
                00032553335         </td>
            <td class="t1 t4" width="15px">
                <form action="einfuegen.php?aktion=loeschen" method="post">
                <input type="hidden" name="id" id="id" value="11" >
                <input type="button" value="Löschen" name="11" onClick="deleteK(this.name)" style="background-color:Lavender">
                </form>
            <td class="t1 t4">
                <form action="positionen.php?aktion=anzeigen&id=11" method="post">
                <input type="submit" id="showpos" value="Positionen" style="background-color:Lavender">
                </form>
            </td>
            <td class="t1 t4">
                <a href="freixenet.php?id=11" style="background-color:lightblue">Rechnungsdruck</a>
            </td>
        </tr>

        <tr id=" 12 ">
            <td class="t1 t4" width="15px">
                12          </td>
            <td class="t1 t4" width="180px">
                hehu            </td>
            <td class="t1 t4" width="180px">
                hetze           </td>
            <td class="t1 t4" width="180px">
                hueh            </td>
            <td class="t1 t4" width="180px">
                xoxo            </td>
            <td class="t1 t4" width="180px">
                00032553335         </td>
            <td class="t1 t4" width="15px">
                <form action="einfuegen.php?aktion=loeschen" method="post">
                <input type="hidden" name="id" id="id" value="12" >
                <input type="button" value="Löschen" name="12" onClick="deleteK(this.name)" style="background-color:Lavender">
                </form>
            <td class="t1 t4">
                <form action="positionen.php?aktion=anzeigen&id=12" method="post">
                <input type="submit" id="showpos" value="Positionen" style="background-color:Lavender">
                </form>
            </td>
            <td class="t1 t4">
                <a href="freixenet.php?id=12" style="background-color:lightblue">Rechnungsdruck</a>
            </td>
        </tr>

编辑:我的主要文件(已实施的表格及更新后的ID)

<?php
    // Klasseneinbindung
    include('inc/kopf.class.php'); 

    // Spalten
    for ($i=0;$i <= 24; $i++){
        $result[$i]="";
    }

    // Köpfe zählen 
    $kopfcount = $kopf0->zaehlen();

    // Daten suchen
    if(isset($_POST['suchbegriff'])){
        $kopf0->suchen($_POST['suchbegriff']);
        $kdaten = $kopf0->tabledata;
        $anzahl = $kopf0->anzahl;
    }
?>
<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" href="css/stylesheeteinfg.css">
        <meta charset="utf-8">
        <script type="text/javascript" src="js/jquery-3.2.1.js"></script>
        <script type="text/javascript" src="js/einfuegen.js"></script>
        <title> Datensatzbearbeitung </title>
        <h2>
            Kopfsätze
        </h2>
    </head>
    <body style="background-color:aquamarine">
        <form action="einfuegen.php?aktion=speichern" id="kopfform" method="post">
        <legend>
            <p id="uhr"></p>
            <p id="result"></p>
        </legend>
        <br/>

        <!--Validierungserror-->
        <p id="error_para" ></p>
            <table width="83%">
                <tr>
                    <td valign=top>
                        <fieldset>
                            <legend> Herstellerinformationen </legend>
                                <label>Hersteller*<br/><input type="text" id="herst" name="hersteller" value="<?php echo $result[1]?>"/>
                                        <!-- <input type="text" value="" id="checkresponse" style="background-color:aquamarine; border:none" readonly> -->
                                </label><br/>
                                <label>
                                    Strasse* <br/><input type="text" id="herst1" name="herststrasse" value="<?php echo $result[2]?>">
                                </label><br/>
                                <label>
                                    Hausnummer* <br/><input type="text" id="herst2" name="hausnummerherst" value="<?php echo $result[3]?>">
                                </label><br/>
                                <label>
                                    PLZ* <br/><input type="number" id="herst3" name="plzherst" onchange="if (!this.value.match(/^[0-9]{5}$/))  {this.style.background='#f00'; alert('Die Postleitzahl ist ungültig, bitte anpassen'); if(showAlert==true){showAlert=false};}
                                        else {this.style.background='#00ff00';}" value="<?php echo $result[4]?>">
                                </label><br/>
                                <label>
                                    Stadt* <br/><input type="text" id="herst4" name="stadtherst" value="<?php echo $result[5]?>">
                                </label>        
                        </fieldset>
                    </td>
                    <td valign=top>
                        <fieldset>
                            <legend> Empfängerinformationen </legend>
                            <label>
                                Lieferungsempfänger*<br/> <input type="text" id="firma" name="empfaenger" value="<?php echo $result[6]?>">
                            </label><br/>
                            <label>
                                Liefernummer* <br/><input type="number" id="firma1" name="liefernr" value="<?php echo $result[7]?>">
                            </label><br/>
                            <label>
                                Lieferstrasse* <br/><input type="text" id="firma2" name="lieferstr" value="<?php echo $result[8]?>">
                            </label><br/>
                            <label>
                                Hausnummer* <br/><input type="text" id="firma3" name="lieferhausnr" value="<?php echo $result[9]?>">
                            </label><br/>
                            <label>
                                PLZ* <br/><input type="number" id="firma4" name="lieferplz" onchange="if (!this.value.match(/^[0-9]{5}$/)) 
                                                                                                {this.style.background='#f00'; alert('Die Postleitzahl ist ungültig, bitte anpassen'); if(showAlert==true){showAlert=false};}
                                                                                                else {this.style.background='#00ff00';}"
                                                                                                value="<?php echo $result[10]?>">
                            </label><br/>
                            <label>
                                Stadt* <br/><input type="text" id="firma5" name="lieferstadt" value="<?php echo $result[11]?>">
                            </label>
                        </fieldset>
                    </td>
                    <td width="35%">
                        <fieldset>
                            <legend> Lieferdetails </legend>
                            <label> 
                                Auftragsnummer* <br/><input type="number" id="info" name="auftragsnr" value="<?php echo $result[12]?>">
                            </label><br/>
                            <label>
                                Auftragsdt <br/> <input type="date" name="auftragsdt" id="info1" value="<?php echo $result[13]?>">
                            </label><br/>
                            <label>
                                Ordernummer* <br/><input type="number" name="ordernr" id="info2" value="<?php echo $result[14]?>">
                            </label><br/>
                            <label>
                                Sachbearbeiter <br/><input type="text" name="sachbearbeiter" id="info3" value="<?php echo $result[15]?>">
                            </label><br/>
                            <label>
                                Kundennummer* <br/><input type="number" name="kundennr" id="info4" value="<?php echo $result[16]?>">
                            </label><br/>
                            <label>
                                Verladedatum <br/><input type="date" name="verladedt" id="info5" value="<?php echo $result[17]?>">
                            </label><br/>
                            <label>
                                Spedition <br/><input type="text" name="spedi" id="info6" value="<?php echo $result[18]?>">
                            </label><br/>
                            <label>
                                Lieferdatum <br/><input type="date" name="lieferdt" id="info7" value="<?php echo $result[19]?>">
                            </label><br/>
                            <label>
                                Lieferuhrzeit <br/><input type="time" name="lieferuz" id="info8" value="<?php echo $result[20]?>">
                            </label><br/>
                            <label>
                                Lieferung ab <br/><input type="datetime-local" name="liefab" id="info9" value="<?php echo $result[21]?>">
                            </label><br/>
                            <label>
                                Lieferscheinnummer* <br/><input type="number" name="lieferscheinnr" id="info10" value="<?php echo $result[22]?>">
                            </label><br/>
                            <label>
                                Relation <br/><input type="text" name="relation" id="info11" value="<?php echo $result[23]?>">
                            </label><br/>
                        </fieldset>
                    </td>
                    <td valign="top">
                        <fieldset>
                            <legend>
                                Aktionen
                            </legend>
                                <input type="Button" id="kopfspeichern" value="Daten speichern" style="background-color:Lavender"/>
                                <input type="hidden" value="<?php echo $result[0]?>" id="dataid">
                                </form>
                                <br/>
                                <input type="reset" value="Änderungen zurücksetzen" style="background-color:Lavender" onClick="return confirm('Eingaben verwerfen?')">                  
                                <input type="button" onclick="Kopieren()" id="Copypaste" value="Kopieren (Laden - Kopieren - Speichern)" <?php if (empty($_POST['id'])){ ?>disabled="disabled"<?php } ?>>
                                <form action="einfuegen.php" method="post">
                                <input type="Button" onClick="NeuK()" value="Neuer Eintrag" style="background-color:Lavender">
                            </form>
                        </td>
                        <form action="einfuegen.php?search" method="post">
                            <input type="Submit" value="Suchen" style="background-color:Lavender"> <input type="text" name="suchbegriff" id="suchbegriff" placeholder="Suchbegriff eingeben" style="background-color:white">
                            <br/>
                        </form>
                        <form action="einfuegen.php" method="post">
                            <input type="submit" value="Vollansicht" style="background-color:Lavender">
                        </form>
                        <br/>
                        <br/>
                        <legend readonly>
                            Datensatz-ID:   
                                <input type="text" name="id" id="dataid" value="<?php   echo $result[0];?>">
                        </legend>
                    </fieldset>
                </tr>
            </table>
            Zuletzt aktualisiert: <div id="date"> </div>
            <br/>
            <legend>
                Anzahl der Einträge:
            <?php   
                echo $kopfcount;
            ?> 
            <br/><br/>
            </legend>
            <br/>
            <b> Datensatzliste </b>
            <div id="tablek"></div>
            <script> 
                function Kopieren(){
                    document.getElementById('id5').value = "";
                    alert('Die Daten wurden kopiert');
                }
            </script>
    </body>
</html>

1 个答案:

答案 0 :(得分:0)

用户stee1rat提供的解决方案

  

也许你应该尝试把你的$(“#tablek tbody tr”)。on(“click”,function(){}放在jquery的.load()的完整函数中?像这样:$(“#tablek “)。load(”tabelle.php“,function(){$(”#tablek tbody tr“)。on(”click“,function(){console.log('这样工作吗?')var ladeid = $(this).attr('id'); LoadK(ladeid);});});

由于我的'showtable'功能和我的加载功能分开,它不起作用,但合并它们都完美无缺,没有任何错误。

格式化代码:

$("#tablek").load("tabelle.php", function() { 
  $("#tablek tbody tr").on("click", function(){ 
    console.log('Does it work this way?') 
    var ladeid = $(this).attr('id'); 
    LoadK(ladeid); 
  }); 
});