我正在使用 jquery 从 html 中的一段数据中获取一个数字,如下所示。
这是 html 行。
echo "<p class = 'bold center remainingland' data-RemainingLandForFacilities = $RemainingLandForFacilities>Remaining Land For New Facilities : " . ($RemainingLandForFacilities) . "</p>";
我使用 jquery 获取数据...
var remainingland = $(this).closest('div').find('.remainingland').attr('data-RemainingLandForFacilities');
(按下按钮后,即“this”)。这可以正常工作,并且警报会显示该数字。
然后我用通常的方式发布到我的 php 脚本......
$.post('scripts/upgradefacility.php',
{
remainingland : remainingland
我用它来收集它。
$RemainingLand = filter_input(INPUT_POST, 'remainingland', FILTER_SANITIZE_STRING);
此时,我确实有数字,但它是一个字符串(由 gettype 确认)。但是当我尝试将其转换为整数时...
$RemainingLand = intval($RemainingLand);
我只是得到一个空白。我应该指出,我只是从我之前传递的变量中复制/粘贴了所有这些,所有这些都正确地通过,所以这真的很奇怪。我只能假设我犯了一些我无法发现的错误,或者我一直在做错事,但到目前为止我以某种方式逃脱了惩罚。
最佳答案
您的问题不是 intval()
无法将数据转换为整数,而是您使用 exit()
返回它。
exit()
接受一个参数。如果它是一个整数,它不会将其打印出来,而是将其用作退出状态代码。
有关 exit()
如何工作的更多信息可以在此处的 php 文档中找到:-
http://php.net/manual/en/function.exit.php
关于javascript - PHP/将数字作为字符串发布,intval无法将其转换为数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42653274/