如何将表单响应转换为html

时间:2018-06-18 15:19:05

标签: php json

以下是我的表格HTML代码:

<form action="mysite.com/searchNum" method="post">
    <div>
        <label for="say">Number</label>
        <input name="num" id="num" value="">
    </div>`
    <div>
        <button>Send</button>
    </div>
</form>

当我提交表格时,我会得到类似的内容:

{
    "message": "Successfully retrived",
    "error": false,
    "result": [{
        "id": 6128071,
        "name": "jhon doe",
        "number": "31231231230",
        "city": "C",
        "cnic": "8982378237897278",
        "address": "address123",
        "activation_date": "0"
    }]
}

如何以下列格式显示回复数据并在其他页面上显示

(data.html):<br>
Name : ___<br>
Number: ______<br>
Cnic: ________<br>
Address : ________<br>
activation date: _______

1 个答案:

答案 0 :(得分:0)

使用您的$_POST变量。它只是一个可以循环或通过单个元素访问的数组,例如$_POST['message']等。但你有一个json字符串?因此,要访问它,您可以按照以下示例:

现在你可以简单地使用json字符串:

$json_string = '{
    "message": "Successfully retrived",
    "error": false,
    "result": [{
        "id": 6128071,
        "name": "jhon doe",
        "number": "31231231230",
        "city": "C",
        "cnic": "8982378237897278",
        "address": "address123",
        "activation_date": "0"
    }]
}';

然后将json文件解码为数组:

$json_array = json_decode($json_string, true);

json字符串成为关联数组:

Array
(
    [message] => Successfully retrived
    [error] => 
    [result] => Array
        (
            [0] => Array
                (
                    [id] => 6128071
                    [name] => jhon doe
                    [number] => 31231231230
                    [city] => C
                    [cnic] => 8982378237897278
                    [address] => address123
                    [activation_date] => 0
                )

        )

)

然后可以这样访问:

echo $json_array['message'];

现在你想要实现的目标就是这样做:

Name : <?=$json_array['result']['name']?><br>
Number: <?=$json_array['result']['number']?><br>
Cnic: <?=$json_array['result']['cnic']?><br>
Address : <?=$json_array['result']['address']?><br>
activation date: <?=$json_array['result']['activation_date']?>