如何使用php从输入表单预设数据中获取数据?
how to use php to get data from a input form preset data?
我在输入字段中有以下预设数据。我想从“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">
正如 lewis4 在评论中指出的那样,您只能发送 name
和 value
属性。无论如何,您可以使用简单的 JS 创建新的 hidden
字段并发送来自其他属性的数据。
假设 data-validate
的值始终有效 JSON,那应该有效:
HTML
<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>
PHP yourscript.php
<?php
if (!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>';
}
不要忘记在 head
中包含 jQuery
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
警告:
请记住,如果您想进行一些服务器端验证,则不应通过您的请求发送它,因为它很容易被操纵,因此可以更改甚至跳过验证。
我在输入字段中有以下预设数据。我想从“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">
正如 lewis4 在评论中指出的那样,您只能发送 name
和 value
属性。无论如何,您可以使用简单的 JS 创建新的 hidden
字段并发送来自其他属性的数据。
假设 data-validate
的值始终有效 JSON,那应该有效:
HTML
<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>
PHP yourscript.php
<?php
if (!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>';
}
不要忘记在 head
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
警告:
请记住,如果您想进行一些服务器端验证,则不应通过您的请求发送它,因为它很容易被操纵,因此可以更改甚至跳过验证。