Drew S Drew S - 3 months ago 9
Javascript Question

pass json object via post along with other data

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 '�'

Answer
<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
    }
}

?>
Comments