简体   繁体   中英

How can a new value be entered into an input field after the initial value has been cleared?

The goal is to clear a field using a ON CLICK event which calls the java script function clear field(this.id)The clear field can then receive a new value. When the UPDATE button is pressed the database is updated with the new value. The clear field function succeeds in clearing the field, how can a new value be entered ?

<html>
        <head>
            <script src="JavaScript/getVal.js" type="text/javascript"></script>
        </head>
        <body>
            <a href="poll.html">Poll</a>
            <?php
            $connection = mysqli_connect("localhost", "root", "");
            mysqli_select_db($connection, "ukelection2015");
            $rs = mysqli_query($connection, "select * from poll");
            ?>
            <hr>
            <table border=1 id="myTable">
                <tr>
                    <th>Name</th>
                    <th>Value</th>
                    <th>Color</th>
                    <th></th>

                    <th>  </th>
                </tr>
                <?php
                $id = 0;
                while ($row = mysqli_fetch_array($rs)) {

                    print("<form method= 'post' action= 'updatePoll.php'>");
                    for ($x = 0; $x <= 2; $x++) {
                        if ($x == 0) {
                            print("<tr>");
                        } else {

                            print("<td>" . $row['name'] . "</td>" . "<td id= '".$id."'  value = '" . $row['value'] . "' onclick = 'clearField(this.id)'>" . $row['value'] . "</td>" . "<td>" . $row['color'] . "</td>" . "<td><a href = ''>Update</a></td>");

                            $x++;
                            if ($x == 1) {
                                print "</tr>";
                            }
                            $id++;
                        }
                    }

                    // this hidden field is used in updatePoll.php to get the party name to update (i.e. $name=$_POST['name'];)
                 /*   print("<input type='hidden' name ='name' value={$row['name']}>"); */

                    print("<tr>");

                    // name
                    // value
                    // color

                 /*   print("<td><input type='submit'  value='Update' onclick='alertMsg()'/></td>"); */
                    print("</tr>");
                    print("</form>");
                }
                mysqli_close($connection);
                ?>

            </table>
        </body>
    </html>

       This is the javascript function
/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */


function clearField(anId) {
    document.getElementById(anId).innerHTML = 0; 


}

You can pass the new value as a parameter while calling the clearField function. Then rather than setting the innerHTML to 0 in the function definition, you can set the innerHTML to the new value as document.getElementById(anId).innerHTML = newValue

This may be helpful

<script type="text/javascript>
function clearField(anId) {
    document.getElementById(anId).innerHTML = 0; 
}
</script>

Given that you create a for four each row, you can add the ID of the element that you want to modify as an hidden input and make the field that you want to change an textbox number. When you click update, just submit the form and update In the database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM