Skip to content
Advertisement

How to combine a input and a hidden value in a form?

I would like to know if there is a way to add a (hidden) value to a input value in a form.

For example, I would like to have a number input like so :

<input type="number" name='questionA' hidden_added_to_the_input_value="+test"> (the code doesn’t work), the user enters ’32’, and the final value $_POST[‘questionA] = ’32+test’

I think i will have to use js to do that, any help appreciate thanks 🙂

Advertisement

Answer

You mean this?

const hiddenValue = 4; // or from a hidden field
document.getElementById("form1").addEventListener("submit", function() {
  const qa = document.getElementById("questionA");
  qa.value = +qa.value + hiddenValue; // convert to number and add
  console.log(qa.value);
})
<form id="form1" action="somephp.php" method="POST">
  <input type="number" name='questionA' id="questionA">
  <input type="submit">
</form>
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement