简体   繁体   English

如何使用javascript更改隐藏类型输入字段的值

[英]How to change the value of a hidden type input field with javascript

What i want to do is each time user clicks the button Add and calls the addRow function i will also call row_counter function to set the value of the hidden input type field equal to the number of dynamic rows tha have been created. 我要做的是每次用户单击“添加”按钮并调用addRow函数时,我还将调用row_counter函数以将隐藏输入类型字段的值设置为等于已创建的动态行数。 so if i have dynamicly created 3 rows the hidden input type field must be 3. I am new to javascript so please be patient. 因此,如果我动态创建了3行,则隐藏的输入类型字段必须为3。我是javascript新手,请耐心等待。

<?php 
// Parse the form data and add inventory item to the system
if (isset($_POST['product_name'])) {

    $product_name = mysql_real_escape_string($_POST['product_name']);
    $price = mysql_real_escape_string($_POST['counter']);
    $details = mysql_real_escape_string($_POST['details']); 
    $category = mysql_real_escape_string($_POST['category_choice']);
    $condition= mysql_real_escape_string($_POST['condition']);;
    $supplier_choice= mysql_real_escape_string($_POST['supplier_choice']);;

    // See if that product name is an identical match to another product in the system
    $sql = mysql_query("SELECT id FROM products WHERE product_name='$product_name' LIMIT 1");
    $productMatch = mysql_num_rows($sql); // count the output amount
    if ($productMatch > 0) {
        echo 'Sorry you tried to place a duplicate "Product Name" into the system, <a href="inventory_list.php">click here</a>';
        exit();
    }
    // Add this product into the database now
    $sql = mysql_query("INSERT INTO products (product_name, price, details, category, active, supplier, date_added) 
        VALUES('$product_name','$price','$details','$category','$condition','$supplier_choice',now())") or die (mysql_error());


     $pid = mysql_insert_id();
    // Place image in the folder 
    $newname = "$pid.jpg";
    move_uploaded_file( $_FILES['my_photo']['tmp_name'], "../inventory_images/$newname");
    //ean den yperxan oi 2 epomenes grammes ean ekana refresh tha prospathouse na perasei to idio proion ksana!!
    header("location: inventory_list.php"); 
    exit();

}

?> ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Inventory Area</title>

<link rel="stylesheet" href="../style/style.css" type="text/css" media="screen"/>

<SCRIPT language="javascript">
    //Add Information
    i=1;
    function addRow(tableID,counter1) {
        //The rows num starts from 0
        var table = document.getElementById(tableID);

        var rowCount = 7;
        var row = table.insertRow(rowCount);

         var cell1 = row.insertCell(0);

        var cell2 = row.insertCell(1);
        var element = document.createElement("input");
        element.type = "text";
        element.name = "information_" + i;
        element.id = "information_" + i;
        element.size = 84;
        cell2.appendChild(element);
        i++;
        row_counter_inc(counter1);
    }

    function deleteRow(tableID) {
        if(i>=2){
            var table = document.getElementById(tableID);
            table.deleteRow(i+5);
            i--;
        }
    }
    function row_counter_inc(counter2){
        w=i;
        w=w-1
        document.getElementByID("counter2").value=w;
    }

</SCRIPT>

<div align="center" id="mainWrapper"> 

  <?php include_once("template_header.php");?>

  <div id="pageContent">

        <div align="left" style="margin-left:24px">
            <h2>Inventory List</h2>
            <?php echo $productList ?>
        </div>

  </div>

  <div>

        <form action="inventory_list.php" enctype="multipart/form-data" name="my_Form" id="my_Form" method="post">

        <a name="add_New_Product_Form" id="add_New_Product_Form">

            <h2>Add New Product Form</h2>

        </a>

        <table width="80%" border="0" cellspacing="0" cellpadding="6" id="dataTable">

            <tr>

                <td>Όνομα</td>
                <td>

                    <label>

                        <input name="product_name" type="text" id="product_name" size="80" />


                    </label>

                </td>

            </tr>

            <tr>

                <td>Κατάσταση</td>
                <td>

                    <label>

                        <input type="radio" name="condition" value="1"> Ενεργό</input>
                        <input type="radio" name="condition" value="0"> Ανενεργό</input>

                    </label>

                </td>

            </tr>


            <tr>

                <td>Τιμή</td>
                <td>

                    <label>

                        <input name="price" type="text" id="price" size="20" />

                    </label>

                </td>

            </tr>

            <tr>

                <td>Προμηθευτές</td>
                <td>

                    <?php echo $suppliers_list; ?>

                </td>

            </tr>

            <tr>

                <td>Κατηγορία</td>
                <td>

                    <?php echo $category_list; ?>

                </td>

            </tr>

            <tr>

                <td>Περιγραφή</td>
                <td>

                    <label>

                        <textarea name="details" id="details" cols="64" rows="5"></textarea>

                    </label>

                </td>

            </tr>

            <tr>

                <td>Τεχνικά Χαρακτηριστικά</td>
                <td>

                    <input type="text" name="information_0" id="information_0" size="84"/>

                </td>

            </tr>

            <tr>

                <td></td>
                <td>

                    <input type="hidden" name="counter" id="counter"/>
                    <INPUT type="button" value="Προσθήκη" onClick="addRow('dataTable','counter')"/>
                    <INPUT type="button" value="Αφαίρεση" onClick="deleteRow('dataTable')"/>

                </td>

            </tr>

            <tr>

                <td>Φωτογραφία</td>
                <td>

                    <label>

                        <input type="file" name="my_photo" id="my_photo"/>

                    </label>

                </td>

            </tr>

            <tr>

                <td>&nbsp;</td>
                <td>

                    <label>

                        <input type="submit" name="button" id="button" value="Αποθήκευση" />

                    </label>

                </td>

            </tr>

        </table>

        </form>



  </div>

   <br /><br />

  <?php include_once("../template_footer.php");?>

</div>

enter code here

这应该为您工作:

 document.getElementById("counter").value = 3;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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