dongpu2727 2012-03-06 19:49
浏览 47
已采纳

编辑动态创建的行的值

What i am trying to do is recover data from a table and add them dynamicly to a table for the user to edit. The thing is tha i can't find the way when the user clicks Προσθηκη to add a new empty field with the function addRow.

<?php 

    session_start();

    if (!isset($_SESSION['manager'])) {

        header("location: admin_login.php");
        exit();

    }

    $managerID= preg_replace('#[^0-9]#i','', $_SESSION["id"]);
    $manager= preg_replace('#[^A-Za-z0-9]#i','', $_SESSION["manager"]);
    $password= preg_replace('#[^A-Za-z0-9]#i','', $_SESSION["password"]);

    include "../storescripts/connect_to_mysql.php";
    $sql=mysql_query("SELECT * FROM admin WHERE id='$managerID' AND username='$manager' AND password='$password' LIMIT 1");

    $existCount=mysql_num_rows($sql);
    if($existCount==0){

        echo "Your Log In session data is not on record in the database";
        exit(); 
    }

?>

<?php
    // Script Error Reporting
    error_reporting(E_ALL);
    ini_set('display_errors', '1');
?>

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

        $pid = mysql_real_escape_string($_POST['thisID']);
        $product_name = mysql_real_escape_string($_POST['product_name']);
        $price = mysql_real_escape_string($_POST['price']);
        $category = mysql_real_escape_string($_POST['category']);
        $subcategory = mysql_real_escape_string($_POST['subcategory']);
        $details = mysql_real_escape_string($_POST['details']);
        // See if that product name is an identical match to another product in the system
    $sql = mysql_query("UPDATE products SET product_name='$product_name', price='$price', details='$details', category='$category', subcategory='$subcategory' WHERE id='$pid'");

        if($_FILES['my_photo']['tmp_name'] != ""){
            // Place image in the folder 
            $newname = "$pid.jpg";
            move_uploaded_file( $_FILES['my_photo']['tmp_name'], "../inventory_images/$newname");
        }
        header("location: inventory_list.php"); 
        exit();

    }


?>

<?php 
    //Edit Product Block
    if (isset($_GET['pid'])) {  

        $targetID=$_GET['pid'];
        $sql=mysql_query("SELECT * FROM products WHERE id='$targetID' LIMIT 1");
        $product_count=mysql_num_rows($sql);

        if($product_count > 0){

            while($row = mysql_fetch_array($sql)){

                $product_name=$row["product_name"];
                //Condition
                $condition_t=$row["active"];
                if($condition_t == 1){
                    $condition="<tr>
                                    <td>Κατάσταση</td>
                                    <td>
                                        <label>
                                            <input type='radio' name='condition' value='1' checked='checked'> Ενεργό</input>
                                            <input type='radio' name='condition' value='0'> Ανενεργό</input>
                                        </label>
                                    </td>
                                </tr>";
                }else if($condition_t == 0){
                    $condition="<tr>
                                    <td>Κατάσταση</td>
                                    <td>
                                        <label>
                                            <input type='radio' name='condition' value='1'> Ενεργό</input>
                                            <input type='radio' name='condition' value='0' checked='checked'> Ανενεργό</input>
                                        </label>
                                    </td>
                                </tr>";
                }
                $price=$row["price"];
                //Supplier
                $supplier=$row["supplier"];
                $sql_2=mysql_query("SELECT id,name FROM suppliers ORDER BY id");
                $suppliers_list = "<select name='supplier_choice'>
                <option value='0'>-------</option>";
                while($row_2 = mysql_fetch_array($sql_2)){
                    $suppliers_id=$row_2["id"];
                    $suppliers_name=$row_2["name"];
                    if($suppliers_id == $supplier){
                        $suppliers_list .= "<option value='$suppliers_id' selected='selected'>$suppliers_name</option>";
                    }else{
                        $suppliers_list .= "<option value='$suppliers_id'>$suppliers_name</option>";
                    }
                }
                $suppliers_list .="</select>";
                //Category
                $category=$row["category"];
                $category_list = "<select name='category_choice'><option value='0'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-----------</option>";
                $sql_3=mysql_query("SELECT id,category_name FROM category WHERE tier='1'");
                while($row_p = mysql_fetch_array($sql_3)){
                    $id_p=$row_p["id"];
                    $category_name_p=$row_p["category_name"];
                    $category_list .= "<option value='$id_p' disabled='disabled'>$category_name_p</option>";
                    $sql_4=mysql_query("SELECT id,category_name FROM category WHERE parent='$id_p'");
                    while($row_c = mysql_fetch_array($sql_4)){
                        $id_c=$row_c["id"];
                        $category_name_c=$row_c["category_name"];
                        if($category == $id_c){
                            $category_list .= "<option value='$id_c' selected='selected'>&nbsp;&nbsp;&nbsp;&nbsp;$category_name_c</option>";
                        }else{
                            $category_list .= "<option value='$id_c'>&nbsp;&nbsp;&nbsp;&nbsp;$category_name_c</option>";
                        }

                    }
                }
                $category_list .="</select>";
                $details=$row["details"];
                //Info
                $sql_5=mysql_query("SELECT id, add_info FROM info WHERE parent='$targetID'");
                $info_count=mysql_num_rows($sql_5);
                global $info_count;
                $info_table ="                    
                    <tr>
                        <td>Τεχνικά Χαρακτηριστικά</td>";

                if($info_count > 0){
                    $counter = 0; 
                    while($row_8 = mysql_fetch_array($sql_5)){
                        if( $counter == 0){
                            $info_id  = $row_8["id"];
                            $info_add = $row_8["add_info"];
                            $info_table .="<td><input type='text' name='information[]' id='information' size='84' value='$info_add'/></td></tr>";
                            $counter = $counter + 1;
                        }else{

                            $info_id  = $row_8["id"];
                            $info_add = $row_8["add_info"];
                            $info_table .="<tr>
                                            <td></td>
                                            <td>
                                                <input type='text' name='information[]' id='information' size='84' value='$info_add'/>
                                            </td>
                                        </tr>";

                        }/* End Else If */

                    }/*End While*/
                }


                $date_added=strftime("%b %d, %Y", strtotime($row["date_added"]));
            }

        }else{

            echo "This product doesn't exist anymore."; 

        }

    }

?>

<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=<?php echo $info_count ?>;
    function addRow(tableID) {

        var table = document.getElementById(tableID);

        var rowCount = 7+(i-1);
        var row = table.insertRow(rowCount);

        var cell1 = row.insertCell(0);

        var cell2 = row.insertCell(1);
        cell2.innerHTML = table.rows[6].cells[1].innerHTML;
        document.getElementById(information[i-1]).value="";
        i++;
    }

    function deleteRow(tableID) {
        if(i>=2){
            var table = document.getElementById(tableID);
            table.deleteRow(i+5);
            i--;
        }
    }

</SCRIPT>

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

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

  <div id="pageContent">

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

            <h2>Επεξεργασία Προϊόντος</h2>

            <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="100" value="<?php echo $product_name ?>" />
                        </label>
                    </td>
                </tr>

                <?php echo $condition;?>

                <tr>
                    <td>Τιμή</td>
                    <td>
                        <label>
                            <input name="price" type="text" id="price" size="20" value="<?php echo $price ?>" />
                        </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"><?php echo $details ?> </textarea>
                        </label>
                    </td>
                </tr>

                <?php echo $info_table ?>

                <tr>
                    <td></td>
                    <td>
                    <INPUT type="button" value="Προσθήκη" onClick="addRow('dataTable')"/>
                    <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 name="thisID" type="hidden" value="<?php echo $targetID ?>" />
                            <input type="submit" name="button" id="button" value="Αποθήκευση" />
                        </label>
                    </td>
                </tr>

            </table>

        </form>



  </div>

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

</div>
  • 写回答

1条回答 默认 最新

  • dongsun5330 2012-03-07 19:40
    关注

    JavaScript:

    function insertRow(position) {
        var table = document.getElementById("theTable");
        var tr = table.insertRow(position);
        var td = tr.insertCell(0);
        tr.insertCell(1);
        tr.insertCell(2);
        var textNode = document.createTextNode("\u00A0");
        td.appendChild(textNode);
    }
    

    jQuery:

    function insertRow() {
        $("#theTable").append("<tr><td>&nbsp;</td><td></td><td></td></tr>");
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 Java-Oj-桌布的计算
  • ¥15 请问如何在openpcdet上对KITTI数据集的测试集进行结果评估?
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路