0

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>
T0xicCode
  • 4,583
  • 2
  • 37
  • 50
A. Sharma
  • 7
  • 3
  • 1
    Off-topic, but please indent your code. This is a bit hard to follow. https://en.wikipedia.org/wiki/Indent_style – WillardSolutions Aug 23 '16 at 13:25
  • Please dont use [the `mysql_` database extension](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php), it is deprecated (gone for ever in PHP7) Specially if you are just learning PHP, spend your energies learning the `PDO` database extensions. [Start here](http://php.net/manual/en/book.pdo.php) its really pretty easy – RiggsFolly Aug 23 '16 at 13:25
  • Some sensible code indentation would be a good idea. It help us read the code and more importantly it will help **you debug your code** [Take a quick look at a coding standard](http://www.php-fig.org/psr/psr-2/) for your own benefit. You may be asked to amend this code in a few weeks/months and you will thank me in the end. – RiggsFolly Aug 23 '16 at 13:26
  • Your script is at risk of [SQL Injection Attack](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) Have a look at what happened to [Little Bobby Tables](http://bobby-tables.com/) Even [if you are escaping inputs, its not safe!](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) Use [prepared parameterized statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) – RiggsFolly Aug 23 '16 at 13:26
  • Make the `name="price"` attribute return an array of prices by calling the field `name="price[]"` and `name='item[]'` You must do that also to the static html created in the PHP as well. Then you dont need to invent names in a loop – RiggsFolly Aug 23 '16 at 13:28

1 Answers1

0

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

hungerstar
  • 21,206
  • 6
  • 50
  • 59
khaliq
  • 98
  • 9