doutuoben6908 2016-08-23 13:21
浏览 90

想要通过追加按钮创建字段时提交表单元素

Here what i did is creating Input fields by for loop and mannualy if we want to add a field then we can add that by append function , but i don't get how to provide different name for each product & price fields. How can i submit the input field values from that append fields,,, here is the code..

<?php
mysql_connect('localhost','root','');
mysql_select_db('filters');
$select_no="";
?>
<!DOCTYPE html>
<html>
<head>
    <title> filters program</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
            $("#btn1").click(function(){
                $("ol").append("<li><input type='text' style='width:300px; height:30px; border-radius:10px;' name='item' placeholder='product..' value=''> <input type='text' style='width:100px;height:30px; border-radius:10px;' name='price' placeholder='price..'></li><br>");
            });
        });
    </script>
</head>
<body>
    <br>
    <?php
        //if condition for saving data in database table name filters and table name products....
        if(isset($_POST['name_s'])){
            session_start();
            $x = $_SESSION["number_of_x"];
            for ($a=1;$a<=$x;$a++){
            $product=$_POST["items".$a];
            $price=$_POST["price".$a];
            echo "$a.) Product name is $product & ";
            echo "its price is Rs. $price was ";
            $sql= "INSERT INTO products (product, price) VALUES('$product','$price')";
            $res=mysql_query($sql);
            if ($res) {
            echo "submitted Successfully in database !!<br><hr><br />";
        }else{
            echo "Not submitted because ".mysql_error();}
        }
        }else{
    ?> 
    <form method="POST" action="">
        Select No. OF Fields <input type="number" style="width:100px;height:25px;border-radius:10px;" id="selct_no" name="select_no" value=""> To <input type="submit" name="create" id="create_fields" value="create" style="width:70px;border-radius:10px;"><br><br><br>
    </form>
        <button id='btn1' style="width:170px;border-radius:10px;">Add Input Field Mannualy</button>
    <?php
    }
    //IF CONDITION FOR CREATIG ITEMS FIELD depend on user need....
    if(isset($_POST['create'])){
    $select_no=$_POST['select_no'];
    // Start the session
    session_start();
    $_SESSION["number_of_x"] = $_POST['select_no'];
    // loop for generating given no. of fields... 
    ?>
    <form method="POST" action="">
            <ol>
            <?php
                for ($i=0; $i < $select_no ; $i++) { 
                    global $x;
                    $x = $i+1;
                    echo "<div  style='width:475px'>";
                    echo "<li><input type='text' style='width:300px; height:30px; border-   radius:10px;' placeholder='Enter Items' id='id_item' name='items".$x."' value=''> <input type='number' style='width:100px;height:30px; border-radius:10px;' placeholder='Price' id='id_price' name='price".$x."' value=''> <br/><br/> ";
                }       
            ?> 
        </ol>
        </div>
        <div style='width:450px'>
            <center>
                <input type="submit" name='name_s'  style='width:300px;height:35px;border- radius:10px'     id='submit_cat'></input>
            </center>  
        </div>
    </form>
    <?php } ?>
</body>
</html>
  • 写回答

1条回答 默认 最新

  • dpba63888 2016-08-23 13:33
    关注

    If you'll pass names as an array like this item[] you will get values individual

    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看