donglizuo8892 2019-02-21 09:24 采纳率: 0%
浏览 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).

  • 写回答

1条回答 默认 最新

  • douzhushen_9776 2019-02-24 15:37
    关注

    Here's how I do it ...

    First a PHP snippet to grab the client POST request and save/update the form data in the db. The PDO.inc.php handles creating prepared statement, filtering, etc. I don't care about any output, status return, etc - so I don't bother checking status of query, just execute and end.

    <?php
        include('security.php');
        include('PDO.inc.php');
    
        $q="update qualityEval set ".$_POST['thingID']."=?, date_last_modified=? where evaluator=? and course=?";
        $qa=array($_POST['thingVal'],time(),$_SESSION['user'],$_SESSION['course']);
        $res=executeQuery($q,$qa);
    
    ?>
    

    Then in my HTML form, I bring in jquery and set up a handler for OnChange/etc. events that I want to trigger a save on

    <script type="text/javascript" src="/ct3-code/jquery.min.js"></script>
    <script type="text/javascript">
        function SaveChange(thingToSave){
            var pass_data = {
                        'thingID' : thingToSave.name,
                        'thingVal' : thingToSave.value,
                    };
                    $.ajax({
                        url : "/ct3-code/saveIt.php",
                        type : "POST",
                        data : pass_data,
                        success : function(data) {
                        }
                    });
                    return false;
        }
    </script>
    

    And then finally I print out a textarea, input type, radio button set, etc.

    print("<tr bgcolor=#ffffff><td colspan=4>Comments on <strong>".$secTitle."</strong><br />
            <textarea onInput=\"SaveChange(this);\" rows=7 cols=80 wrap=physical id=\"".$k."Comments\" name=\"".$k."Comments\">");
            if($res[1][$k."Comments"]!==null){print($res[1][$k."Comments"]);}
    print("</textarea>");
    

    As a user types in the textarea (or selects a checkbox or radio button, etc) each change triggers the http POST call to save the contents of whatever is being modified. Last triggered event wins and is what is actually kept in DB.

    评论

报告相同问题?