javascript - Display the return of an ajax in the corresponding row -
i needed add runtime rows on table , send data database. managed of stuck on 2 parts of program.
i using ajax check available quantity drop down, wish display in corresponding row, cant, have been able alert correct value far).
the php post sending me 1 parameter less available on form if fill row, send me correct amount of parameters if add rows , don’t fill information. ( ie if add 3 blank rows , click on submit submits 4 values, -> because 1 row hidden if fill of 3 rows in screen sends parameter 3 post $name)
function getspqty(strurl) { var xmlhttp; console.log(this) if (window.xmlhttprequest) { // code ie7+, firefox, chrome, opera, safari xmlhttp = new xmlhttprequest(); } else { // code ie6, ie5 xmlhttp = new activexobject("microsoft.xmlhttp"); } xmlhttp.onreadystatechange = function() { if (xmlhttp.readystate == 4) { if (xmlhttp.status == 200) { alert(xmlhttp.responsetext); document.getelementbyid('sp').value = xmlhttp.responsetext; } else { alert("there problem while using xmlhttp:\n" + xmlhttp.statustext); } } } xmlhttp.open("get", strurl, true); xmlhttp.send(null); }
<?php include_once( "dbconn-sp.php"); $sqlsp="select * dbo.sparepartmanagement" ; $stmtsp=s qlsrv_query($connigms, $sqlsp); ?> <html> <head> <script src="js/jquery1.7.js"></script> <script> $(document).ready(function() { var id = 0; // add button functionality $("table.dynatable button.add").click(function() { id++; var master = $(this).parents("table.dynatable"); // new row based on prototype row var prot = master.find(".prototype").clone(); prot.attr("class", "") prot.find(".id").attr("value", id); master.find("tbody").append(prot); }); // remove button functionality $("table.dynatable button.remove").live("click", function() { $(this).parents("tr").remove(); }); }); </script> <style> .dynatable { border: solid 1px #000; border-collapse: collapse; } .dynatable th, .dynatable td { border: solid 1px #000; padding: 2px 10px; width: 170px; text-align: center; } .dynatable .prototype { display: none; } </style> <script type="text/javascript" language="javascript1.2" src="spqty.js"></script> </head> <body> <form action="test.php" method="post" name="frm"> <table width="100%" class="dynatable"> <thead> <tr> <th width="144">id</th> <th width="144">spareparts</th> <th width="144">quanitity</th> <th width="144">issued quantity</th> <th width="140"> <button type="button" class="add">add</button> </th> </tr> </thead> <tbody> <tr class="prototype"> <td> <input type="text" name="id[]" value="0" class="id" /> </td> <td> <select style="width:200px" name="name[]" id="sp" onchange="getspqty('spname.php?spareparts='+this.value)"> <option value="error">select spare parts</option> <?php while($viewallsp=s qlsrv_fetch_array($stmtsp,sqlsrv_fetch_assoc)) { echo "<option value='$viewallsp[generalitemdescription]'>$viewallsp[generalitemdescription]</option>\n"; }; ?> </select> </td> <td> <input type="text" name="col4[]" value="" /> </td> <td> <input type="text" name="col3[]" value="" /> </td> <td> <button class="remove">remove</button> </td> </tr> </table> <input type="submit" value='send' /> </form> </body> </html>
this spname.php
include_once("dbconn-sp.php"); $spareparts=$_request['spareparts']; $tsqls = "select initialqty sparepartmanagement generalitemdescription ='$spareparts'"; $params = array(); $options = array("scrollable" => sqlsrv_cursor_keyset ); $stmt = sqlsrv_query($connigms, $tsqls , $params, $options); $row_count = sqlsrv_num_rows($stmt); while( $row = sqlsrv_fetch_array( $stmt, sqlsrv_fetch_assoc) ) { $test = $row['initialqty']; } echo $test; ?>
here test.php
<?php error_reporting(0); include_once("dbconn-sp.php"); $qty = $_post["col4"]; $issuedqty = $_post["col3"]; $name= $_post["name"]; //step 3: insert statment print_r($name); ($i = 1; $i < sizeof($qty); $i++) { $j=$i-1; $insert = "insert spareparttransaction (spdesc,initialqty,qtyissued) values ". " ('$name[$j]','$qty[$i]','$issuedqty[$i]')"; $stmt = sqlsrv_query($connigms,$insert); } sqlsrv_close($connigms); ?>
unfortunately not have enough reputation post image here here link actual display on screen http://prntscr.com/749n4n
what want instead of having alert -> alert(xmlhttp.responsetext); wish value displayed on cell beside drop down. appreciated.
Comments
Post a Comment