【发布时间】:2022-01-24 20:13:21
【问题描述】:
我正在尝试使用文本框并使用 JQuery 进行乘法运算,并尝试将值保存到 PHP 变量中。
一个文本框是以美元为单位的金额,我试图将其乘以当地货币汇率。
这里是 HTML 代码
<form action="#" method="POST" id="easyPayStartForm">
<input name="storeId" value="
<?php echo $storeId; ?>" hidden="true" />
<input name="amount" value="
<?php echo $amount; ?>" hidden="true" />
<input name="postBackURL" value="
<?php echo $postBackURL; ?>" hidden="true" />
<input name="orderRefNum" value="
<?php echo $orderRefNum; ?>" hidden="true" />
<input type="hidden" name="expiryDate" value="
<?php echo $expiryDate; ?>">
<input type="hidden" name="autoRedirect" value="
<?php echo $autoRedirect; ?>">
<input type="hidden" name="paymentMethod" value="
<?php echo $paymentMethod; ?>">
<input type="hidden" name="emailAddr" value="
<?php echo $emailAddr; ?>">
<input type="hidden" name="mobileNum" value="
<?php echo $mobileNum; ?>">
<input type="hidden" name="merchantHashedReq" value="
<?php echo $hashRequest; ?>">
<input type="text" id="firstNumber" name="text22" class="form-control ml-1" placeholder="0">
<input type="text" name="newtxt" id="result1" class="amount">
<button type="submit" id="sbt" class="btn btn-primary button">Submit</button>
</form>
jQuery
$(document).ready(function() {
var timestamp = new Date().getTime();
$('#orderref').val(timestamp);
$("#firstNumber").keyup(function() {
is also possible instead of "keyup()"
var input_value = parseFloat($("#firstNumber").val());
if (!isNaN(input_value)) { // the input is a number
var newnum = (input_value * 182);
$("#result").text(newnum.toFixed(1));
//$("#amount").val(newnum.toFixed(1));
$("#result1").val(newnum.toFixed(1));
} else { // the input wasn't a number
$("#result").val("not a number?");
}
});
});
PHP:
ob_start();
ini_set('display_errors', 1);
error_reporting(E_ALL);
var_dump($_POST);
$hashRequest = '';
$hashKey = 'abcede'; // generated from easypay account
$storeId="1234";
$int = filter_input(INPUT_POST, 'text22', FILTER_VALIDATE_INT);
$amount= $int ;
DUMP :
array(13) { ["storeId"]=> string(4) "123" ["amount"]=> string(0) "" ["postBackURL"]=> string(44) "abvbc" ["orderRefNum"]=> string(10) "1640307967" ["expiryDate"]=> string(138) "
试图在此处获取文本框值,但无法显示字符串而不是值和整数
["amount"]=> string(0) ""
谢谢
【问题讨论】:
-
看起来缺少代码。你是通过ajax把它发送到服务器吗?我假设是因为您的 var_dump($_POST) 中没有引用许多表单变量
-
不使用 Ajax。其他变量工作正常,在最后的转储 ["text22"]=> string(4) "10.2" ["optradio"]=> string(2) "on" ["newtxt"]=> string(6 ) "1856.4" } 值是正确的,但不是在实际变量上
-
即使我回显变量,它也会显示正确的值,但是当我使用表单操作 url 继续它时,它会将变量传递到表单。
-
是的,我使用的是 'name' 而不是 'id'
-
$("#amount")将使用id="amount"引用 DOM 元素,但您使用name=amount属性指向元素。将$("#amount").val(newnum.toFixed(1));更改为$("[name=amount]").val(newnum.toFixed(1));或将id="amount"属性添加到您的输入元素