2015-12-04 90 views
1

我有網絡電子商務,它使用傳遞代理網站的API rajaongkir.com, 我想使用傳遞代理的價格是我的數據庫中的變量。從內部發送變量php內部

她是文件order.php

<script type="text/javascript" src="js/script.js"></script> 
<form action="input.php?input=inputform" method="post"> 
<table class="zebra-table"> 
      <thead> 
      <tr> 
       <th>Kurir</th> 
       <th>Servis</th> 
       <th>Deskripsi Servis</th> 
       <th>Lama Kirim (hari)</th> 
       <th>Total Biaya (Rp)</th> 
       <th>Opsi</th> 
      </tr> 
      </thead> 
      <tbody id="resultsbox"></tbody> 
</table> 

的script.js

function cekHarga(){ 
//var origin = $('#oricity').val(); 
var origin = 35; 
var destination = $('#descity').val(); 
var weight = $('#berat').val(); 
var courier = $('#service').val(); 
$.ajax({ 
    url:'process.php?act=cost', 
    data:{origin:origin,destination:destination,weight:weight,courier:courier}, 
    success:function(response){ 
     $('#resultsbox').html(response); 
    }, 
    error:function(){ 
     $('#resultsbox').html('ERROR'); 
    } 
}); 

}

process.php

if(isset($_GET['act'])): 
switch ($_GET['act']) { 
$cost = $IdmoreRO->hitungOngkir($origin,$destination,$weight,$courier); 
    //parse json 
    $costarray = json_decode($cost); 
    $results = $costarray->rajaongkir->results; 
    if(!empty($results)): 
     foreach($results as $r): 
      foreach($r->costs as $rc): 
       foreach($rc->cost as $rcc): 
       echo "<tr><td>$r->code</td><td>$rc->service</td><td>$rc->description</td><td>$rcc->etd</td><td>".number_format($rcc->value)."</td> <td></td></tr>"; 
        $bayarr=$rcc->value;             
       endforeach; 
      endforeach; 
     endforeach; 
    endif; 
} 
endif; 

我可以在形式訪問變量$ bayarr在order.php。

如何發送變量$ bayarr在process.php到order.php?

<tbody id="resultsbox"></tbody>

+1

你的問題有些含糊。如果你想把這個變量「發送」到'order.php',那麼頭部重定向('header(「Location:order.php」);')就是你要找的東西。如果你想使用order.php中的函數,你可以使用類方法(定義,包括和啓動)。請更確切地說明你真正想要的東西。 – Jan

+0

當我嘗試所有代碼粘貼在這裏,它說錯誤張貼。我在PHP編程中太小氣了。我會學到更多。 thx尋求建議 – Hafidh

回答

0

使用json發送從Ajax調用超過1倍的值。

$arr['html'] = "<tr><td>$r->code</td><td>$rc->service</td><td>$rc->description</td><td>$rcc->etd</td><td>".number_format($rcc->value)."</td> <td></td></tr>"; 
$arr['price'] = $bayarr=$rcc->value; 
echo json_encode($arr); 

在阿賈克斯成功,

success: function(response){ 
     var content = JSON.parse(response); 
     $('#resultsbox').html(response.html); 
     alert(response.price);// will have your price here. 
} 
0

如果你想用它給你HTML中你可以在你的反應和后里面添加一些HTML標記的帕拉姆同樣要求一些特定的值該解析吧..

東西爲:

foreach($rc->cost as $rcc): 
    echo "<tr data-bayarr=\"{$rcc->value}\"><td>$r->code</td><td>$rc->service</td><td>$rc->description</td><td>$rcc->etd</td><td>".number_format($rcc->value)."</td> <td></td></tr>"; 
endforeach; 

之後只是繼續它在你的Ajax更迭爲:

success:function(response){ 
    $('#resultsbox').html(response); 
    /* Because its in a foreach there will be multiple bayarrs .. */ 
    $('#resultsbox').find('[data-bayarr]').each(function(i) { 
     alert($(this).data('bayarr')); 
    }); 
}, 

另一種方式是將響應轉換成JSON數組,你必須HTML和bayarr值轉換..

0

您可以編碼整個$results數組,你循環,如果你有到:

<input type="hidden" id="data_clustor" name="data_clustor" 
value="<?php echo base64_encode(json_encode($results)); ?>"> 

然後,只需引用$('#data_clustor').val();,並把它傳遞給你的下一個PHP頁面,你會簡單地做:

$results=json_decode(base64_decode($_POST['data_clustor'])); 

並將其重新循環。或者只傳遞你感興趣的數據,而不是整個整個陣列。

沒有看到你的代碼,調用cekHarga()或你與引用元素:

var destination = $('#descity').val(); 
var weight = $('#berat').val(); 
var courier = $('#service').val(); 

它變得有點難以看到你想要做什麼,因爲目前還不清楚在那裏你拉如果您向我們展示的任何代碼都包含這些ID,則這些字段將包含該ID。

或者,只需使用PHP會話,並將特定用戶的服務器上的信息加載到$_SESSION中。寫下它,無論你想記住什麼,然後在另一頁上需要它時讀取它。