php - Cant Run javascript in AJAX file -
i have php file , sending variables php ajax file , running great , when run javascript in ajax file nothing appear alerting msg , nothing appear , there conflict between ajax , javascript ? in ajax file
<script type="text/javascript" src="js/library.js"></script> <script type="text/javascript" src="js/jquery.js"></script> <?php session_start(); include("mysqlconnection.php"); if (!isset($_session['username'])) { echo "<h2 align='center'>warning: not signed in </h2>"; die ("<script type = 'text/javascript' language = 'javascript'>window.settimeout(\"location.href='index.php'\", 1000); </script>"); } $uid=$_session['uid']; $code = $_get['user']; $qty = $_get['qty']; $custid = $_get['custid']; $warehouseid = $_get['warehouseid']; if ($code==''){ $sql1 = "select * itemscopy uid='$uid'"; $result1=mysql_query($sql1,$con); if(!$result1)die("couldn't execute".mysql_error()); ?> <table border="0" width="100%" align="center"> <thead> <tr> <th>code</th> <th>code 2</th> <th>description</th> <th>discount%</th> <th>discount amount</th> <th>quantity</th> <th>price</th> <th>amount</th> <th>after discount</th> <th>warehouse</th> </tr> </thead> <?php for($counter=0;$row1=mysql_fetch_assoc($result1);$counter++){ $wid=$row1['warehouseid']; $selwarehouse="select * warehouse id='$wid'"; $runwarehouse=mysql_query($selwarehouse,$con); if(!$runwarehouse)die("error"); $rowz=mysql_fetch_assoc($runwarehouse); if($counter%2==0){ echo ("<tr class='light'>"); echo ("<th>".$row1['code']."</th>"); echo ("<th>".$row1['code2']."</th>"); echo ("<th>".$row1['description']."</th>"); echo ("<th>".$row1['discountper']."</th>"); echo ("<th>".$row1['discountamount']."</th>"); echo ("<th>".$row1['quantity']."</th>"); echo ("<th>".$row1['price']."</th>"); echo ("<th>".$row1['amount']."</th>"); echo ("<th>".$row1['totaldiscount']."</th>"); echo ("<th>".$rowz['name']."</th>"); echo ("<th><a href='edit.php?id=".$row1['id']."'>edit</a></th>"); echo ("</tr>"); }else{ echo ("<tr class='dark'>"); echo ("<th>".$row1['code']."</th>"); echo ("<th>".$row1['code2']."</th>"); echo ("<th>".$row1['description']."</th>"); echo ("<th>".$row1['discountper']."</th>"); echo ("<th>".$row1['discountamount']."</th>"); echo ("<th>".$row1['quantity']."</th>"); echo ("<th>".$row1['price']."</th>"); echo ("<th>".$row1['amount']."</th>"); echo ("<th>".$row1['totaldiscount']."</th>"); echo ("<th>".$rowz['name']."</th>"); echo ("<th><a href='edit.php?id=".$row1['id']."'>edit</a></th>"); echo ("</tr>"); } } echo "</table>"; ?> </div> </body> </html> <?php die(''); } ?> <head> </head> <?php if($warehouseid==''){ die("<h2 align='center'>please choose warehouse</h2>"); } $x=(explode('-',$code)); $code1=$x[0]; $codeid=$x[1]; $code2=$x[2]; $selqty="select * inventory itemid='$codeid' , warehouseid='$warehouseid'"; $runselqty=mysql_query($selqty,$con); $rowinventory=mysql_fetch_assoc($runselqty); if(!$runselqty)die("error"); if(mysql_num_rows($runselqty)==0){ die("<h1 align='center'>this item not in inventory</h1>"); } else{ if($rowinventory['qty']<0){ ?> <script type='text/javascript'> function check(){ var r = confirm("press button!"); if(r) { var urllink = 'http://www.example.com/warehouse/record.php?codeid=<?php echo $codeid;?>&qty=<?php echo $qty;?>&custid=<?php echo $custid;?>&warehouseid=<?php echo $warehouseid;?>'; $.ajax({ type: 'get', url: urllink, success: function(data) { if(data == 'success') { alert('123'); } }, error: function(data) { console.log(data); } }); } else { return false; } } check(); alert('123'); </script> <?php // die("<h1 align='center'>the quantity of item negative want continue ?</h1>"); } else if($rowinventory['qty']-$qty>=0){ // die("<h1 align='center'>the quantity enough in inventory can continue</h1>"); } else if($rowinventory['qty']-$qty<0){ // die("<h1 align='center'>the available quantity in inventory less quantity select</h1>"); } } $sqlx = "select * items id='$codeid'"; $resultx=mysql_query($sqlx,$con); if(!$resultx)die("couldn't execute".mysql_error()); $row = mysql_fetch_assoc($resultx); $desc=$row['description']; $price=$row['price']; if((!isset($qty))||($qty=='')){ $insert="insert itemscopy(code,code2,description,quantity,price,amount,custid,uid,discountper,discountamount,totaldiscount,warehouseid,codeid)values('$code1','$code2','$desc','1','$price','$price','$custid','$uid','0','0','$price','$warehouseid','$codeid')"; $r=mysql_query($insert,$con); if(!$r)die("error".mysql_error()); } else{ $insert="insert itemscopy(code,code2,description,quantity,price,amount,custid,uid,discountper,discountamount,totaldiscount,warehouseid,codeid)values('$code1','$code2','$desc','$qty','$price','$price'*'$qty','$custid','$uid','0','0','$price'*'$qty','$warehouseid','$codeid')"; $r=mysql_query($insert,$con); if(!$r)die("error".mysql_error()); } $sql1 = "select * itemscopy uid='$uid'"; $result1=mysql_query($sql1,$con); if(!$result1)die("couldn't execute".mysql_error()); ?> <table border="0" width="100%" align="center"> <thead> <tr> <th>code</th> <th>code 2</th> <th>description</th> <th>discount%</th> <th>discount amount</th> <th>quantity</th> <th>price</th> <th>amount</th> <th>after discount</th> <th>warehouse</th> </tr> </thead> <?php for($counter=0;$row1=mysql_fetch_assoc($result1);$counter++){ $wid=$row1['warehouseid']; $selwarehouse="select * warehouse id='$wid'"; $runwarehouse=mysql_query($selwarehouse,$con); if(!$runwarehouse)die("error"); $rowz=mysql_fetch_assoc($runwarehouse); if($counter%2==0){ echo ("<tr class='light'>"); echo ("<th>".$row1['code']."</th>"); echo ("<th>".$row1['code2']."</th>"); echo ("<th>".$row1['description']."</th>"); echo ("<th>".$row1['discountper']."</th>"); echo ("<th>".$row1['discountamount']."</th>"); echo ("<th>".$row1['quantity']."</th>"); echo ("<th>".$row1['price']."</th>"); echo ("<th>".$row1['amount']."</th>"); echo ("<th>".$row1['totaldiscount']."</th>"); echo ("<th>".$rowz['name']."</th>"); echo ("<th><a href='edit.php?id=".$row1['id']."'>edit</a></th>"); echo ("</tr>"); }else{ echo ("<tr class='dark'>"); echo ("<th>".$row1['code']."</th>"); echo ("<th>".$row1['code2']."</th>"); echo ("<th>".$row1['description']."</th>"); echo ("<th>".$row1['discountper']."</th>"); echo ("<th>".$row1['discountamount']."</th>"); echo ("<th>".$row1['quantity']."</th>"); echo ("<th>".$row1['price']."</th>"); echo ("<th>".$row1['amount']."</th>"); echo ("<th>".$row1['totaldiscount']."</th>"); echo ("<th>".$rowz['name']."</th>"); echo ("<th><a href='edit.php?id=".$row1['id']."'>edit</a></th>"); echo ("</tr>"); } } echo "</table>"; ?> </div> </body> </html>
here php code runs ajax file should put javascript
<?php include("header.php"); include("mysqlconnection.php"); if(!isset($_post['custname'])){ die("<h2 align='center'>you not allowed visit page</h2>"); } ?> <html> <head> <script type="text/javascript" src="jquery1.9.1.min.js"></script> <script type="text/javascript" src="jquery.js"></script> <script type="text/javascript" src="jquerysearchabledropdown.min.js"></script> <script> function showuser(){ var str=document.form.users.value; var qty=document.form.qty.value; var custid=document.form.custid.value; var warehouseid=document.form.warehouse.value; if (window.xmlhttprequest){ xmlhttp=new xmlhttprequest(); } xmlhttp.onreadystatechange=function(){ if (xmlhttp.readystate==4 && xmlhttp.status==200){ document.getelementbyid("txthint").innerhtml=xmlhttp.responsetext; alert('123'); } } xmlhttp.open("get","getcode.php?user="+str+"&qty="+qty+"&custid="+custid+"&warehouseid="+warehouseid,true); xmlhttp.send(); document.form.users.value=''; document.form.users.focus(); } $(document).ready(function(){ $("#users").keypress(function(event) { if (event.which == 13) { event.preventdefault(); document.getelementbyid('goo').click(); } }); }); $(document).ready(function() { $("#users2").searchable(); }); function appendselectoption(str) { $("#users2").append("<option value=\"" + str + "\">" + str + "</option>"); } </script> </head> <body onload="document.form.users.focus()"> <div id=""> </br></br> <form name="form" method="post"> <table border="0" align="center"> <tr> <td align="center"><select id="users2" name="users" width="350" style="width: 230px"> <option value="" selected="selected">please select</option> <?php $sql1="select code,description,code2,id items"; $result1=mysql_query($sql1,$con); for($counter=0;$row=mysql_fetch_row($result1);$counter++) print ("<option value = '$row[0]-$row[3]-$row[2]'>$row[0] - $row[1] - $row[2]</option>"); ?> </select> </td> <td align="center"><select id="warehouse" name="warehouse" > <option value="" selected="selected">please select</option> <?php $sql1="select id,name warehouse order id desc"; $result1=mysql_query($sql1,$con); for($counter=0;$row=mysql_fetch_row($result1);$counter++) print ("<option value = '$row[0]'>$row[1]</option>"); ?> </select> </td> <td> <input type="text" name="qty" id="qty" size="2"/> </td> <td> <input type="button" name="goo" id="goo" value="go" onclick="showuser()"/> </td> <td> <input type="hidden" name="custid" id="custid" value="<?php echo $_post['custname'];?>" size="3"/> </td> <td> <input type="hidden" name="warehouseid" id="warehouseid" value="<?php echo $_post['warehouse'];?>" size="3"/> </td> <td> <input type="hidden" name="date" id="date" value="<?php echo $_post['date'];?>"/> </td> </tr> </table> </form> </div> <div id="txthint"></div> <script> $(document).ready(function(){ $('#users').attr("placeholder", "barcode search..."); $('#users').focusin(function(){ $(this).attr("placeholder", ""); }); $('#users').focusout(function(){ $(this).attr("placeholder", "barcode search..."); }); }); </script> </body> </html>
to run javascript function after ajax call should below
$.ajax({ url: "getcode.php?user="+str+"&qty="+qty+"&custid="+custid+"&warehouseid="+warehouseid, type:"get", success: function(responsetext) { $("#txthint").html(responsetext); $("#txthint").find("script").each(function(i) { eval($(this).text()); }); } });
put javascript function in success
function.. function fire when there success ajax call
edit
as per coding need following change in function
xmlhttp.onreadystatechange=function(){ if (xmlhttp.readystate==4 && xmlhttp.status==200){ document.getelementbyid("txthint").innerhtml=xmlhttp.responsetext; alert('something') // put javascript here }
let me know if other needed
Comments
Post a Comment