在下面的设置中,我有一个简单的输入字段和一个按钮,用户在输入字段中输入一个数字(金额),一旦他们按Submit(提款)按钮,就会出现一个警告对话框(预期)说“您是否要继续$。(来自输入字段的金额)的交易”,那么如果用户单击“确定”,则该金额将传递到php变量,并且数据库交易会发生,如下面的代码所示。问题是首先没有出现警告对话框,因此单击按钮时基本上没有任何反应。
<?php
//Actual values hidden
$dbhost = "xxxxx";
$dbuser = "rxxxx";
$dbpass = "xxxxx";
$db = "xxxxx";
$conn = new mysqli($dbhost, $dbuser, $dbpass,$db) or die("Connect failed: %s\n". $conn -> error);
if(! $conn ) {
echo "Connection failed";
}
?>
<!DOCTYPE html>
<!--This is a simple input box to get user input, It has one text box and
a button-->
<html>
<body>
<form action="doc.php" method="post">
Enter Amount to Withdraw: <input type="number" name="amount" id="amount" required><br><br>
<button onClick="myFunction()">Withdraw</button>
</form>
</body>
</html>
<script>
//To get user input
var amount = document.getElementById("amount").value;
function myFunction() {
if (confirm("Do you want to proceed with transaction of $."+amount)) {
<?php
//User input (amount) passed to php variable
$amount = "<script>document.write(amount)</script>";
$id = "23y4z32";//For specific user
//To get user row in database
$getUser = "SELECT * FROM members WHERE id = '$id'";
$userresult = mysqli_query($conn,$getUser);
//Result of user row is stored in array $userrow
$userrow = $userresult->fetch_array(MYSQLI_NUM);
//To minus users amount user wants to withdraw from his total earning
$hi1=$userrow[8]-$amount;
// mysql query to Update transaction data
$querytransact = "UPDATE members SET bal='$hi1' WHERE id='$id'";
$resulttransact = mysqli_query($conn,$querytransact);
?>
//Php ended above
} else {
//If user presses "Cancel" on the JS dialogue box
alert("Transaction cancelled!");
}
}
</script>