首页 > 解决方案 > 如何将 html5 输出标签中的数据检索到 php 变量中?

问题描述

我的 html 代码中有一个输出标签,它是来自两个输入的数学计算

<form oninput="duyd.value = (+tot.value)-(+paid.value);"action="createinvoice.php" method="post" name="my-form">
<output name="tot" for="subtot lasdue" id="tot" ></output>
<input type="submit" name="Submit" value="Submit">
</form>
now i was to use this output data into a php variable . For example 

    $tot = $_POST['tot'];

但它不起作用。任何解决方案?

得到解决方案-_-

而不是输出标签,只需使用使用相同 id 和 name 的输入标签。还要检查 for 部分

 <input type="text" id="duyd" name="dued" for="tot paid">

标签: phphtmlforms

解决方案


这是使用 JavaScript 填充隐藏<input>以及的一种解决方案<output>

var form = document.getElementById('my-form');
form.addEventListener('input', function() {
  this.tot.value = this.tot_display.value = this.subtot.value - this.lasdue.value;
});
<form action="//httpbin.org/post" method="post" id="my-form">
  <input type="text" name="subtot">
  <input type="text" name="lasdue">
  <input type="hidden" name="tot">
  <output name="tot_display"></output>
  <input type="submit" name="submitted" value="Submit">
</form>

以下是发布内容的示例:

{
  "submitted": "Submit",
  "subtot": "5",
  "lasdue": "1",
  "tot": "4"
}

推荐阅读