繁体   English   中英

JSON object 到 php 变量

[英]JSON object to php variables

I need to convert a JSON object that the php receive from the html form into php variable. 我有两个 forms form1 给出值,form2 提交它们。 这是我给form2 JSON 字符串值的地方

-html--javascript function-

var myObj = {
    "username" : theUsername,
    "name" : theName,
    "surname" : theSurName,
    "email" : theEmail,
    "password" : thePass,
    "confirmpass" : thePass2,
    "dob" : theDate,
    "gender" : theGender,
    "age" : theAge
};

var jsonstr=JSON.stringify(myObj);
document.forms["form2"]["hide"].value = jsonstr; //form2 gets the whole string

-php- //$jstr = $_REQUEST["hide"];

//$newObj=JSON.parse(jstr);

//$name = $newObj.name;

这是我尝试过的,但它不起作用

我在 html 中使用了 post 方法。 我现在如何将其转换回 php 中的单独变量?

看看json_decode

json_decode 的结果是一个关联数组,其中包含 javascript object 中存在的键和值。

如果您在发布到 PHP 脚本后不知道如何获取信息,请查看超全局$_POST 但是,如果您对此不熟悉,我建议您购买 PHP 书或阅读一些教程:)

如果您想要 de JSON 中的字符串索引,请将其发送到 php 页面

var myObj = {
    "username" : theUsername,
    "name" : theName,
    "surname" : theSurName,
    "email" : theEmail,
    "password" : thePass,
    "confirmpass" : thePass2,
    "dob" : theDate,
    "gender" : theGender,
    "age" : theAge 
}

然后在 PHP 页面中:

  extract($_POST);

然后你应该看到你的变量 $name, $surname, $email...

来源http://php.net/extract

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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