Saturday 15 March 2014

php - when try to add more field and select then its conflict first row -



php - when try to add more field and select then its conflict first row -

when trying select alternative value form row 1 there's no problem if add together more , select optional value in sec row getting conflict first. every time when select optional value first row conflict want first row alter while changing first select alternative . sec row select alter sec row values.

index.php

<?php if(!empty($_post["save"])) { $conn = mysql_connect("localhost","root",""); mysql_select_db("ajaxphp",$conn); $itemcount = count($_post["item_name"]); $itemvalues = 0; $query = "insert item (item_name,item_price) values "; $queryvalue = ""; for($i=0;$i<$itemcount;$i++) { if(!empty($_post["item_name"][$i]) || !empty($_post["item_price"][$i])) { $itemvalues++; if($queryvalue!="") { $queryvalue .= ","; } $queryvalue .= "('" . $_post["item_name"][$i] . "', '" . $_post["item_price"][$i] . "')"; } } $sql = $query.$queryvalue; if($itemvalues!=0) { $result = mysql_query($sql); if(!empty($result)) $message = "added successfully."; } } ?> <html> <head> <title>php jquery dynamic textbox</title> <link href="style.css" rel="stylesheet" type="text/css" /> <script src="http://code.jquery.com/jquery-2.1.1.js"></script> <script> function addmore() { $("<div>").load("input.php", function() { $("#product").append($(this).html()); }); } function deleterow() { $('div.product-item').each(function(index, item){ jquery(':checkbox', this).each(function () { if ($(this).is(':checked')) { $(item).remove(); } }); }); } </script> </head> <body> <form name="frmproduct" method="post" action=""> <div id="outer"> <div id="header"> <div class="float-left">&nbsp;</div> <div class="float-left col-heading">item name</div> <div class="float-left col-heading">item price</div> </div> <div id="product"> <?php require_once("input.php") ?> </div> <div class="btn-action float-clear"> <input type="button" name="add_item" value="add more" onclick="addmore();" /> <input type="button" name="del_item" value="delete" onclick="deleterow();" /> <span class="success"><?php if(isset($message)) { echo $message; }?></span> </div> <div class="footer"> <input type="submit" name="save" value="save" /> </div> </div> </form> </body> </html>

input.php

<script type="text/javascript" src="js/jquery.min.js"></script> <script> function salesdetail(item_index) { alert(item_index); $.ajax({ url: 'getsaleinfo.php', type: 'post', data: {item_index:item_index},` success:function(result){ alert(result); $('#div1').html(result); } }); } </script> <div class="product-item float-clear" style="clear:both;"> <div class="float-left"><input type="checkbox" name="item_index[]" /></div> <div class="float-left"><select name="item_index" id="item_index" class="required input-small" onchange="salesdetail(this.value);" > <option>select</option> <?php $conn = mysql_connect("localhost","root",""); mysql_select_db("ajaxphp",$conn); $result = mysql_query("select * item"); while($row=mysql_fetch_assoc($result)) { echo "<option>".$row['item_name']."</option>"; } ?> </select> </div> <div class="float-left" id="div1"><input type="text" id="unit_price" name="unit_price" /></div> </div>

and getsaleinfo.php

<?php $conn = mysql_connect("localhost","root",""); mysql_select_db("ajaxphp",$conn); $supplier= $_post['item_index']; $sql = "select * item item_name='$supplier'"; $rs = mysql_query($sql); ?> <?php if($row = mysql_fetch_array($rs)) { ?> <div class="float-left"> <!--<label id="unit" ></label>--> <input type="text" name="unit_price" id="unit_price" class="input-mini" value="<?php echo $row['item_price'];?>" > </div> <?php } ?>

database

create table if not exists `item` ( `id` int(11) not null auto_increment, `item_name` varchar(255) not null, `item_price` int(11) not null, primary key (`id`) ) engine=innodb default charset=latin1 auto_increment=3 ; insert `item` (`id`, `item_name`, `item_price`) values (1, 'hello', 21), (2, 'hi', 22);

/*------------------------index.php--------------------------*/ <?php if (!empty($_post["save"])) { $conn = mysql_connect("localhost", "root", ""); mysql_select_db("ajaxphp", $conn); $itemcount = count($_post["item_index"]); $itemvalues = 0; $query = "insert item (item_name,item_price) values "; $queryvalue = ""; ($i = 0; $i < $itemcount; $i++) { if (!empty($_post["item_index"][$i]) || !empty($_post["unit_price"][$i])) { $itemvalues++; if ($queryvalue != "") { $queryvalue .= ","; } $queryvalue .= "('" . $_post["item_index"][$i] . "', '" . $_post["unit_price"][$i] . "')"; } } $sql = $query . $queryvalue; if ($itemvalues != 0) { $result = mysql_query($sql); if (!empty($result)) $message = "added successfully."; } } ?> <html> <head> <title>php jquery dynamic textbox</title> <link href="style.css" rel="stylesheet" type="text/css" /> <script src="http://code.jquery.com/jquery-2.1.1.js"></script> <script> var cnt = 1; function addmore() { $("<div>").load("input.php?cnt=" + cnt, function() { $("#product").append($(this).html()); cnt++; }); } function deleterow() { $('div.product-item').each(function(index, item) { jquery(':checkbox', this).each(function() { if ($(this).is(':checked')) { $(item).remove(); } }); }); } </script> </head> <body> <form name="frmproduct" method="post" action=""> <div id="outer"> <div id="header"> <div class="float-left">&nbsp;</div> <div class="float-left col-heading">item name</div> <div class="float-left col-heading">item price</div> </div> <div id="product"> <?php require_once("input.php") ?> </div> <div class="btn-action float-clear"> <input type="button" name="add_item" value="add more" onclick="addmore();" /> <input type="button" name="del_item" value="delete" onclick="deleterow();" /> <span class="success"><?php if (isset($message)) { echo $message; } ?></span> </div> <div class="footer"> <input type="submit" name="save" value="save" /> </div> </div> </form> </body> </html>

input.php

/*------------------------input.php--------------------------*/ <script type="text/javascript" src="js/jquery.min.js"></script> <script> function salesdetail(item_index, item_id) { alert(item_index); $.ajax({ url: 'getsaleinfo.php', type: 'post', data: {item_index: item_index, item_id: item_id}, success: function(result) { alert(result); $('#div_' + item_id).html(result); } }); } </script> <?php $_request['cnt'] = (isset($_request['cnt'])) ? $_request['cnt'] : 0; ?> <div class="product-item float-clear" style="clear:both;"> <div class="float-left"><input type="checkbox" name="item_ind[]" id="item_ind_<?php echo $_request['cnt']; ?>" /></div> <div class="float-left"><select name="item_index[]" id="item_index_<?php echo $_request['cnt']; ?>" class="required input-small" onchange="salesdetail(this.value, '<?php echo $_request['cnt']; ?>');" > <option>select</option> <?php $conn = mysql_connect("localhost", "root", ""); mysql_select_db("ajaxphp", $conn); $result = mysql_query("select * item"); while ($row = mysql_fetch_assoc($result)) { echo "<option>" . $row['item_name'] . "</option>"; } ?> </select></div> <div class="float-left" id="div_<?php echo $_request['cnt']; ?>"><input type="text" id="unit_price_<?php echo $_request['cnt']; ?>" name="unit_price[]" /></div> </div>

getsaleinfo.php

/*------------------------getsaleinfo.php--------------------------*/ <?php $conn = mysql_connect("localhost", "root", ""); mysql_select_db("ajaxphp", $conn); $supplier = $_post['item_index']; $sql = "select * item item_name='$supplier'"; $rs = mysql_query($sql); ?> <?php $_request['item_id'] = (isset($_request['item_id'])) ? $_request['item_id'] : ''; if ($row = mysql_fetch_array($rs)) { ?> <div class="float-left"> <input type="text" name="unit_price[]" id="unit_price_<?php echo $_request['item_id']; ?>" class="input-mini" value="<?php echo $row['item_price']; ?>" > </div> <?php } ?>

php mysql

No comments:

Post a Comment