【问题标题】:html5 number input type step attribute multiplying number with 100html5数字输入类型步骤属性数字乘以100
【发布时间】:2017-11-21 22:18:54
【问题描述】:

下面是简单的代码:

 Amount: <input type='number' step="0.01" min="1" max="500" name='pr_amount' id='pr_amount' /><br>

这个表格是用 PHP 制作的,用于在 MYSQL 数据库中插入数据。 INPUT 1.50 数据库中的 OUTPUT 150.00

如何解决这个问题?我需要在 php 编码部分使用 $pr_amount/100 类型的代码还是有其他方法。如果没有 step="0.01" ,则不采用小数部分或美元的分值。

PHP 部分: payment.inc.php

      if (isset($_POST['pr_amount'], $_POST['pr_processor'],$_POST['pr_comment'])) {
// Sanitize and validate the data passed in
$pr_amount = filter_input(INPUT_POST, 'pr_amount', FILTER_SANITIZE_NUMBER_FLOAT);
$pr_processor = filter_input(INPUT_POST, 'pr_processor', FILTER_SANITIZE_STRING);
$pr_comment = filter_input(INPUT_POST, 'pr_comment', FILTER_SANITIZE_STRING);
$user_id = htmlentities($_SESSION['user_id']);

// for checking minimum payout allowed
         if ($pr_amount < 1) {
    // If it's not, something really odd has happened
    $error_msg_payout .= '<p class="error">Minimum Payout is $1.00, for INR withdraw Minimum payout is Rs.100</p>';
}
   if(empty($pr_amount || $pr_comment)) {
  $error_msg_payout .= '<p class="error">User must input all field</p>';
   }

// checking if pr amount is above the available balance
$stmt_balance = $mysqli->prepare("SELECT current_balance FROM client WHERE user_id = ?");
$stmt_balance->bind_param('i', $user_id);
    $stmt_balance->execute();
    $stmt_balance->store_result();
if($stmt_balance < $pr_amount){
    // If it's not, something really odd has happened
    $error_msg_payout .= '<p class="error">Payment request amount is greater than the available balance.</p>';
}
 $stmt_balance->close();

//-------For checking pr comment validity
     if (strlen($pr_comment) > 150) {
    // If it's not, something really odd has happened
    $error_msg_payout .= '<p class="error">Payment instruction should be within 150 charecters</p>';
}
if (!ctype_alpha(str_replace(' ', '',$pr_comment))) {
$error_msg_payout .= '<p class="error">Payment Instruction Include disallowed charecters</p>';
}

if (empty($error_msg_payout)) {

    // Insert the new account into the database 
    if ($insert_stmt_payout = $mysqli->prepare("INSERT INTO payment (user_id,pr_amount,pr_comment,pr_processor) 
SELECT ?, ?, ?, pr_processor FROM paymentlist WHERE pr_processor = ?")) {
        $insert_stmt_payout->bind_param('idss', $user_id,$pr_amount,$pr_comment,$pr_processor);
        // Execute the prepared query.
        if (! $insert_stmt_payout->execute()) {
            header('Location: ../error.php?err=AddPayment failure: INSERT');
            exit();
        }
    }
    //---deducting balance after succesful request       
    $stmt_deduct = $mysqli->prepare("UPDATE client SET current_balance = (current_balance-?) WHERE user_id = ?");
    $stmt_deduct->bind_param('di', $pr_amount,$user_id);
    $stmt_deduct->execute();
    if (! $stmt_deduct->execute()) {
            header('Location: ../error.php?err=AddPayment failure: INSERT AMOUNT');
            exit();
        }
        $stmt_deduct->close();          
    header('Location: ./payment.php');
    exit();
}

}

HTML 部分: payment.php

<form method="post" name="payment_request" action="<?php echo esc_url($_SERVER['PHP_SELF']); ?>">
        Add Request:
        <?php
         $stmt = $mysqli->prepare('SELECT pr_processor FROM paymentlist '); 
         $stmt->execute();    // Execute the prepared query.
         $stmt->bind_result($pr_processor);
         $stmt->store_result();
         echo "<select name='pr_processor'>";
         while($stmt->fetch()) {
         echo "<option value='" . $pr_processor . "'>" . $pr_processor . "</option>";
          }
         $stmt->close();
         echo "</select>";
         ?> <br>
         Amount: <input type='number' min="1" max="500" name='pr_amount' id='pr_amount' step='0.01' value='0.00' placeholder='0.00' /><br>
          Payment Instruction: <input type='text' name='pr_comment' id='pr_comment' /><br>
          <input type="submit" value="Add Payout Request" name="addpayment" />
    </form>

注意:如果我不使用step,那么它只需要整数并且输入的输出是完全可以的。但我要求输入带小数点的货币价值。

【问题讨论】:

    标签: php mysql html


    【解决方案1】:

    我在https://www.w3schools.com/html/tryit.asp?filename=tryhtml_input_number 上运行了您的 html 代码,提交数据后它显示了正确的数值。

    您是否在将 $pr_amount 添加到数据库之前对其进行处理。请发布您的 php 文件的完整内容。

    【讨论】:

    • 我用完整代码更新了 php 部分,是的,我在将 $pr_amount 添加到数据库之前对其进行处理。请看一下。谢谢
    • 您需要将 FILTER_FLAG_ALLOW_FRACTION 标志添加到您的 filter_input 函数调用中。所以你的过滤器应该是:$pr_amount = filter_input(INPUT_POST, 'pr_amount', FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION);。见此链接:w3schools.com/php/filter_sanitize_number_float.asp
    • 不客气。 ctype_alpha 只允许使用字母。 '。', '&' 不允许。您可以使用 ctype_print 功能。 php.net/manual/en/function.ctype-print.php
    • 嗨。此代码似乎不正确:empty($pr_amount || $pr_comment)。您应该将其替换为:empty($pr_amount) || empty($pr_comment)
    • 问题是你给$stmt_deduct-&gt;execute();打了两次电话,所以是两次扣款。只需删除第一个电话:$stmt_deduct-&gt;execute();
    【解决方案2】:

    试试这个

    <input type='number' min="1" max="500" name='pr_amount' id='pr_amount' step='0.01' value='0.00' placeholder='0.00' />
    

    【讨论】:

    • 我应用了你的代码,但没有运气,数据库中的 1.50 仍然是 150.00
    猜你喜欢
    • 2012-07-29
    • 1970-01-01
    • 1970-01-01
    • 2021-02-24
    • 2022-12-23
    • 2015-09-03
    • 2011-03-17
    • 1970-01-01
    • 2016-11-03
    相关资源
    最近更新 更多