且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

自动填充动态生成的表单

更新时间:2023-02-04 10:06:12

以下是我如何解决这个问题。我已经在评论中给出了所有这些信息,但是如果我以编号的方式列出它,这可能会有所帮助,因此它是毫不含糊的。这可能是***的避免在这里跳过项目,因为每个任务都依赖于前一个任务。


  1. 第一个任务是修复您的重复 id 属性。考虑循环中的HTML:

     < td> 
    < input type =textname =input3 []
    placeholder =Enter QTYsize =5id =displayqty>
    < / td>

    这将为您提供多个输入标签,其中包含唯一标识符冲突。你可以在这里使用一个整数,或者 $ row ['SN'] 这样做吗?如果这个值是唯一的,你可以这样做:

     < td> 
    < input type =textname =input3 []
    placeholder =Enter QTYsize =5
    id =displayqty_<?php echo(int)$行['SN']→>
    >
    < / td>

    (为了安全起见,我强制它为int,但是如果它被定义为一个整数数据库,你不需要这个)。

    一旦你修改了你的代码来为你的id属性提供唯一的名字,查看你生成的HTML并确保它可以工作。

  2. 然后,您需要修改您的JavaScript函数,以便您可以告诉它使用什么 id 来使用。由于它现在硬连线到 displayqty ,它最多只会修改一个元素,这不是你想要的。尝试切换:

      function showqty(str)

    至此:

     函数showqty(str,id)

    然后,您可以使用变量 id 而不是displayqty在你的函数中。

  3. 最后把它连接起来,你的改变处理器需要传入目标 id 。改变:

      showqty(this.value)

    转换为之前用于 id 的任何字符串。例如,也许这是吗?

      showqty(this.value,displayqty_<?php echo(int)$ row ['SN ']?>)


请记住,这没有经过测试,我只能通过一个关于你的屏幕在这里做什么的传递想法。如果没有一点调整和调试你的最终目标,不要指望它能够正常工作 - 如果你愿意坚持下去,你就会到达那里。祝你好运!


I built a form that can select data from MySQL database and the desired field of the selected row will be displayed in an input field of a form. However, this form is dynamic in the sense that you can have multiple forms of the same form.

If two or more forms are generated, only the first form is auto populated. I want each form to auto-populate their corresponding input field upon selection of a row from the database.

Here is the code:

<?php
$start=1;
$end= $_POST['item']; echo'<br>';

for($start;$start<=$end;$start++){

require("connect.php");

$sql = "(SELECT * FROM drug_name)";


 $result = mysqli_query($conn, $sql);


?>
<form align="center" method="post" action="transaction.php">


        Name:
        <select align="right" name="inputname[]" onchange="showunit(this.value); showqty(this.value)"
 value="inputvalue[]">
 <option selected="selected" value="0">S/N Select drug --</option>

 <?php while($row = $result->fetch_assoc()){ ?>

 <option value='<?php echo $row['SN']; ?>'><?php     

echo$row['Drug_Item_Name']?></option> 

        <?php
}
    ?>      

        </select>



        <td  align="right">Stock Level:</td>
        <td><input type="text" name="input3[]" placeholder="Enter QTY" size="5" id="displayqty"></td>
        </tr> 


<?php       echo '<br>'; ?>

<?php }

?>   </form>

This is the code to make the AJAX code:

     <script>
        function showqty(str) {   

            if (str == "") {
        document.getElementById("displayqty").value = "";    
                return;  `
            } else {     
                if (window.XMLHttpRequest) {




                    // code for IE7+, Firefox, Chrome, Opera, Safari
                    xmlhttp = new XMLHttpRequest();
                } else {    
                    // code for IE6, IE5
                    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                }
                xmlhttp.onreadystatechange = function() {
                    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                        document.getElementById("displayqty").value = xmlhttp.responseText;
                    }
                }
                xmlhttp.open("GET","qtydisplay.php?q2="+str,true);
                xmlhttp.send();
            }
        }  


        </script>

This is the AJAX code to fetch data from MySQL database:

        <?php

        $q2 = intval($_GET['q2']);
        include("connect.php");//connection to database


        mysqli_select_db($conn,"ajax_demo");
        $sql="SELECT Qty_Stocked FROM drug_name WHERE SN = '".$q2."' LIMIT 1";
        $result = mysqli_query($conn,$sql);


        while($row = mysqli_fetch_array( $result)) {
            echo $row['Qty_Stocked'];  
        }

        mysqli_close($conn);
        ?>

Here's how I'd resolve this. I've given all this information in comments already, but perhaps it will help if I list it in a numbered fashion, so that it is unambiguous. It is probably best to avoid skipping items here, since each task relies on the previous one.

  1. The first task is to fix your duplicate id attributes. Consider this HTML inside your loop:

    <td>
        <input type="text" name="input3[]"
               placeholder="Enter QTY" size="5" id="displayqty">
    </td>
    

    That will give you multiple input tags with conflicting unique identifiers. You could use an integer here, or would $row['SN'] do it? If that value is unique, you could do something like this:

    <td>
        <input type="text" name="input3[]"
               placeholder="Enter QTY" size="5"
               id="displayqty_<?php echo (int) $row['SN'] ?>"
        >
    </td>
    

    (I have forced it to an int for security reasons, but if it is defined as an integer in the database, you don't need this).

    Once you have modified your code to render unique names for your id attributes, view your generated HTML and make sure it works.

  2. You then need to modify your JavaScript function so that you can tell it what id to use. Since it is hardwired presently to displayqty it will only modify one element at most, which is not what you want. Try switching this:

    function showqty(str)
    

    to this:

    function showqty(str, id)
    

    You can then use the variable id instead of "displayqty" in your function.

  3. Finally to wire it all together, your change handler needs to pass in the target id. Change this:

    showqty(this.value)
    

    to whatever string you used for id before. For example, maybe this?

    showqty(this.value, "displayqty_<?php echo (int) $row['SN'] ?>")
    

Bear in mind that this is not tested, and I only have a passing idea of what your screen is doing here. Do not expect this to work without a little bit of tweaking and debugging your end - if you are willing to persist, you will get there. Good luck!