0

私はサーバー側のプログラミングに慣れていません。form必要なアクションは、ユーザーが名前、アドレス、ピンコードを入力し、送信元からページをロードする必要があり (送信からネイティブのように)、新しいデータを表示する必要があることです (データはJSON ファイルで利用可能)。

サーバーに JSON ファイルがあります。データベースを使用せずにJSON形式でサーバーからの応答を取得することは可能ですか??

HTML:---

<form action="https://domain.com/jsonfilelocation/json.json">
    <input type="text" name="name" />
    <input type="text" name="address" />
    <input type="text" name="pincode" />
    <input type="submit" name="submit" />        
</form>

JSON:--- json.json にあるhttps://domain.com/jsonfilelocation/json.json

{
    "name": "kk",
    "address": "XYZ, New Delhi",
    "pincode": "1000001"
}
4

4 に答える 4

1

おそらく、あなたが探しているのはJQuery getJSONです.JQueryライブラリの使用を気にしないのであれば、これを強くお勧めします.

基本的に、JSON ファイルを要求して解析するために AJAX を使用します。

$.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
    // ...

私があなたに与えるドキュメントリンクを読んでください、それは非常によく書かれています。

于 2013-09-14T17:21:18.083 に答える
1
<?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>

それをタフに保存するのはあなた次第です。しかし、これで可能性とこれを設定する方法についての良いアイデアが得られるはずです。

于 2013-09-14T17:27:02.793 に答える
1

JSON 文字列をサーバー側のローカル変数に格納し、データベースにアクセスすることなく、クライアント側の AJAX 呼び出しの結果として返すことができます。

于 2013-09-14T17:00:57.150 に答える
0

https://domain.com/jsonfilelocation/json.jsonリクエストを行うときにページをリクエストするだけです。

于 2013-09-14T17:04:13.150 に答える