0

I am trying to pass 3 variables through post for a basic admin panel for a site.

I have a form like so

<script>
  var json = {"thing":"stuff"};
  var json_file = JSON.stringify(json);
</script>

<form action='load.php' method='post'>
    <input type='hidden' name='username' value='<?php echo $_POST["username"]; ?>' />
    <input type='hidden' name='password' value='<?php echo $_POST["password"]; ?>' />
    <input type='hidden' name='json' value='json_file' />
    <input type='submit' value='Submit' />
</form>

I need the username and password passed along to make sure the person is meant to have access. And the json is meant to be passed on so the PHP script can write it to a file.

I tried wrapping json_file in btoa(json_file) and using base64_decode($_POST["json"]) to write, but its always writing things it shouldn't '�'

Drew
  • 1,171
  • 4
  • 21
  • 36
  • Why not put things like that in a session? No need to put them in a web form where they can get corrupted or tampered with – Machavity Aug 16 '16 at 23:46
  • You are missing a quote here: `{ "thing": "stuff" }`. – ryanpcmcquen Aug 16 '16 at 23:46
  • 1
    why don't you try it via AJAX and append the user and password? a URL encoding? – MikeVelazco Aug 16 '16 at 23:47
  • http://stackoverflow.com/questions/4226480/ampersand-character-inside-a-value-of-jquery-ajax-request-data-option this question has a clean accepted solution which may help you. – Niloct Aug 17 '16 at 00:11

2 Answers2

1
<script>
  var json = {"thing":"stuff"};
  var json_file = JSON.stringify (json);
</script>

<form method='post' onsubmit="this.json.value = json_file;">
    <input type='hidden' name='json'/>
    <input type='submit' value='Submit' />
</form>

<?php

if (isset ($_POST['username'], $_POST['password'])) {
    if ($_POST['username'] == 'admin' && $_POST['password'] == 'password') {
        // do something
    }
}

?>
0

value='json_file' means that the value is set to the string 'json_file', not the value of the variable json_file. You can do the following instead:

<script>
  var json = {"thing":"stuff"};
  var json_file = JSON.stringify(json);
  window.onload = function() {
    document.getElementsByName('json')[0].value = json_file;
  };
</script>

<form action='load.php' method='post'>
  <input type='hidden' name='username' value='<?php echo $_POST["username"]; ?>' />
  <input type='hidden' name='password' value='<?php echo $_POST["password"]; ?>' />
  <input type='hidden' name='json' />
  <input type='submit' value='Submit' />
</form>
csander
  • 1,385
  • 10
  • 11