【问题标题】:Get JSON data from external form in codeigniter在 codeigniter 中从外部表单获取 JSON 数据
【发布时间】:2014-09-10 10:31:13
【问题描述】:

我有一个将数据发布到我的控制器 URL 的外部 Web 表单。数据以 JSON 字符串形式发送。

我需要做的是获取 JSON 字符串中的各个值并将它们添加到我的数据库中。但是,我在获取发布的值和解码它们时遇到了一些麻烦。

这是我尝试过的代码 - 任何帮助将不胜感激谢谢。

public function index() { 
    $this->load->view('lead');
    $form_data = array(
    'firstname' => json_decode($this->input->post('first_name')),
    'lastname' =>json_decode($this->input->post('last_name')),
    'number' =>json_decode($this->input->post('phone_number')),
    'email' =>json_decode($this->input->post('email')),
    'suburb' =>json_decode($this->input->post('suburb')),
    'state' =>json_decode($this->input->post('state')),
    'enquiry' =>json_decode($this->input->post('enquiry'))
);

// run insert model to write data to db

if ($this->AddLeadModel->SaveForm($form_data) == TRUE) // the information has therefore been successfully saved in the db { //Do something if successful }

【问题讨论】:

  • 请检查 JSON 字符串格式。检查下面的答案。

标签: php json codeigniter


【解决方案1】:

不要对单个表单字段进行 json_decode。 您必须改为使用 json 对传入字段进行 json_decode, 然后使用数组数据再次填充表单。

简单来说:你从字段被塞进 JS 端的一个数组中,然后 json_encoded 传输到服务器。现在您需要扩展 json 以获取数组。

// decode the incomning json 
// you get an array
$json_array = json_decode($this->input->post('the_form_field_name_of_your_json'));

// now assign the array data to the form
$form_data = array(
    'firstname' => $json_array['first_name'],
    ...
    ...
);

【讨论】:

  • 这就是我一直在寻找的东西,需要进行一些细微的修改,但效果很好。谢谢!
【解决方案2】:

试试这个:

$json = file_get_contents('php://input');
$input_data = json_decode($json, TRUE);

【讨论】:

    【解决方案3】:

    将用一个例子来解释(这个工作):

    // Assuming the values you are getting via POST
    
    $first_name = '{"first_name" : "Parag"}';
    $last_name = '{"last_name" : "Tyagi"}';
    $phone_number = '{"phone_number" : "9999999999"}';
    
    $form_data['firstname'] = json_decode($first_name, TRUE)['first_name'];
    $form_data['lastname'] = json_decode($last_name, TRUE)['last_name'];
    $form_data['number'] = json_decode($phone_number, TRUE)['phone_number'];
    
    print_r($form_data);
    


    演示:

    http://3v4l.org/dmIrr


    现在检查下面(这行不通):

    // Assuming the values you are getting via POST
    
    $first_name = "{'first_name' : 'Parag'}";
    $last_name = "{'last_name' : 'Tyagi'}";
    $phone_number = "{'phone_number' : '9999999999'}";
    
    $form_data['firstname'] = json_decode($first_name, TRUE)['first_name'];
    $form_data['lastname'] = json_decode($last_name, TRUE)['last_name'];
    $form_data['number'] = json_decode($phone_number, TRUE)['phone_number'];
    
    print_r($form_data);
    


    演示:

    http://3v4l.org/MeJoU


    说明:

    如果您将帖子中的 JSON 传递给 json_decode,它将失败。有效的 JSON 字符串具有带引号的键。因此,请检查您的情况并查看您以何种格式获取 JSON(通过 POST)。

    【讨论】:

    • 两者之间的(细微)区别在于 JSON 字符串中使用了双引号 (") 和单引号 (')。
    • 是的。这可能是 OP 没有得到其解码值的原因之一。
    • 这可能有效,但我发现最好将表单序列化为 json 并传输该对象,而不是将每个表单字段传输为 json。关键词:“jquery post form serialized” - var form_data = $("#form").serialize(); - api.jquery.com/serializearray.
    猜你喜欢
    • 2017-07-08
    • 2013-11-03
    • 2018-03-25
    • 2015-11-11
    • 1970-01-01
    • 2013-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多