如何获取从ajax传递的id

时间:2015-12-19 11:24:17

标签: javascript php jquery html ajax

请帮助我:)。

我有这样的表格  http://i.stack.imgur.com/JgyyQ.png

这是上面表格的代码

input_espenses.php

<div class="form-group">
	<label class="col-sm-5 col-md-5 col-lg-4 control-label">Jenis pengeluaran</label>
	<div class="col-md-4">
		<select name="type" id="type" onchange="showPrice(this.value)" required>
		<option value=""></option>
        	<?php
                include ('../connector.php');
                
                $query="SELECT * FROM tabel_equipment WHERE sign=0";
                
                $hasil=mysql_query($query, $id_mysql);
                
                while ($record=mysql_fetch_row($hasil))
                {
                    $ID= $record[0];
                    $equipment=$record[1];
                    $price=$record[3];
                    echo "<option value=\"$record[0]\">$record[1]</option>";
                }
            ?>
		 </select>
	</div>
</div>

<div class="form-group">
<label class="col-sm-5 col-md-5 col-lg-4 control-label">Harga Satuan</label>
	<div class="col-md-4" id="txtHint" class="tip-right form-control input-sm"/>
		<input type="text" placeholder="Pilih jenis pengeluaran" readonly="">
	</div> 
</div>
			
<div class="form-group">
<label for="" class="col-sm-5 col-md-5 col-lg-4 control-label">Jumlah</label>
	<div class="col-md-2">
		<input type='text' id="banyak" name="amount" onkeyup="hitung2(getElementById('txtHint').value,this.value)" title="Masukkan jumlah" class="tip-right form-control input-sm" required/>
	</div>
</div>

<div class="form-group">
<label for="" class="col-sm-5 col-md-5 col-lg-4 control-label">Total</label>
	<div class="col-md-4" id="total">
		<input type='text' title="Masukkan total biaya" class="tip-right form-control input-sm" readonly/>
	</div>
</div>

这是从我的数据库中获取“Harga Satuan”内部值的代码(我使用ajax获取值)

price.js

function showPrice(str) {
    if (str == "") {
        document.getElementById("txtHint").innerHTML = "";
        return;
    } else { 
        if (window.XMLHttpRequest) {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        } else {
            // code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
            }
        };
        xmlhttp.open("GET","../pages/getprice.php?q="+str,true);
        xmlhttp.send();
    }
}

getprice.php

<?php
$q = intval($_GET['q']);

include ('../connector.php');

$query="SELECT * FROM tabel_equipment WHERE ID = '".$q."'";				                                        
$hasil=mysql_query($query, $id_mysql);

while ($record=mysql_fetch_row($hasil))
{
    $ID= $record[0];
    $equipment=$record[1];
    $price=$record[3]; ?>
    	<input type="text" class="" name="satuan" id="txtHint" value="<?=$price?>" readonly>
    <?php
}
?>

返回表格http://i.stack.imgur.com/JgyyQ.png

我的问题是如何从“Harga Satuan”(id = txtHint)获取id,所以我可以使用“Jumlah”中用户输入的值将其复用,并将结果显示为“Total”

这个我的脚本要多个

totaleq.js

function hitung2(txtHint,banyak){

    var sum = eval(txtHint) * eval(banyak);
    document.getElementById('total').innerHTML = sum;

};

0 个答案:

没有答案