将ID值发送到ajax jquery

时间:2013-12-18 06:17:15

标签: jquery ajax

伙计们我有一个搜索输入并将其值发送到ajax jquery并成功将结果显示在div中,我可以再次将该div上的值(id)发送给另一个ajax jquery吗?我试过这个但不发送任何萤火虫,这是程序流从html发送id值到ajax然后发送到cari.php显示结果在div(responsecontainer)和从ajax jquery结果我想发送id值到显示细节的第二个ajax ..

this is my jquery

    $('.cariinvbranch').click(function(e) { 
    var branch = $("#branch").val();
    var status = $("#status").val();   
        $.ajax({
            type: "POST",
            url: "pages/cari.php",
            data:  'branch='+branch+'&status='+status,                      
            success: function(response){
                $("#responsecontainer").html(response);
                //alert(data);
            }                             
        }); 

    }); 


    $(document).ready(function(){ 
        $('.detil').click(function(e) {         
        var id = $(this).attr('id');
        var dataString = 'id='+ id ;
        alert(dataString);
            $.ajax({
                type: "POST",
                url: "pages/detil.php",
                data:  dataString,                      
                success: function(response){
                    $("#responsecontainer").html(response);
                }                             
            });     
        });

$('.cariinvbranch').click(function(e) { 
    var branch = $("#branch").val();
    var status = $("#status").val();   
        $.ajax({
            type: "POST",
            url: "pages/cari.php",
            data:  'branch='+branch+'&status='+status,                      
            success: function(response){
                $("#responsecontainer").html(response);
                //alert(data);
            }                             
        }); 

    }); 





my html

    <select name='branch' id='branch' class='searchtoolbar' >
                <option></option>
                <?php
                $query = $database->GetBranch();
                while($row = mysql_fetch_array($query))
                {
                    echo "<option value=$row[branch]>$row[branch]</option>";            
                }
                ?>
            </select>
            <select name='status' id='status' class='searchtoolbar'>
                <option></option>
                <?php
                $query = $database->GetStatus();
                while($row = mysql_fetch_array($query))
                {
                    echo "<option value=$row[asset_status]>$row[asset_status]</option>";            
                }
                ?>
            </select>
            <a href ='#' class='cariinvbranch' id='branch'>Cari</a>

        <div ='responsecontainer'>

        <result here from search>

        </div>

-------------------------------------------------------------
cari.php

        <?php
    // if($_POST['branch'] !== '' AND $_POST['status'] !== ''){
    include_once('../include/classes/session.php');
    if(strlen($_POST['branch']) < 1){
    $_POST['branch'];
    $status = $_POST['status'];

    $result1 = $database->getPcAsset();
    $num_rows1 = mysql_num_rows($result1);
    for ($i = 0; $i < $num_rows1; $i++) {
    $idx = mysql_result($result1,$i,'idx');}

    $result=$database->CariInvStatusAll($status);
    $numrows = mysql_num_rows($result);
    echo "<table id = 'asset'>
            <tr class = 'header'>
                <td>
                    <input type='checkbox' name='cek'>
                </td>
                <td>
                    Name
                </td>       
                <td>
                    Model
                </td>
                <td>
                    OS
                </td>
                <td>
                    Asset Status
                </td>
                <td>
                    Branch
                </td>
                <td>
                    SN
                </td>
            </tr>   
        </table>";
    for($i=0;$i<$numrows;$i++){
    $idx = mysql_result($result,$i,'idx');
    $name = mysql_result($result,$i,'name');
    $model = mysql_result($result,$i,'model');
    $operation_system = mysql_result($result,$i,'operation_system');
    $asset_status = mysql_result($result,$i,'asset_status');
    $branch = mysql_result($result,$i,'branch');
    $serial_number = mysql_result($result,$i,'serial_number');


    echo "
        <table>
            <tr>
                <td>
                    <input type='checkbox' name='cek'>
                </td>
                <td>                
                    <a href='#' class='detil' id='$idx'>$name<a>
                        $idx
                </td>
                <td>
                    $model
                </td>
                <td>
                    $operation_system
                </td>
                <td>
                    $asset_status
                </td>
                <td>
                    $branch
                </td>
                <td>
                    $serial_number
                </td>
            </tr>   
        </table>";

    }

----------------------------------------------------

detil.php

    <?php
    include_once ("../include/classes/session.php");

    $id = $_POST['id'];
    $result = $database->getDetilPcAsset($id);
    $num_rows = mysql_num_rows($result);
     for ($i = 0; $i < $num_rows; $i++) {
    $idx = mysql_result($result,$i,'idx');
    $name = mysql_result($result,$i,'name');
    $model = mysql_result($result,$i,'model');
    $manufacturer = mysql_result($result,$i,'manufacturer');
    $purchasing_date = mysql_result($result,$i,'purchasing_date');
    $operation_system = mysql_result($result,$i,'operation_system');
    $ipaddress = mysql_result($result,$i,'ipaddress');
    $asset_status = mysql_result($result,$i,'asset_status');
    $branch = mysql_result($result,$i,'branch');
    $location = mysql_result($result,$i,'location');
    $serial_number = mysql_result($result,$i,'serial_number');
    $warranty_expired = mysql_result($result,$i,'warranty_expired');
    $delivered_date = mysql_result($result,$i,'delivered_date');
    $pic = mysql_result($result,$i,'pic');
    $installation_date = mysql_result($result,$i,'installation_date');
    $requested_by = mysql_result($result,$i,'requested_by');
    $vendor_name = mysql_result($result,$i,'vendor_name');

    echo "<table id='tableassetdetil'>
        <tr>
            <td>
                Name
            </td>
            <td>
                :
            </td>

            <td>
                $name
            </td>
        </tr>

        <tr>
            <td>
                Manufacturer
            </td>
            <td>
                :
            </td>
            <td>
                $manufacturer
            </td>
        </tr>
        <tr>
            <td>
                purchasing_date
            </td>
            <td>
                :
            </td>
            <td>
                $purchasing_date
            </td>
         </tr>
        <tr>
            <td>
                operation_system
            </td>
            <td>
                :
            </td>
            <td>
                $operation_system
            </td>
         </tr>
        <tr>
            <td>
                ipaddress
            </td>
            <td>
                :
            </td>

            <td>
                $ipaddress
            </td>
        </tr>
        <tr>
            <td>

                asset_status
            </td>
            <td>
                :
            </td>
            <td>
                $asset_status
            </td>
        </tr>
        <tr>
            <td>
                branch
            </td>
            <td>
                :
            </td>
            <td>
                $branch
            </td>
        </tr>
        <tr>
            <td>
                location
            </td>
            <td>
                :
            </td>

            <td>
                $location
            </td>
        </tr>

        <tr>
            <td>
                serial_number
            </td>
            <td>
                :
            </td>
            <td>
                $serial_number
            </td>
   </tr>
        <tr>
            <td>
                warranty_expired 
            </td>
            <td>
                :
            </td>
            <td>
                $warranty_expired 
            </td>
        </tr>
        <tr>
            <td>
                delivered_date
            </td>
            <td>
                :
            </td>
            <td>
                $delivered_date
            </td>
        </tr>
        <tr>
            <td>
                pic
            </td>
            <td>
                :
            </td>
            <td>
                $pic
            </td>
        </tr>
        <tr>
            <td>
                installation_date
            </td>
            <td>
                :
            </td>
            <td>
                $installation_date
            </td>
        </tr>
        <tr>
            <td>

                requested_by 
            </td>
            <td>
                :
            </td>
            </td>
            <td>
                $requested_by 
            </td>
        </tr>
        <tr>
            <td>
                vendor_name
            </td>
            <td>
                :
            </td>

            <td>
                $vendor_name
            </td>
        </tr>       
    </table>    ";

     }


    ?>

2 个答案:

答案 0 :(得分:0)

您可以使用键值对{key1: 'value1', key2: 'value2'}

 var dataString = {id: id};

答案 1 :(得分:0)

我的代码中没有看到任何.detil元素。无论如何,您应该使用.on()方法,这样您的脚本将查看当前的元素集。因为在您加载AJAX内容之后,.click()将无法看到新元素。

$('.parent').on('click', '.child', function() {
    // Do magic
});

这是文档中的link for .on()

考虑使用对象。

$.ajax({
    type: "POST",
    url: "some.php",
    data: { name: "John", location: "Boston" },
    success: function(resp) {
        // Magic
    }
});