使用jquery

时间:2017-09-04 05:27:46

标签: php jquery mysql

我正在PHP中构建一个简单的应用程序。我在输入字段中从mysql获取数据然后执行basi算术运算。 我正在使用以下代码。

<script type="text/javascript">
$(document).ready(function(){

$(document).on('keydown', '.username', function() {

var id = this.id;
var splitid = id.split('_');
var index = splitid[1];

$( '#'+id ).autocomplete({
source: function( request, response ) {
$.ajax({
url: "getDetails.php",
type: 'post',
dataType: "json",
data: {
search: request.term,request:1
},
success: function( data ) {
response( data );
}
});
},
select: function (event, ui) {
$(this).val(ui.item.label); // display the selected text
var userid = ui.item.value; // selected id to input

                            // AJAX
$.ajax({
url: 'getDetails.php',
type: 'post',
data: {userid:userid,request:2},
dataType: 'json',
success:function(response){

var len = response.length;

if(len > 0){
 var id = response[0]['id'];
 var price = response[0]['price'];

                                        document.getElementById('price_'+index).value = price;
                                    }

                                }
                            });

                            return false;
                        }
                    });
                });

            });

        </script>

<script>
   $( document ).ready(function() {
    $('input.amount').keyup(function(){   
    var price = $("#price").val();
    var qty = $("#qty").val();
    var tax = $("#taxa").val();
    var disc = $("#disca").val();
    var total = $("#total").val();
      var total = (parseFloat(price) * parseFloat(qty)) + (parseFloat(tax) - parseFloat(disc));
    $("#total").val(parseFloat(total));
   });
});
</script>
  <td width="7%">Item Name</td>
    <td width="16%"><input type="text" name="item" style="height:35px;border:groove;width:200px;" /></td>
    <td width="3%">Qty.</td>
    <td width="7%"><input type="text" name="qty" id="qty" style="height:35px;border:groove;width:70px;"  class="amount"/></td>
    <td width="3%">Price</td>
    <td width="7%"><input type="text" name="price" id="price" style="height:35px;border:groove;width:80px;"  class="amount"/></td>
    <td width="4%">Tax</td>
    <td width="7%"><select name="tax"  style="height:35px;border:groove;width:80px;"></select></td>
    <td width="5%">Tax Amt.</td>
    <td width="7%"><input type="text" id="taxa" name="taxAmmount" style="height:35px;border:groove;width:80px;"  class="amount" /></td>
    <td width="4%">Disc. %</td>
    <td width="6%"><input type="text"  name="disc" style="height:35px;border:groove;width:70px;" /></td>
    <td width="6%">Disc. Amt.</td>
    <td width="7%"><input type="text" id="disca" name="discAmmount" style="height:35px;border:groove;width:80px;"  class="amount" /></td>
    <td width="3%">Total</td>
    <td width="8%"><input type="text" id="total" name="total" style="height:35px;border:groove;width:80px;"  class="amount" /></td>

getDetails.php

<?php
include "config.php";

$request = $_POST['request'];   // request

// Get username list
if($request == 1){
$search = $_POST['search'];

$query = "SELECT * FROM item WHERE itemName like'%".$search."%'";
$result = mysqli_query($con,$query);

while($row = mysqli_fetch_array($result) ){
$response[] = array("value"=>$row['id'],"label"=>$row['itemName']);
}

    // encoding array to json format
echo json_encode($response);
exit;
}

// Get details
if($request == 2){
$userid = $_POST['userid'];
$sql = "SELECT * FROM item WHERE id=".$userid;

$result = mysqli_query($con,$sql);

$users_arr = array();

while( $row = mysqli_fetch_array($result) ){
$userid = $row['id'];
$price = $row['salePrice'];
$users_arr[] = array("id" => $userid, "price" => $price,  );
 }

    // encoding array to json format
echo json_encode($users_arr);
exit;
}

错误是什么?实际上我从互联网上抓了两个代码并将它们组合在一起它们两个单独工作正常但是当我把它们放在一起时它们不能正常工作。 请帮助我是初学者程序员,请帮助我学习。谢谢

0 个答案:

没有答案