Sunday 15 September 2013

mysql - How to get form input array into PHP array but not empty field -



mysql - How to get form input array into PHP array but not empty field -

i have form 1 below posted same page, , user can dynamically add together more textbox jquery.

<h1> add together order </h1> <form method="post" action=""> <p id="add_field"><a href="#"><span> click add together </span></a></p> <div id="container"> <input type="text" class="pid" id="' + counter + '" name="product[]" /> <input type="text" class="qid" id="' + counter + '" name="quantity[]" /><br /> </div><br /> <input type= "submit" name="submit_order" value="submit"> </form>

everything work fine if add together more textbox , leave textbox empty going submit. problem don't want submit empty textboxes in table , want server side solution this.

here total code php

<body> <?php if ( isset($_post['submit_order']) ) { if ( !empty($_post['product']) && !empty($_post['quantity']) ) { $product = ($_post['product']); $quantity = ($_post['quantity']); foreach ($product $id => $value) { $products = ($product[$id]); $quantitys = ($quantity[$id]); $query = mysql_query("insert myorders (product,quantity) values ('$products','$quantitys')", $connection); } } echo "<i><h2><stront>" . count($_post['product']) . "</strong> entry added </h2></i>"; mysql_close(); } ?> <?php if (!isset($_post['submit_order'])) { ?> <h1> add together order </h1> <form method="post" action=""> <p id="add_field"><a href="#"><span> click add together </span></a></p> <div id="container"> <input type="text" class="pid" id="' + counter + '" name="product[]" /> <input type="text" class="qid" id="' + counter + '" name="quantity[]" /><br /> </div><br /> <input type= "submit" name="submit_order" value="submit"> </form> <?php } ?> </body>

something should work:

foreach ($_post[product] $key => $value): if (empty($value)): unset($_post[product][$key]); endif; endforeach; foreach ($_post[quantity] $key => $value): if (empty($value)): unset($_post[quantity][$key]); endif; endforeach;

php mysql

No comments:

Post a Comment