用 PHP 创建 .JSON 文件 [英] Creating .JSON file with PHP

查看:17
本文介绍了用 PHP 创建 .JSON 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含 input 和提交 button 的基本表单.我希望将 input 的值转换为 JSON,然后将该 JSON 放在服务器上的文件中以供使用之后.我使用 AJAX 和一个小的 PHP 脚本来处理数据和文件创建,但是 JSON 文件(test.json)是从未创建.

HTML 标记

<button type="submit" value="submit" id="submit">Submit</button>

JS

var submit = $('#submit');var title = $('#title');函数 createJSON() {var jsonObj = [];标题.每个(功能(){var 值 = $(this).val();var item = {};item.title = 值;jsonObj.push(item);});$.ajax({url: "create-file.php",数据: {数据:jsonObj},类型:POST"});}submit.on('点击', function() {createJSON();});

PHP (create-file.php)

JSON

<预><代码>[{title: "页面标题"}]

解决方案

你有 data: {data: jsonObj}, 所以在 php 中它需要是:-

$json = $_POST['data'];

在php页面中添加一些错误报告,以便您获得有关错误的详细信息.当所有错误都解决后,请注释这些行.

像下面这样:-

我已经检查过了,它在我的本地端工作

注意:- 你有 title.each(function() { where title = $('#title');.

将来如果您有多个文本框,则将 id 转换为 class 如下:-

I have a basic form that includes an input and a submit button. I'd like the value of the input to be converted into JSON and then that JSON to be placed in a file on the server for use later. I'm using AJAX and a small PHP script to handle the data and the file creation, however the JSON file (test.json) is never created.

HTML Markup

<input id="title" type="text" name="title" value="Page Title"/>
<button type="submit" value="submit" id="submit">Submit</button>

JS

var submit = $('#submit');
var title = $('#title');

function createJSON() {
    var jsonObj = [];
    title.each(function() {

        var value = $(this).val();
        var item = {};
        item.title = value;
        jsonObj.push(item);
    });

    $.ajax({
        url:     "create-file.php",
        data: {
            data: jsonObj
        },
        type: "POST"
    });   
}

submit.on('click', function() {
    createJSON();
});

PHP (create-file.php)

<?php
    $json = $_POST['data'];
    $info = json_encode($json);
    $file = fopen('test.json','w+') or die("File not found");
    fwrite($file, $info);
    fclose($file);
?>

JSON

[
    {
        title: "Page Title"
    }
]

解决方案

You have data: {data: jsonObj}, so in php it need to be:-

$json = $_POST['data'];

Add some error reporting in php page too so that you will get details about error.when all errors are solved then comment those lines.

Do like below:-

<?php
    //comment these two lines when errors are resolved
    error_reporting(E_ALL);
    ini_set('display_errors',1);

    $json = $_POST['data']; //json need to be data
    $info = json_encode($json);
    $file = fopen('test.json','w+') or die("File not found");
    fwrite($file, $info);
    fclose($file);exit;
?>

I have checked it and it's working at my local end

Note:- you have title.each(function() { where title = $('#title');.

In future if you have more than one text-box then convert id to class like this:-

<input class="title" type="text" name="title" value="Page Title"/>

这篇关于用 PHP 创建 .JSON 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆