Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a JS variable which holds the value of a PHP variable.

<script>
var xxxx = "<?= $v2 ?>";
</script>

I also have a form which points to superman.php

<form action="superman.php" method="get" target="_blank">

I need to amend superman.php so that it holds:

  1. values from elements in the form

  2. and the javascript variable too

How do I achieve this?

Thank you.

share|improve this question

1 Answer

You can append hidden input:

addDataToForm('xxxx', xxxx);

function addDataToForm(name, value){
  var form = document.getElementsByTagName('form')[0];
  var hidden = document.createElement('input');
  hidden.type = 'hidden';
  hidden.name = name;
  hidden.value = value;
  form.appendChild(hidden);
}
share|improve this answer
Where do I paste this? addDataToForm('xxxx', xxxx); Which one of the xxxx is my variable? – user1581579 Apr 19 at 15:01
in script tag. addDataToForm('name', 'val'); is same as writing action of form like this: <form action="superman.php?name=val" method="get" target="_blank"> – karaxuna Apr 19 at 15:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.