我从数据库中得到一个包含支付数据的数组。需要选择特定的,形成JSON,并用JS替换结果,根据HTML表单中的输入分配数据。
我得到这样的数据:
while($row = $results->fetch_assoc()) {
$payment_service = $row["payment_service"];
if ($payment_service == 'PayPal'){
$paypal_item_number = $row["id"];
$paypal_amount = $row["amount"];
$paypal_curr_code = $row["currency_code"];
$validity = $row["validity"];
$paypal_email = $row["paypal_email"];
var_dump($row["id"]);
var_dump($row);
//echo json_encode($row);
}
}
var_dump($row["id"]);结果:
string(2) "74"
string(2) "76"
string(2) "77"
var_dump($row);结果:
array(8) {
["id"]=>
string(2) "74"
["amount"]=>
string(2) "10"
["currency_code"]=>
string(3) "USD"
["validity"]=>
string(2) "72"
["payment_service"]=>
string(6) "PayPal"
["paypal_email"]=>
string(16) "book@ya.ru"
["stripe_pkey"]=>
string(0) ""
["status"]=>
string(1) "1"
}
array(8) {
["id"]=>
string(2) "76"
["amount"]=>
string(3) "100"
["currency_code"]=>
string(3) "RUB"
["validity"]=>
string(1) "5"
["payment_service"]=>
string(6) "PayPal"
["paypal_email"]=>
string(16) "book@ya.ru"
["stripe_pkey"]=>
string(0) ""
["status"]=>
string(1) "1"
}...
进一步在表单中,使用 select 我选择所需的 id
<option value="74">USD 10</option>
<option value="76">RUB 100</option>
<option value="77">RUB 500</option>
更进一步的代码,使用 js 获取所选选择的值
var select = document.getElementById("PayPalAmount");
select.addEventListener("change", function(){
getValue = this.value;
// выбранный select ($row["id"]) = getValue
//alert( getValue );
});
现在我从 select 中获得了所需的 id,该 id 存储在getValue 中
如何在代码的以下部分输出 json:
var array =
它应该是这样的:
var array = {cmd: '_xclick', business: 'book-facilitator@ya.ru', custom: '5'......};
即,如何在从数据库中获取数据的阶段,将数据通过 分布$row["id"],以便在选中后用特定的getValue ( $row["id"]) 的数据代入所需的变量?
例如像这样:
getValue = какие-то манипуляции для получения массива с данными на основе id лежащим в getValue;
$peremennay = json_encode($row['что-то']);
var array = <?php echo $peremennay; ?>
其中$peremennay;等于从接收到的数据数组形成的 json 字符串{cmd: '_xclick', business: 'book-facilitator@ya.ru', custom: '5'......}
此代码可以在一个 html 表单中更改大量数据,即 我们选择 select,json 将输入的数据排列在表单中,因此无需创建多个单独的表单。如果不使用 jquery,我没有找到更简单的解决方案。
整个代码:
while($row = $results->fetch_assoc()) {
$payment_service = $row["payment_service"];
if ($payment_service == 'PayPal'){
$paypal_item_number = $row["id"];
$paypal_amount = $row["amount"];
$paypal_curr_code = $row["currency_code"];
$validity = $row["validity"];
$paypal_email = $row["paypal_email"];
//var_dump($row);
//echo json_encode($row);
}
}
// Include paypal form
// include __DIR__ . '/forms/paypal.php';
<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post">
<select id="PayPalAmount" required class="form-control">
<option value="">Choose Payment Service</option>
<option value="74">USD 10</option>
<option value="76">RUB 100</option>
<option value="77">RUB 500</option>
</select>
<div class="col-sm" id="select"></div>
<div class="col-sm">
<button type="submit" name="submit" id="js-button">
Buy Now With PayPal
</button>
</div>
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
var buyBtnPayPal = document.getElementById("js-button");
buyBtnPayPal.addEventListener("click", function (evt) {
buyBtnPayPal.disabled = false;
buyBtnPayPal.textContent = "Please wait...";
});
var select = document.getElementById("PayPalAmount");
select.addEventListener("change", function(){
getValue = this.value;
// this в этом контексте - элемент, который запустил фукнцию. То же, что и select.value;
//alert( getValue );
});
$('#js-button').click(function(){
$('#select').empty();
// ТА САМАЯ СТРОКА, ГДЕ НУЖНО ПОЛУЧИТЬ JSON С КОНКРЕТНЫМИ ДАННЫМИ
var array = {cmd: '_xclick', business: 'book-facilitator@ya.ru', custom: '5'......};
$.each(array, function(key, value) {
$('#select').append('<input value="' + value + '" name="' + key + '">');
});
});
</script>
或者,您可以将整个数组
php转换为一个对象js并使用它。为方便起见,可以将 php 数组放在另一个数组中,其中必要的 id 将是键:
然后在js中获取:
选项2:不要立即加载数组,而是在从列表中选择一个值时用ajax加载必要的数据。