我有一個 USD->BTC?? PHP 轉換器,用戶將輸入一個美元金額并點擊提交。
它的作業原理是提交<form action="converter.php method="post">
用戶將被重定向到converter.php的表單并查看結果到BTC(見下圖)

我想做的事
我不想將用戶重定向到converter.php頁面,而是將結果發布并顯示到模態彈出視窗中(不重定向)
目前,echo 根據給定的用戶輸入作業,"Your number is: ". $amount;
但不會將此金額轉換為 BTC,“請填寫表格。回傳。 ”在 if else 陳述句之后。

索引.php
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
<script src="//code.jquery.com/jquery-3.5.1.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
<!--form-->
<form id="form" method="post">
<label for="amount">Amount</label>
<input type="text" name="amount" id="amount">
<label for="currency">Currency</label>
<select name="currency" id="currency"><option value="USD">USD</option></select><br>
<button type="button" id="btn" class="btn btn-info" data-toggle="modal" data-target="#myModal">Send Data</button>
</form>
<!--modal-start-->
<div class="modal fade" id="myModal" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel" style="text-align:left; float:left; position:absolute;">Title</h4>
</div>
<div class="modal-body">
<div id="bingo"></div> <!--bingo shows response - whatever written in echo of PHP script.-->
</div>
</div>
</div>
</div>
<!--modal-end-->
<!--AJAX Script to receive response from PHP and put into modal-->
<script>
$(document).ready(function(){
$("#btn").click(function(){
var vAmount = $("#amount").val();
$.post("converter.php", //Required URL of the page on server
{ // Data Sending With Request To Server
amount:vAmount ,},
function(response,status){ // Required Callback Function
$("#bingo").html(response); //"response" receives - whatever written in echo of above PHP script.
$("#form")[0].reset();
});
});
});
</script>
轉換器.php
<?php
//Echo the given number by user
if($_POST["amount"])
{
$amount = $_POST["amount"];
// Here, you can also perform some database query operations with above values.
echo "Your number is: ". $amount;
}
// Make sure the user submitted all of the data required
if(isset($_POST['amount']) && is_numeric($_POST['amount']) && isset($_POST['currency'])) {
// Use curl to perform the currency conversion using Blockchain.info's currency conversion API
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://blockchain.info/tobtc?currency=" . $_POST['currency'] . "&value=" . $_POST['amount']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$conversion = curl_exec($ch);
// Use curl to get current prices and 15 minute averages for all currencies from Blockchain.info's exchange rates API
curl_setopt($ch, CURLOPT_URL, "https://blockchain.info/ticker");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$prices = json_decode(curl_exec($ch), true);
curl_close($ch);
?>
<h1>Conversion Results</h1>
<p><?php echo $_POST['amount']; ?> <?php echo $_POST['currency']; ?> is <?php echo $conversion; ?> BTC.</p>
<?php
// Display the pricing chart if we're doing a US Dollar conversion
if($_POST['currency'] == "USD") {
// Use curl to get pricing chart data for the past 60 days
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://blockchain.info/charts/market-price?showDataPoints=true×pan=60days&show_header=true&daysAverageString=7&scale=0&format= JSON");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$chartdata = json_decode(curl_exec($ch), true);
?>
<?php } ?>
<?php } else { ?>
<p>Please fill out the form. <a href="index.php">Go back.</a></p>
<?php } ?>
uj5u.com熱心網友回復:
你沒有傳遞貨幣.. 你if statements在這里檢查它 -->if($_POST['currency'] == "USD") { 你在這里檢查它 -->if(isset($_POST['amount']) && is_numeric($_POST['amount']) && isset($_POST['currency'])) {
您需要添加附加引數:
$.post("converter.php", //Required URL of the page on server
{ // Data Sending With Request To Server
amount:vAmount , currency:'USD'},
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/365236.html
上一篇:PHP爆炸回圈達到價值
