donglizuo8892 2019-02-21 09:24
浏览 92
已采纳

如何在输入(onchange)中将文本写入php / html中的数组?

My code:

<?php
$testthis[] = 0;

$mysql = new mysqli("host","name","password","database");
if($mysql->connect_error) {
        echo "connection error: ".mysqli_connect_error()."<br/>";
}
else {
echo "<p>succeed</p>";
}   

$result = $mysql->query("SELECT * FROM t_table;");
while($line = $result->fetch_array()) {
            $help = $line["sNr"];

            echo "<tr><td>".htmlspecialchars($line["sNr"])."</td>"
                    ."<td>".htmlspecialchars($line["c2"])."</td>"
                    ."<form action='my.php' method='post'>"

Here is the mess:

                    ."<td>"."<input type='text' name='$help' value='$help' onchange=".$testthis[$help] = $_POST[$help]."/>"
                    ."</td>"
                    ."
 </tr></form>";
        }
?>

My idea is, that 'sNr' equals the index of an array so that I can later easily write the input from the html (what will be some kind of count or size) to an other table on my database (with 'sNr' as foreign key).

  • 写回答

2条回答 默认 最新

  • dongwupei7803 2019-02-21 19:05
    关注

    A few thoughts, Hans.

    (1) PHP is not able to interact with a user (i.e. detect DOM events like button clicks or text input into an <input> field or etc). That's because once the DOM has been rendered and control turned over ot the user, PHP has finished running. It has no functionality to interact with the user. Use javascript/jQuery.

    (2) Here is a simple bit of (untested) code that might do what you want:

    Here is a proposed slight improvement on your code:

        $result = $mysql->query("SELECT * FROM t_table;");
        while($line = $result->fetch_array()) {
        $help = $line["sNr"];
    
        $htmlOut = "<tr>
                <td>" .htmlspecialchars($line["sNr"]). "</td>
                <td>" .htmlspecialchars($line["c2"]). "</td>
                <td>
                    <form action='my.php' method='post'>
                        <input id="myFormID" type='text' name='$help' value='$help' onchange=".$testthis[$help] = $_POST[$help]."/>
                    </form>
                </td>
            </tr>";
            
        $htmlOut .= "
            <input id='snr_hidden' type='hidden' value='" .$line["sNr"]. "' />
            <input id='phlp_hidden' type='hidden' value='" .$_POST[$help]. "' />
            <!-- Note: you need jQuery library loaded before you can use jQuery js library-->
            <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
            <script>
                $(document).ready(function(){
                    $('#myFormID').blur(function(){
                        var tst = $(this).val();
                        var snr = $('#snr_hidden').val();
                        var pst = $('#phlp_hidden').val();
                        if (tst !== snr){
                            $('#myFormID').val(pst);
                        }
                    });
                }); //END document.ready
            </script>
        ";
    
        echo $htmlOut;

    Here is a working demo of the example:


    $(document).ready(function(){
        $('#myFormID').blur(function(){
            var val = $(this).val();
            var tst = $('#hdnHelpVal').val();
            var pst = $('#hdnPostVal').val();
            if (val !== tst){
                $('#inpCompare').val(pst);
            }else{
                $('#inpCompare').val(val);
            }
        });
        
        $('#myFormID').focus(); //START with cursor in first input field
    }); //END document.ready
    table{border:1px solid #ccc;border-collapse:collapse;padding:5px;}
    input{width:250px;}
    
    div{color:#aaf;font-family:sans-serif;margin:25px 0;}
    span{color:#89a;}
    aside{display:inline;color:blue;}
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
    <table>
        <tr>
            <td>
                <form action='my.php' method='post'>
                    <input id="myFormID" type='text' value='The hidden HELP valueDELETEME' />
                </form>
            </td>
            <td>
                <input id="inpCompare" type='text' placeholder="Tab over to this field" />
            </td>
        </tr>
    </table>
    <input id="hdnHelpVal" type="hidden" value="The hidden HELP value" />
    <input id="hdnPostVal" type="hidden" value="Data received via $_POST" />
    
    <div>When you tab to the right-side field, jQuery/javascript will check the value in the left field and do something based on whether the contents == the hidden input field<br><br><span>If the left side is not (exactly) "<aside>The hidden HELP value</aside>", then the javascript code will fetch the value from input#hdnPostVal and place that data into the right side</span> </div>

    Notes on my code example:

    1. Just as your code is not complete, my example is not a working example. Please just 
    use it to get ideas, and try some of the ideas in your project.
    
    2. PHP is great at building an HTML page and spitting it out to the user. But once 
    the user has control, PHP is done. Finished. PHP has no way to interact with the user, 
    or to detect user events like button clicks, or changes to a drop-down, or etc. 
    ***Use javascript/jQuery***
    
    3. If you aren't comfortable working with javascript, I recommend learning how to use 
    jQuery (a javascript library) so you can come up to speed quickly and save typing.
    
    Read this: https://stackoverflow.com/a/54757176/1447509
    
    4. Don't use:
            "<td>Something here</td>" . "<td>Summat else</td>";
    when you can use:
            "<td>Something here</td><td>Summat else</td>";
    
    5. When working on tables, make sure all your HTML is placed *within* <td> elements. 
    Do not place <form> tags, or divs, or etc, between rows, or between <td>s - for example, 
    this is wrong:
    
        <tr><td></td>In between text<td></td></tr>  <=== text is between <td> elements
    
    This is also wrong:
    
      <tr><td>Text in table cell</td><td>Text</td></tr>
        Never put anything BETWEEN rows
      <tr><td>Text</td><td>Text</td></tr>
            
    
    6. One way to transfer data from PHP to javascript is to write it into a hidden text 
    field as you are building the HTML code *(study how I did that in the above example)*.
    After the DOM is written, the data just sits there, hidden, until javascript retrieves 
    it.
    
    7. Note that if using the above idea, the data in that hidden input field is visible 
    to anyone who presses `F12` and snoops around using DevTools. A more secure way is to 
    save the data on the PHP side (either in MySQL or in a text file on the server) and 
    retrieve it on-the-fly using AJAX (which is actually pretty *easy*)

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?