【发布时间】:2017-03-28 02:18:05
【问题描述】:
我正在为要求 3 个输入的表单创建 AJAX 功能 - 床、浴室、频率。我希望输出是价格。所以用户在输入的时候,只会输出我创建的Array对应的价格。
PHP 代码片段(数组):
$pricing = array (
array ('frequency' => "one", 'beds' => 1 , 'baths' => 1 , 'price' => 90),
array ('frequency' => "one", 'beds' => 1 , 'baths' => 1.5 , 'price' => 113),
array ('frequency' => "one", 'beds' => 1 , 'baths' => 2 , 'price' => 113),
array ('frequency' => "one", 'beds' => 2 , 'baths' => 2.5 , 'price' => 135),
array ('frequency' => "weekly", 'beds' => 3 , 'baths' => 3 , 'price' => 135),
array ('frequency' => "weekly", 'beds' => 3 , 'baths' => 3.5 , 'price' => 158),
array ('frequency' => "biweekly", 'beds' => 4 , 'baths' => 4 , 'price' => 158),
array ('frequency' => "biweekly", 'beds' => 4 , 'baths' => 4.5 , 'price' => 180),
array ('frequency' => "monthly", 'beds' => 5 , 'baths' => 5 , 'price' => 180),
array ('frequency' => "monthly", 'beds' => 5 , 'baths' => 5.5 , 'price' => 203),
array ('frequency' => "monthly", 'beds' => 6 , 'baths' => 6 , 'price' => 203)
);
我设法传递了输入的值,但我的函数不起作用。显然我可以回显(回显 $selected_frequency_id;)从我的表单中选择的值,但我不能回显价格。我的功能不正确吗?我找不到 foreach 循环有什么问题。请看下面的代码:
PHP 代码继续:
function ajax_update_price() {
$selected_bed_id = $_POST['bedID'];
$selected_bath_id = $_POST['bathID'];
$selected_frequency_id = $_POST['frequencyID'];
//echo $selected_frequency_id;
foreach( $pricing as $element ) {
if( $element['frequency'] == $selected_frequency_id && $element['beds'] == $selected_bed_id && $element['baths'] == $selected_bath_id) {
echo $element['price'];
break;
}
}
wp_die();
}
【问题讨论】:
-
这和 JavaScript 有什么关系?