doulaopu2343 2018-06-18 15:19 采纳率: 100%
浏览 68

如何将表单响应转换为html

Following is my form html code:

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

When I submit the form I get something like this:

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

How do I show response data in following format and show on other page

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

1条回答 默认 最新

  • dongshan3806 2018-06-18 15:27
    关注

    Use your $_POST variable. It's just an array which can be looped, or accessed via individual elements, such as $_POST['message'] and so on. But you have a json string? So to access that you can follow these examples:

    Now you have a json string you could simply:

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

    Then decode the json file into an array:

    $json_array = json_decode($json_string, true);
    

    The json string becomes an associative array:

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

    Which, then can be accessed like this:

    echo $json_array['message'];
    

    Now for what you are trying to achieve simply do this:

    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']?>
    
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度