如何使用php从输入表单预设数据中获取数据?

我在输入字段内有以下预设数据。我想从“validate-item-blank-quantity”->“minAllowed”中获取 20。

是否可以使用 Php 从中获取价值?

谢谢

    <input type="number" name="qty_custom" id="qty_custom" value="" title="Quantity" class="input-text qty mage-error" data-validate="{"required-number":true,"validate-item-quantity":{"minAllowed":1,"maxAllowed":1000000,"qtyIncrements":1},"validate-item-blank-quantity":{"minAllowed":20,"maxAllowed":1000000,"qtyIncrements":10}}" aria-invalid="true" aria-describedby="qty_custom-error">


慕运维8079593
浏览 74回答 1
1回答

呼如林

您只能发送name和value属性。无论如何,您可以使用简单的 JS 创建新hidden字段并发送其他属性的数据。假设 的值data-validate始终是有效的 JSON,那么应该可以工作:超文本标记语言<form action="yourscript.php" id="myform" method="post">    <input type="number" name="qty_custom" id="qty_custom" value="" title="Quantity" class="input-text qty mage-error" data-validate='{"required-number":true,"validate-item-quantity":{"minAllowed":1,"maxAllowed":1000000,"qtyIncrements":1},"validate-item-blank-quantity":{"minAllowed":20,"maxAllowed":1000000,"qtyIncrements":10}}' aria-invalid="true" aria-describedby="qty_custom-error">    <input type="submit" value="Send"></form><script>    $('[data-validate]').each(function () {        let $el = $(this);        $('#myform').append("<input type='hidden' name='" + $el.attr('name') + '[data-validate]' + "' value='" + $el.attr('data-validate') + "' />");    });</script>PHPyourscript.php<?phpif (!is_null($_POST) && !is_null($_POST['qty_custom']['data-validate'])) {    $arr = json_decode($_POST['qty_custom']['data-validate'], true);    echo "Minimal value is: " . $arr['validate-item-quantity']['minAllowed'], '<br>';    echo "Maximal value is: " . $arr['validate-item-quantity']['maxAllowed'], '<br>';}不要忘记将 jQuery 包含在您的head<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>警告:请记住,如果您想进行一些服务器端验证,则不应通过请求发送它,因为它很容易被操纵,因此可以更改甚至跳过验证。
打开App,查看更多内容
随时随地看视频慕课网APP