2016-08-16 66 views
0

我想通过帖子为基本的管理面板为一个网站的3个变量。通过帖子传递json对象以及其他数据

我有像这样

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

我需要传递下去,以确保人是为了有机会获得用户名和密码的形式。而json是为了传递,所以PHP脚本可以将它写入文件。

我试图在包装btoa(json_file)json_file,并使用base64_decode($_POST["json"])写的,但它总是写的东西不应该“”

+0

为什么不把这样的事情在一个会话?没有必要把他们放在一个网页表格中,他们可以被损坏或篡改 – Machavity

+0

你在这里缺少一个引用:'{“thing”:“stuff”}'。 – ryanpcmcquen

+1

为什么不通过AJAX尝试它并附加用户和密码?一个URL编码? – MikeVelazco

回答

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'意味着该值设置为字符串'json_file',而不是变量的值json_file。您可以执行以下操作来代替:

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