0

How to I re-POST data with GET parameters with javascript? (build and append html form and then submit it)

<?php
if($_POST){
    print_r($_POST);
    die();
}
?>

<script>
    //code here to create form, based on GET arguments
    form.submit();
</script>

and page url would be called with many diffrent get arguments like

?a=b&b=c ?ddd=eee&fff=ccc

(only pure js, no jquery or other js frameworks)

ndd
  • 1
  • 1
  • search for how to make html from jquery you will get the answer... – Avi Aug 16 '18 at 13:04
  • 1
    Possible duplicate of [JavaScript post request like a form submit](https://stackoverflow.com/questions/133925/javascript-post-request-like-a-form-submit) – ᴄʀᴏᴢᴇᴛ Aug 16 '18 at 13:07

2 Answers2

0

use the FormData object of javascript and a formData.append() for every field you get from the $_POST;

Example:

var formData = new FormData(form)
<?php
    foreach($_POST as $key => $value) {
        echo "formData.append({$key}, {$value});";
    }
?>
// and then just submit the form...
Anderson Ivan Witzke
  • 1,397
  • 12
  • 24
0

I am not sure if I fully understood you, but here's what I consider to be helpful for you atm. To keep it simple, I assume you have no array children in your GET object. This is possible but makes the code a bit longer and I am short on time right now.

This resends GET data as POST data.

<form id="repost" method="post" action="/target.php">
<?php
   foreach ($_GET as $key => $val)
   {
      // ADD VALIDATIONS AND
      // XSS INJECTION PROTECTION HERE, 
      // E.G. $val = str_replace('"',"'",$val);
      ?><input type="hidden" name="<?=($key)?>" value="<?=($val)?>"><?php
   }
?>
</form>
<script>document.getElementById("repost").submit();</script>
Chris S.
  • 611
  • 8
  • 21