我有一個PHP函數返回JSON響應ajax請求的傳送費用。這裏是PHP函數:PHP和Prototype的AJAX
public function getDeliveryOptions($weight, $postcode)
{
if($weight == 0)
{
return array(array("name" => "Non-Deliverable", "value" => "0.00"));
}
$this->db->where('dmc_lower_boundary <=', $weight);
$this->db->where('dmc_upper_boundary >=', $weight);
if(preg_match("([aA][bB](3[1-8]|4[1-5]|5[1-6])\s?[1-9][a-zA-Z]{2}|[fF][kK](19|20|21)\s?[1-9][a-zA-Z]{2}|[hH][sS][1-9]\s?[1-9][a-zA-Z]{2}|[iI][vV][1-9]{1,2}\s?[1-9][a-zA-Z]{2}|[kK][aA](27|28)\s?[1-9][a-zA-Z]{2}|[kK][wW][1-9]{1,2}?\s?[1-9][a-zA-Z]{2}|[pP][aA][2-8][0-9]\s?[1-9][a-zA-Z]{2}|[pP][hH]([156789]|1[056789]|[2-5][0-9]){1,2}\s?[1-9][a-zA-Z]{2}|[zZ][eE][1-9]\s?[1-9][a-zA-Z]{2})", $postcode))
{
$this->db->where("dm_id = ", 1);
$this->db->or_where("dm_id =", 3);
}
elseif(preg_match("/([bB][tT][1-9]{1,2}\s?[1-9][a-zA-Z]{2}|[iI][mM][1-9]{1,2}\s?[1-9][a-zA-Z]{2}|[tT][rR](21|22|23|24|25)\s?[1-9][a-zA-Z]{2})/", $postcode))
{
$this->db->where("dm_id =", 1);
$this->db->or_where("dm_id =", 4);
}
elseif(preg_match("/([gG][yY][1-9]\s?[1-9][a-zA-Z]{2}|[jJ][eE][1-4]\s?[1-9][a-zA-Z]{2})/", $postcode))
{
$this->db->where("dm_id =", 1);
$this->db->or_where("dm_id =", 5);
}
else
{
$this->db->where("dm_id =", 1);
$this->db->or_where("dm_id =", 2);
}
$this->db->group_by("dm_id");
$query = $this->db->get("delivery_method_option_views");
//print_r($query->result());
//print_r(json_encode($query->result()));
return(json_encode($query->result()));
}
這裏是我的JavaScript(使用原型)將結果發送到服務器,並使用響應創建單選按鈕:
function updateDelivery()
{
if($('deliveryUpdate') == null)
{
return false;
}
// This 'observes' our form submit - sort of like onsubmit
$('deliveryUpdate').observe('submit', function(evt) {
// Stop the actual event from happening
evt.stop();
// This is the url we submit to - change it as needed
var url = '/checkout/updateDelivery';
//This will be the form and div we update
var containerForm = $('deliveryUpdate');
var optionsDisplay = $('deliveryOptions');
// Grab all the info in the form
var form_data = containerForm.serialize();
var form = containerForm.innerHTML;
// Here we make the request
new Ajax.Request(url, {
method: 'post',
parameters: form_data,
onCreate: function() {
form = containerForm.innerHTML;
containerForm.update(form+'<img src="/images/loader.gif" alt="loading..." class="loader" />');
},
onSuccess: function(transport) {
containerForm.update(form);
NVPResponse = transport.responseText;
if(NVPResponse !== '[object Array]')
{
NVPResponse = new Array(NVPResponse);
}
alert(NVPResponse);
options = "";
for(i=0; i<NVPResponse.length; ++i)
{
options += '<label for="delivery">'+NVPResponse[i].dm_name+'</label><input type="radio" name="delivery" value="'+NVPResponse[i].dmc_price+'" />';
}
//optionsDisplay.update(NVPResponse);
optionsDisplay.update(options);
}
});
});
}
如果我剛剛與更新JSON結果(郵編ab31 2AS),我得到如下:
Calculate Delivery Costs[{"dm_id":"1","dm_name":"Royal Mail","dm_admin_name":"Royal Mail","dmc_lower_boundary":"101","dmc_upper_boundary":"250","dmc_price":"3.65"},{"dm_id":"3","dm_name":"Courier","dm_admin_name":"Fed Ex Zone 4","dmc_lower_boundary":"101","dmc_upper_boundary":"250","dmc_price":"4.50"}]
但是,如果我的選項VAR更新,我得到 「未定義」,其中標籤文本和單選按鈕值。這是在onSuccess函數的for循環中構建的。有任何想法嗎?
編輯
我之前添加以下代碼在for循環,因爲我試圖用JSON字符串作爲對象,而不是對象:
NVPResponse = JSON.parse(NVPResponse);
但我的數據庫查詢拉正確的結果? :S感謝第一個數組雖然:) –
我的不好。 CI沒有在OR語句中放置括號,因此它們往往是越野車。我編輯了答案來反映這一點。 – Elad