使用AJAX将变量传递给PHP并再次获取该变量

时间:2014-07-22 23:12:35

标签: javascript php jquery ajax

我想将值传递给PHP脚本,所以我使用AJAX传递那些并且该部分有效。但是在同一个函数中,我想要检索传递给PHP脚本的那些值。问题是我无法从PHP文件中检索任何值。我有搜索高低,但现在来找你答案。如何将传递给PHP脚本的变量存储起来,以便我的ajax可以检索它?我的代码如下:

这是我的表格:

<!-- file album.php -->
<html>
<head>

    <?PHP
      include ("conection/connect.php");
    ?>
    <script type="text/javascript" src="jstbl/jquery-1.3.2.min.js"></script>
    <script type="text/javascript">

        function cekjumlah(tableID)
        {
            var hitung=document.getElementById(tableID).rows.length;
            var jumlah = hitung-1;
            document.getElementById("media").value=jumlah;
        }

    </script>

</head>
<title></title>
<body>

    <form name="form1" id="form1" method="post">
        Jumlah Inputan : 
        <input type="text" id="media" name="media" size="5">
        <input type="text" id="data_barang" name="data_barang" size="60" onKeyPress="onEnterPenjualan(event);">
        <br><br>

        <table id="tabelimei" border="1">
            <tr id="Last">
            </tr>
        </table>
        <br><br>
        <div id="menu">
            <p>
                <input type="submit" name="simpan" value="Simpan" onClick="cekjumlah('tabelimei')">
                <input onClick="deleteRow('DivTambah')" name="button" type="submit" value="Hapus" />
            </p>
            <p id="hasil">hasil</p>
        </div>
    </form>
</body>
</html>

这是我的jquery:

function onEnterPenjualan(e){// => Digunakan untuk membaca karakter enter
    var key=e.keyCode || e.which;
    if(key==13){// => Karakter enter dikenali sebagai angka 13
        $(function(){
        var data_barang = $("#data_barang").val();
        $.ajax({
            url: "ambildatabarang.php",
            data: "data_barang="+data_barang,
            cache: false,
            success: function(data){
                console.log(data);
                alert(data);  
            }
        });                   
        var nama = alert (data.$nama);
        var baris = document.getElementById("data_barang").value;
        var i = 1;
        var row = $(document.createElement('tr')).attr("id", 'DivTambah' + i);
        row = '<tr>'+
              '<td>1</td>'+
              '<td><input name="cek[0]" id="cek" type="checkbox" size="10" /></td>'+
              '<td><input name="qty_penjualan[0]" type="text" id="qty_penjualan" value="1" size="10" required/></td>'+
              '<td><input type="text" name="imei_penjualan[0]" id="imei_penjualan" size="60" value="'+baris+'" required/></td>'+
              '<td><input type="text" name="nama_penjualan[0]" id="nama_penjualan" size="30" value = "'+nama+'"required/></td>'+
              '<td><input type="text" name="hargajual_penjualan[0]" id="hargajual_penjualan" value="300000" size="15" required/></td>'+
              '<td><input type="text" name="diskon_penjualan[0]" id="diskon_penjualan" size="15" value="0"/></td>'+
              '<td><input type="text" name="total_penjualan[0]" id="total_penjualan" size="15" value="300000" required/></td>'+
              '<td><button type="button" class="del">Del</button></td>'+
              '</tr>';
        $(row).insertBefore("#Last");
        var hapus = "";
        document.getElementById('data_barang').value=hapus;
        document.getElementById('data_barang').value.focus();
        i++;
    });

    $(".del").live('click', function(){
        $(this).parent().parent().remove();
    });

}}

这是我的PHP文件:

<?php

if (isset($_GET['data_barang'])){    
      // Instructions if $_POST['value'] exist 
    include ("conection/koneksidb.php");
    $databarang = $_GET['data_barang'];
    $datalengkapbarang = mysql_query("SELECT i.id_imeibarang, jb.nama_jenisbarang, b.type_barang, b.hargajual_barang FROM jenisbarang jb, barang b, imeibarang i WHERE i.id_imeibarang='$databarang' and i.idbarang_imeibarang=b.id_barang and b.idjenis_barang=jb.id_jenisbarang");

    $angka = 1; 
    while($k = mysql_fetch_array($datalengkapbarang)){
        echo $no[$angka] = $angka;
        echo $imei[$angka]=$k['id_imeibarang'];
        echo $nama[$angka]=$k['nama_jenisbarang'].$k['type_barang'];
        echo $harga[$angka]=$k['hargajual_barang'];

        $angka = $angka+1;
    }  
} 
?>

1 个答案:

答案 0 :(得分:0)

将一些数据发送到php并同步获取php文件的结果到javascript,这就是你需要的

function sendData (data1,data2)
{
            if (window.XMLHttpRequest)
                AJAX=new XMLHttpRequest(); 
            else
                AJAX=new ActiveXObject("Microsoft.XMLHTTP");
            if (AJAX)
            {
                AJAX.open("POST", "url.php", false);
                AJAX.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                AJAX.send("data1=" + data1 + "&data2=" + data2);
                return AJAX.responseText;                                         
            } 
            else
                return null;
}

现在,在您的php文件中,您只需获取此类数据

$data1 = $_POST['data1'];
$data2 = $_POST['data2'];

一旦你使用你的php文件中的数据,然后你echo结果,它就会被javascript函数返回,所以你只需要在你的javascript文件中使用它

var data_from_php = sendData(data1,data2);