2015-12-04 89 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中。写下它,无论你想记住什么,然后在另一页上需要它时读取它。