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

时间:2016-08-23 13:21:35

标签: javascript php jquery html css

这里我所做的是通过for循环和mannualy创建输入字段,如果我们想要添加一个字段然后我们可以通过追加功能添加它,但我不知道如何为每个产品提供不同的名称&价格领域。如何从该追加字段提交输入字段值,,这里是代码..

<?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 个答案:

答案 0 :(得分:0)

如果您将名称作为数组item[]传递,您将获得个人值