bambinou1980 Posted August 5, 2015 Share Posted August 5, 2015 Hi All, I am still on the learning curbe with PHP, I have created a form that generated fields based on the number of products in the database. My problem now is to work out how to submit the $_POST data of each dynimically generated fields, I just cannot work out how I will manage to find the fields "name" in advance as the form data can grow from one product up to let's say 10. Could you please give me some hints on the type of logic I should use as I really do not know(due to my beginner level) where to start on dynimically generated fields. Thank you! Ben <form role="form" method="post" action="index.php"> <div class="form-group"> <label for="name_id"> Customer Name * </label> <input type="text" name="customer_name" placeholder="Customer Name" class="form-control" id="name_id"/> </div> <div class="form-group"> <label for="due_date_id"> Due Date * </label> <input type="text" name="due_date" placeholder="" class="form-control" id="duedate_id"/> </div> <!--Product 1--> <?php $query_field1 = "SELECT * FROM products ORDER BY name desc"; $result_field1 = mysqli_query($connection, $query_field1) or die (mysqli_error()); $number = 0; while($row_field1 = mysqli_fetch_array($result_field1)){ $name = $row_field1['name']; $price1 = $row_field1['price1']; $price2 = $row_field1['price2']; $price3 = $row_field1['price3']; ?> <div class="form-inline well"> <label for="product1_id">Choose Product <?php echo ++$number; ?></label> <div class="form-group"> <select name="product1" id="product1" class="form-control"> <option value="<?php echo $name; ?>" /><?php echo $name; ?></option>"; </select> <label class="btn btn-default"> <input type="radio" id="price1_id" name="action" value="<?php echo $price1;?>" /> €<?php echo $price1;?> </label> <?php if(($price2) != null) { echo "<label class='btn btn-default'> <input type='radio' id='price2_id' name='action' value='{$price2}'/> € {$price2} </label>"; } ?> <?php if(($price3) != null) { echo "<label class='btn btn-default'> <input type='radio' id='price3_id' name='action' value='{$price3}'/> € {$price3} </label>"; } ?> <label for="product1_id">Qty</label> <input name="quantity1" id="quantity1" type="text" class="form-control" maxlength="4" size="4"><!--disabled="disabled"--> </div> </div> <?php } ?> <div class="form-group"> <label for="address_id"> Address * </label> <textarea type="text" name="address" placeholder="Company's Address" rows="4" cols="50" class="form-control" id="address_id" value=""></textarea> </div> <div class="form-group"> <label for="phone_id"> Phone </label> <input type="text" name="phone" placeholder="Customer Phone" class="form-control" id="phone_id" value="" /> </div> <div class="form-group"> <label for="email_id"> Email </label> <input type="email" name="email" placeholder="Customer Email" class="form-control" id="email_id" value="" /> </div> <div class="form-group"> <label for="vat_id"> V.A.T Number(ie:MT20343324) </label> <input type="text" name="vat" placeholder="V.A.T Number" class="form-control" id="vat_id" value="" /> </div> <button type="submit" name="submit" class="btn btn-default"> Create Order </button> </div> </form> Quote Link to comment https://forums.phpfreaks.com/topic/297645-submit-_post-with-dynamically-generated-fields/ Share on other sites More sharing options...
Barand Posted August 5, 2015 Share Posted August 5, 2015 You are listing all the products. Each one has dropdown menu with only a single option. What is the point of that? Quote Link to comment https://forums.phpfreaks.com/topic/297645-submit-_post-with-dynamically-generated-fields/#findComment-1518076 Share on other sites More sharing options...
cyberRobot Posted August 5, 2015 Share Posted August 5, 2015 You could use the array format when naming the form fields. Here is a quick example: <?php if($_SERVER['REQUEST_METHOD'] == 'POST') { foreach($_POST['product'] as $currKey=>$currValue) { echo '<div>' . $_POST['product'][$currKey] . ' = ' . $_POST['price'][$currKey] . '</div>'; } } ?> <form method="post"> <?php for($i=1; $i<=5; $i++) { ?> <div> <label>Product <?php echo $i; ?> <input type="text" name="product[]" /></label> <label>Price <?php echo $i; ?> <input type="text" name="price[]" /></label> </div> <?php } ?> <input type="submit" /> </form> Quote Link to comment https://forums.phpfreaks.com/topic/297645-submit-_post-with-dynamically-generated-fields/#findComment-1518077 Share on other sites More sharing options...
Barand Posted August 5, 2015 Share Posted August 5, 2015 (edited) I would go a step further than ch0cu3r and use the product's row id as the array indexes name="product[$id]" name="price[$id]" then you know which product the data relates to when processing the form Edited August 5, 2015 by Barand 1 Quote Link to comment https://forums.phpfreaks.com/topic/297645-submit-_post-with-dynamically-generated-fields/#findComment-1518081 Share on other sites More sharing options...
mac_gyver Posted August 5, 2015 Share Posted August 5, 2015 You are listing all the products. Each one has dropdown menu with only a single option. What is the point of that? this is the second time someone has questioned this. i don't think the OP has planned how his page is actually going to be used. Quote Link to comment https://forums.phpfreaks.com/topic/297645-submit-_post-with-dynamically-generated-fields/#findComment-1518092 Share on other sites More sharing options...
cyberRobot Posted August 5, 2015 Share Posted August 5, 2015 this is the second time someone has questioned this. i don't think the OP has planned how his page is actually going to be used. I would imagine that the OP was just trying to connect the product name to the corresponding price fields. Using Barand's suggestion to tie the product ID to the field names should eliminate the need for the drop-down fields. Quote Link to comment https://forums.phpfreaks.com/topic/297645-submit-_post-with-dynamically-generated-fields/#findComment-1518093 Share on other sites More sharing options...
bambinou1980 Posted August 8, 2015 Author Share Posted August 8, 2015 Yes sorry about the drodown menu, this should be a simple input field greyed out so the user cannot change it's value. The reason this was there is because I just copied and pasted the first line of code, I was going to change this later on. Quote Link to comment https://forums.phpfreaks.com/topic/297645-submit-_post-with-dynamically-generated-fields/#findComment-1518256 Share on other sites More sharing options...
mac_gyver Posted August 8, 2015 Share Posted August 8, 2015 this should be a simple input field greyed out so the user cannot change it's value it shouldn't even be that. just echo the product name next to the radio buttons it belongs to. Quote Link to comment https://forums.phpfreaks.com/topic/297645-submit-_post-with-dynamically-generated-fields/#findComment-1518263 Share on other sites More sharing options...
bambinou1980 Posted August 12, 2015 Author Share Posted August 12, 2015 (edited) Mac_Gyver, I cannot do that. Each product has 3 price(3 types of clients). So product1 = Price1[radio], Price2[radio], Price3[radio] product2 = Price1[radio], Price2[radio], Price3[radio] product3 = Price1[radio], Price2[radio], Price3[radio] And so on... I have now managed to generate all my "names" and ids dynamically but I was Looking at the first solution of CyberRobt, I am not sure this will work with 3 prices per product, or will it? I have this now: <?php $random_number = rand(999 ,99999999999 ); ?> <form role="form" method="post" action="index.php"> <div class="form-group"><label for="order_id">Order ID</label><input type="text" name="cust_order_id" class="form-control" value="<?php echo "FP" .$random_number; ?>" readonly="readonly" /></div> <div class="form-group"><label for="name_id">Company *</label> <select name="cust_company" class="form-control" id="cust_company_id"> <?php $query_field1 = "SELECT * FROM customers ORDER BY cust_company desc"; $result_field1 = mysqli_query($connection, $query_field1) or die (mysqli_error()); $number1 = 0; $number2 = 0; $number3 = 0; while($row_field1 = mysqli_fetch_array($result_field1)){ $cust_name = htmlspecialchars($row_field1['cust_name']); $cust_surname = htmlspecialchars($row_field1['cust_surname']); $cust_company = htmlspecialchars($row_field1['cust_company']); $cust_address = htmlspecialchars($row_field1['cust_address']); $cust_phone = htmlspecialchars($row_field1['cust_phone']); $cust_email = htmlspecialchars($row_field1['cust_email']); $cust_vat = htmlspecialchars($row_field1['cust_vat']);?> <option value="<?php echo $cust_company; ?>" data-name="<?php echo $cust_name; ?>" data-surname="<?php echo $cust_surname; ?>" data-address="<?php echo $cust_address; ?>" data-phone="<?php echo $cust_phone; ?>" data-email="<?php echo $cust_email; ?>" data-vat="<?php echo $cust_vat; ?>"><?php echo $cust_company; ?></option> <?php } ?> </select> </div> <div class="form-group"><label for="address_id">Address *</label><textarea name="cust_address" placeholder="Company's Address" rows="4" cols="50" class="form-control" readonly="readonly" ></textarea></div> <div class="form-group"><label for="name_id">Name *</label><input type="text" name="cust_name" placeholder="Customer Name" class="form-control" value="" readonly="readonly" /></div> <div class="form-group"><label for="surname_id">Surname *</label><input type="text" name="cust_surname" placeholder="Customer Surname" class="form-control" value="" readonly="readonly" /></div> <div class="form-group"><label for="phone_id">Phone</label><input type="text" name="cust_phone" placeholder="Customer Phone" class="form-control" value="" readonly="readonly" /></div> <div class="form-group"><label for="email_id">Email</label><input type="email" name="cust_email" placeholder="Customer Email" class="form-control" value="" readonly="readonly" /></div> <div class="form-group"><label for="vat_id">V.A.T Number(ie:MT20343324)</label><input type="text" name="cust_vat" placeholder="V.A.T Number" class="form-control" value="" readonly="readonly" /></div> <div class="form-group"><label for="due_date_id">Due Date *</label><div class="form-inline well"> <div class="input-append date" id="dp3" data-date="12-02-2012" data-date-format="dd-mm-yyyy"> <input name="due_date" class="form-control" size="25" type="text" id="dp1" value="12-02-2012"> <span class="add-on"><i class="icon-th"></i></span></div> </div> </div> <script>$('#dp1').datepicker({ format: 'dd-mm-yyyy' });</script> <?php //Query all the prices $query_field2 = "SELECT * FROM products ORDER BY name desc"; $result_field2 = mysqli_query($connection, $query_field2) or die (mysqli_error()); $number1 = 0; $number2 = 0; $number3 = 0; $id_1 = 0; $id_2 = 0; $id_3 = 0; $quantity = 0; while($row_field2 = mysqli_fetch_array($result_field2)){$name = htmlspecialchars($row_field2['name']);$price1 = htmlspecialchars($row_field2['price1']);$price2 = htmlspecialchars($row_field2['price2']);$price3 = htmlspecialchars($row_field2['price3']); ?><div class="form-inline well"><label for="product1_id">Choose Product <?php echo ++$number1; ?></label><div class="form-group"> <input name="<?php echo "product".++$number2; ?>" type="text" class="form-control" value="<?php echo $name; ?>" disabled> <!--Price 1--> <label class="btn btn-default"> <input type="radio" name="<?php echo "action".++$number3; ?>" value="<?php echo $price1;?>" id="<?php echo ++$id_1; ?>" />€ <?php echo $price1; ?> </label> <!--Price 1--> <!--Price 2--> <?php //Do not show second price if not setif(($price2) != null) {echo "<label class=\"btn btn-default\"> <input type=\"radio\" name=\"action".($number3)."\" value=\"{$price2}\" id=\"".(++$id_2)."\" /> € {$price2} </label>";} ?> <!--Price 2--> <!--Price 3--> <?php //Do not show third price if not setif(($price3) != null) {echo "<label class=\"btn btn-default\"> <input type=\"radio\" name=\"action{$number3}\" value=\"{$price3}\" id=\"".(++$id_3)."\" /> € {$price3} </label>";} ?> <!--Price 3--> <label for="product1_id">Qty</label><input name="<?php echo "quantity".++$quantity; ?>" type="text" class="form-control" maxlength="4" size="4"> </div> </div> <?php } ?> <div class="form-inline well text-center"><button type="submit" name="submit" class="btn btn-success btn-lg">Create Order</button></div></div> </form> </div> </div></div></div></div> <script> $('#cust_company_id').change(function() { selectedOption = $('option:selected', this); $('textarea[name=cust_address]').val( selectedOption.data('address') ); $('input[name=cust_name]').val( selectedOption.data('name') ); $('input[name=cust_surname]').val( selectedOption.data('surname') ); $('input[name=cust_phone]').val( selectedOption.data('phone') ); $('input[name=cust_email]').val( selectedOption.data('email') ); $('input[name=cust_vat]').val( selectedOption.data('vat') );}); </script> Edited August 12, 2015 by bambinou1980 Quote Link to comment https://forums.phpfreaks.com/topic/297645-submit-_post-with-dynamically-generated-fields/#findComment-1518579 Share on other sites More sharing options...
mac_gyver Posted August 13, 2015 Share Posted August 13, 2015 (edited) I cannot do that yes you can. between all the threads you have started for this, you have been given the parts you need, though some of the advice has been off topic because you have shown things like using a select/option menu where nothing is being selected. start off with the basics and define what you are trying to do or produce before writing any code for it. until you can create a form that submits the data you want, there's no point in all of the other markup you have in your code. see this basic example - <form method="post" action="index.php"> <?php // Query for all the products $query_field2 = "SELECT * FROM products ORDER BY name"; $result_field2 = mysqli_query($connection, $query_field2) or die (mysqli_error($connection)); // you were missing the connection link in the mysqli_error() statement while($row_field2 = mysqli_fetch_array($result_field2)){ $id = $row_field2['id']; // i guessed this was id, even though you stated your columns were product_id (and product_name) in one of the threads $name = htmlspecialchars($row_field2['name']); $price1 = htmlspecialchars($row_field2['price1']); // these should be decimal numbers only and be under your control, so no need to apply htmlspecialchars $price2 = htmlspecialchars($row_field2['price2']); $price3 = htmlspecialchars($row_field2['price3']); echo $name; echo "<input type='radio' name='price[$id]' value='$price1' checked>€ $price1"; // pre-check/select the first price if($price2 != null){ echo "<input type='radio' name='price[$id]' value='$price2'>€ $price2"; } if($price3 != null){ echo "<input type='radio' name='price[$id]' value='$price3'>€ $price3"; } echo "<label> Qty "; echo "<input name='quantity[$id]' type='text' size='4'></label><br>\n"; } ?> <button type="submit" name="submit" >Create Order</button> </form> Edited August 13, 2015 by mac_gyver Quote Link to comment https://forums.phpfreaks.com/topic/297645-submit-_post-with-dynamically-generated-fields/#findComment-1518664 Share on other sites More sharing options...
bambinou1980 Posted August 13, 2015 Author Share Posted August 13, 2015 (edited) Hi Mac_gyver, Yes now I am getting all the ids and all the php working right, where I am struggling is with the jquery. Here is my new code, remember I will then add this later on: http://jsfiddle.net/jaredwilli/tzpg4/4/ <form role="form" method="post" action="index.php"> <div class="form-group"> <label for="order_id"> Order ID </label> <input type="text" name="cust_order_id" class="form-control" value="<?php echo "FP" .$random_number; ?>" readonly="readonly" /> </div> <div class="form-group"> <label for="name_id"> Company * </label> <select name="cust_company" class="form-control" id="cust_company_id"> <?php $query_field1 = "SELECT * FROM customers ORDER BY cust_company desc"; $result_field1 = mysqli_query($connection, $query_field1) or die (mysqli_error()); while($row_field1 = mysqli_fetch_array($result_field1)){ $cust_name = htmlspecialchars($row_field1['cust_name']); $cust_surname = htmlspecialchars($row_field1['cust_surname']); $cust_company = htmlspecialchars($row_field1['cust_company']); $cust_address = htmlspecialchars($row_field1['cust_address']); $cust_phone = htmlspecialchars($row_field1['cust_phone']); $cust_email = htmlspecialchars($row_field1['cust_email']); $cust_vat = htmlspecialchars($row_field1['cust_vat']); ?> <option value="<?php echo $cust_company; ?>" data-name="<?php echo $cust_name; ?>" data-surname="<?php echo $cust_surname; ?>" data-address="<?php echo $cust_address; ?>" data-phone="<?php echo $cust_phone; ?>" data-email="<?php echo $cust_email; ?>" data-vat="<?php echo $cust_vat; ?>"><?php echo $cust_company; ?></option> <?php } ?> </select> </div> <div class="form-group"> <label for="address_id"> Address * </label> <textarea name="cust_address" placeholder="Company's Address" rows="4" cols="50" class="form-control" readonly="readonly" ></textarea> </div> <div class="form-group"> <label for="name_id"> Name * </label> <input type="text" name="cust_name" placeholder="Customer Name" class="form-control" value="" readonly="readonly" /> </div> <div class="form-group"> <label for="surname_id"> Surname * </label> <input type="text" name="cust_surname" placeholder="Customer Surname" class="form-control" value="" readonly="readonly" /> </div> <div class="form-group"> <label for="phone_id"> Phone </label> <input type="text" name="cust_phone" placeholder="Customer Phone" class="form-control" value="" readonly="readonly" /> </div> <div class="form-group"> <label for="email_id"> Email </label> <input type="email" name="cust_email" placeholder="Customer Email" class="form-control" value="" readonly="readonly" /> </div> <div class="form-group"> <label for="vat_id"> V.A.T Number(ie:MT20343324) </label> <input type="text" name="cust_vat" placeholder="V.A.T Number" class="form-control" value="" readonly="readonly" /> </div> <div class="form-group"> <label for="due_date_id"> Due Date * </label> <div class="form-inline well"> <div class="input-append date" id="dp3" data-date="12-02-2012" data-date-format="dd-mm-yyyy"> <input name="due_date" class="form-control" size="25" type="text" id="dp1" value="12-02-2012"> <span class="add-on"><i class="icon-th"></i></span> </div> </div> </div> <script> $('#dp1').datepicker({ format: 'dd-mm-yyyy' }); </script> <?php //Query all the prices $query_field2 = "SELECT * FROM products ORDER BY name desc"; $result_field2 = mysqli_query($connection, $query_field2) or die (mysqli_error()); $choose_product_id = 0; $action_id = 0; $products_id = 0; $label_id = 0; $quantity = 0; while($row_field2 = mysqli_fetch_array($result_field2)){ $name = htmlspecialchars($row_field2['name']); $price1 = $row_field2['price1']; $price2 = $row_field2['price2']; $price3 = $row_field2['price3']; ?> <div class="form-inline well"> <label for="<?php echo "label".++$label_id; ?>">Choose Product <?php echo ++$choose_product_id; ?></label> <div class="form-group"> <!--Product name Select--> <select name="<?php echo "action".++$action_id; ?>" class="form-control products" id="<?php echo "products".++$products_id; ?>"> <?php $query_field3 = "SELECT * FROM products ORDER BY name desc"; $result_field3 = mysqli_query($connection, $query_field3) or die (mysqli_error()); while($row_field3 = mysqli_fetch_array($result_field3)){ $product_name = htmlspecialchars($row_field3['name']); $product_price1 = htmlspecialchars($row_field3['price1']); $product_price2 = htmlspecialchars($row_field3['price2']); $product_price3 = htmlspecialchars($row_field3['price3']); ?> <option value="<?php echo $product_name; ?>" data-product_price1="<?php echo $product_price1; ?>" data-product_price2="<?php echo $product_price2; ?>" data-product_price3="<?php echo $product_price3; ?>"><?php echo $product_name; ?></option> <?php } ?> </select> <!--Price Select--> <select name="<?php echo "action".++$number2; ?>" class="form-control"> <option id="price1"></option> <option id="price2"></option> <option id="price3"></option> </select> <label for="product1_id">Qty</label> <input name="<?php echo "quantity".++$quantity; ?>" type="text" class="form-control" maxlength="4" size="4" id="<?php echo "quantity".$quantity; ?>"> </div> </div> <?php } ?> <div class="form-inline well text-center"> <button type="submit" name="submit" class="btn btn-success btn-lg"> Create Order </button> </div> </div> </form> </div> </div> </div> </div> </div> <!-- <script> $("#products").change(function() { $("#product_prices").load("getter.php?choice=" + $("#products").val()); }); </script> --> <script> $('#cust_company_id').change(function() { selectedOption = $('option:selected', this); $('textarea[name=cust_address]').val( selectedOption.data('address') ); $('input[name=cust_name]').val( selectedOption.data('name') ); $('input[name=cust_surname]').val( selectedOption.data('surname') ); $('input[name=cust_phone]').val( selectedOption.data('phone') ); $('input[name=cust_email]').val( selectedOption.data('email') ); $('input[name=cust_vat]').val( selectedOption.data('vat') ); }); </script> <script> $(".products").change(function() { selectedOption = $(this).find(":selected"); $(this).next().children().each(function(idx){ var num=idx+1; $(this).text( selectedOption.data('product_price'+num)); }); </script> I have the dropdowns populated correctly but the jquery code that read the first drop down menu is not pulling the second dropdown menu. I have also modified my code to make it simpler for me to understand. I have also added more dynamic ids in my code as it was full of mistakes. This is the last part of my problem, after that, all good...... I have removed the radios and added 2 dropdown menu instead. Edited August 13, 2015 by bambinou1980 Quote Link to comment https://forums.phpfreaks.com/topic/297645-submit-_post-with-dynamically-generated-fields/#findComment-1518720 Share on other sites More sharing options...
bambinou1980 Posted August 13, 2015 Author Share Posted August 13, 2015 (edited) oups posted twice Edited August 13, 2015 by bambinou1980 Quote Link to comment https://forums.phpfreaks.com/topic/297645-submit-_post-with-dynamically-generated-fields/#findComment-1518721 Share on other sites More sharing options...
bambinou1980 Posted August 13, 2015 Author Share Posted August 13, 2015 <form role="form" method="post" action="index.php"> <div class="form-group"><label for="order_id">Order ID</label><input type="text" name="cust_order_id" class="form-control" value="<?php echo "FP" .$random_number; ?>" readonly="readonly" /></div> <div class="form-group"><label for="name_id">Company *</label> <select name="cust_company" class="form-control" id="cust_company_id"> <?php $query_field1 = "SELECT * FROM customers ORDER BY cust_company desc"; $result_field1 = mysqli_query($connection, $query_field1) or die (mysqli_error()); while($row_field1 = mysqli_fetch_array($result_field1)){$cust_name = htmlspecialchars($row_field1['cust_name']);$cust_surname = htmlspecialchars($row_field1['cust_surname']);$cust_company = htmlspecialchars($row_field1['cust_company']); $cust_address = htmlspecialchars($row_field1['cust_address']); $cust_phone = htmlspecialchars($row_field1['cust_phone']); $cust_email = htmlspecialchars($row_field1['cust_email']); $cust_vat = htmlspecialchars($row_field1['cust_vat']);?> <option value="<?php echo $cust_company; ?>" data-name="<?php echo $cust_name; ?>" data-surname="<?php echo $cust_surname; ?>" data-address="<?php echo $cust_address; ?>" data-phone="<?php echo $cust_phone; ?>" data-email="<?php echo $cust_email; ?>" data-vat="<?php echo $cust_vat; ?>"><?php echo $cust_company; ?></option> <?php } ?> </select> </div> <div class="form-group"><label for="address_id">Address *</label><textarea name="cust_address" placeholder="Company's Address" rows="4" cols="50" class="form-control" readonly="readonly" ></textarea></div> <div class="form-group"><label for="name_id">Name *</label><input type="text" name="cust_name" placeholder="Customer Name" class="form-control" value="" readonly="readonly" /></div> <div class="form-group"><label for="surname_id">Surname *</label><input type="text" name="cust_surname" placeholder="Customer Surname" class="form-control" value="" readonly="readonly" /></div> <div class="form-group"><label for="phone_id">Phone</label><input type="text" name="cust_phone" placeholder="Customer Phone" class="form-control" value="" readonly="readonly" /></div> <div class="form-group"><label for="email_id">Email</label><input type="email" name="cust_email" placeholder="Customer Email" class="form-control" value="" readonly="readonly" /></div> <div class="form-group"><label for="vat_id">V.A.T Number(ie:MT20343324)</label><input type="text" name="cust_vat" placeholder="V.A.T Number" class="form-control" value="" readonly="readonly" /></div> <div class="form-group"><label for="due_date_id">Due Date *</label><div class="form-inline well"> <div class="input-append date" id="dp3" data-date="12-02-2012" data-date-format="dd-mm-yyyy"> <input name="due_date" class="form-control" size="25" type="text" id="dp1" value="12-02-2012"> <span class="add-on"><i class="icon-th"></i></span></div> </div> </div> <script>$('#dp1').datepicker({ format: 'dd-mm-yyyy' });</script> <?php //Query all the prices $query_field2 = "SELECT * FROM products ORDER BY name desc"; $result_field2 = mysqli_query($connection, $query_field2) or die (mysqli_error());$choose_product_id = 0; $action_id = 0;$products_id = 0;$label_id = 0;$quantity = 0; while($row_field2 = mysqli_fetch_array($result_field2)){$name = htmlspecialchars($row_field2['name']);$price1 = htmlspecialchars($row_field2['price1']);$price2 = htmlspecialchars($row_field2['price2']);$price3 = htmlspecialchars($row_field2['price3']); ?><div class="form-inline well"><label for="<?php echo "label".++$label_id; ?>">Choose Product <?php echo ++$choose_product_id; ?></label><div class="form-group"> <!--Product name Select--><select name="<?php echo "action".++$action_id; ?>" class="form-control products" id="<?php echo "products".++$products_id; ?>"><?php $query_field3 = "SELECT * FROM products ORDER BY name desc"; $result_field3 = mysqli_query($connection, $query_field3) or die (mysqli_error()); while($row_field3 = mysqli_fetch_array($result_field3)){$product_name = htmlspecialchars($row_field3['name']);$product_price1 = htmlspecialchars($row_field3['price1']);$product_price2 = htmlspecialchars($row_field3['price2']); $product_price3 = htmlspecialchars($row_field3['price3']);?><option value="<?php echo $product_name; ?>" data-product_price1="<?php echo $product_price1; ?>" data-product_price2="<?php echo $product_price2; ?>" data-product_price3="<?php echo $product_price3; ?>"><?php echo $product_name; ?></option> <?php } ?> </select> <!--Price Select--> <select name="<?php echo "action".++$number2; ?>" class="form-control"> <option id="price1"></option><option id="price2"></option><option id="price3"></option> </select> <label for="product1_id">Qty</label><input name="<?php echo "quantity".++$quantity; ?>" type="text" class="form-control" maxlength="4" size="4" id="<?php echo "quantity".$quantity; ?>"> </div> </div> <?php } ?> <div class="form-inline well text-center"><button type="submit" name="submit" class="btn btn-success btn-lg">Create Order</button></div></div> </form> </div> </div></div></div></div><!--<script> $("#products").change(function() { $("#product_prices").load("getter.php?choice=" + $("#products").val());});</script> --><script> $('#cust_company_id').change(function() { selectedOption = $('option:selected', this); $('textarea[name=cust_address]').val( selectedOption.data('address') );$('input[name=cust_name]').val( selectedOption.data('name') );$('input[name=cust_surname]').val( selectedOption.data('surname') ); $('input[name=cust_phone]').val( selectedOption.data('phone') ); $('input[name=cust_email]').val( selectedOption.data('email') ); $('input[name=cust_vat]').val( selectedOption.data('vat') );}); </script> <script> $(".products").change(function() { selectedOption = $(this).find(":selected"); $(this).next().children().each(function(idx){ var num=idx+1; $(this).text( selectedOption.data('product_price'+num)); }); </script> Quote Link to comment https://forums.phpfreaks.com/topic/297645-submit-_post-with-dynamically-generated-fields/#findComment-1518722 Share on other sites More sharing options...
mac_gyver Posted August 14, 2015 Share Posted August 14, 2015 your current code should be using an array for the form field name="...." attributes. this will allow you to process the submitted form data using php array functions. this is even more important if you plan on having an 'add' javascrpt/jquery button that dynamically adds more rows of data. using sequentially numbered field names will mean that you have to find and keep track of the number of fields in the javascript/jquery so that you can number the dynamic ones properly. by using an array for the field name, you don't need to do anything extra in the javascript/jquery for the dynamically added rows of data and all the form fields, the static ones and the dynamically added ones, will all be part of the same submitted data and will all be processed by the php code the same. you are also querying for all the product rows, then querying for all the rows again inside of the loop that's looping over the result from the first query. that is killing your database server with queries. even if your current method will result in a workable solution, once you query for all the rows in your database table, one time, just reuse that result set. the easiest and quickest way of reusing a result set multiple times would be to store all the rows in a php array or use a fetch_all statement if the database library you are using supports it. beyond those, i'm not sure why your code doesn't work, and since you have changed your concept multiple times, i'm not sure any of the previous helpers want to take the time to figure out what you are doing in this iteration of your design. Quote Link to comment https://forums.phpfreaks.com/topic/297645-submit-_post-with-dynamically-generated-fields/#findComment-1518793 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.