Get The Response From Server In JSON Format Without Using Database?
I am new to server side programming, I have a form and action required is, when user fill the name, address and pin code, submit the from then page should be load (like native from
Solution 1:
You can store your JSON string in a local variable server side and have it returned as the result of a client side AJAX call without having to access a database.
Solution 2:
<?php
session_start();
//Variables
$ext = '.json';
$me = isset($_SESSION['me']) ? $_SESSION['me'] : $_SESSION['me'] = rand();
$file = $me . $ext;
//If we have a post handle our data, write to a json file.
if ($_POST) {
//Don't need the submit key in our data.
unset($_POST['submit']);
//Write to the file.
$str = json_encode($_POST);
$fp = fopen($file, 'w') or die("can't open file");
fputs($fp, $str);
fclose($fp);
}
//Check if there is a file with our session name.
if (file_exists($file)) {
//Get the file content and json decode it.
$json = file_get_contents($file);
$values = json_decode($json);
}
?>
<!-- Form with pre-populated values, if they are set -->
<form action=""method="post">
<input type="text" name="name" value="<?php print isset($values->name) ? $values->name : ''; ?>" />
<input type="text" name="address" value="<?php print isset($values->address) ? $values->address : ''; ?>"/>
<input type="text" name="pincode" value="<?php print isset($values->pincode) ? $values->pincode : ''; ?>"/>
<input type="submit" name="submit"/>
</form>
Up to you to make it save tough. But i think that should give you a good idea of the possibilities and how to set this up.
Solution 3:
Maybe what you are looking for is JQuery getJSON, if you don't mind using JQuery library, which I would heavily recommend.
It basically uses AJAX for requesting the json file and parses it:
$.getJSON('https://domain.com/jsonfilelocation/json.json', function(data) {
// 'data' is your object containing the parsed JSON data
var new_name = data.name
var new_address = data.address
var new_pincode = data.pincode
// ...
Read the doc link I give you, it's very well written.
Solution 4:
just request the page https://domain.com/jsonfilelocation/json.json
when doing your request.
Post a Comment for "Get The Response From Server In JSON Format Without Using Database?"