Mosarof Hossain
Mosarof Hossain

Reputation: 37

How to retrieve data from html5 output tag into php variable?

I have a output tag in my html code , which is a mathematical calculation from two inputs

<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'];

But itsnt working . any solution ?

Got the solution -_-

Instead of output tag , just use input tag using same id and name . also check the for part

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

Upvotes: 1

Views: 484

Answers (3)

alberto987
alberto987

Reputation: 11

You can read it by:

const alpha = document.querySelector('form.inverse output[name=tot]').value;

Upvotes: 1

Mosarof Hossain
Mosarof Hossain

Reputation: 37

Instead of output tag , just use input tag using same id and name . also check the for part

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

Upvotes: 0

showdev
showdev

Reputation: 29178

Here's one solution using JavaScript to populate a hidden <input> as well as the <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>

Here's an example of what's posted:

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

Upvotes: 2

Related Questions