简体   繁体   中英

HTML Form Action URL - add JS variable to URL

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.

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM