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: _______
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']?>