从骨干表格帖子解析php数组

时间:2013-06-26 21:59:49

标签: php backbone.js

我将表单数据发布到php并获得下面的响应,如何将此响应转换为可用于设置表单变量并发送电子邮件的内容?

这是调用process.php并发布表单数据

的主干集合
define([
  'jquery',
  'underscore',
  'backbone',
  'models/postModel'
], function($, _, Backbone, postModel){
    var postForm = Backbone.Collection.extend({

    model: postModel,

    url: 'process.php',

    sync: function(method, model, options) {
        var postObj = {
            'name': $('#cmName').val(),
            'email': $('#cmEmail').val(),
            'website': $('#cmWeb').val(),
            'project': $('#cmProject input[type=radio]:checked').val(),
            'message': $('#contactMessage').val()
        }
        method = 'create';
        options.timeout = 100000;
        options.contentType = "application/json",
        options.dataType = "json";
        options.cache = false;
        options.data = JSON.stringify(postObj);
        return Backbone.sync(method, model, options);
    },

    parse: function(response) {
        if(response != null) {
            if (typeof response.data !== 'undefined') {
                this.result = response.data;
            }
        }

        return this.result;

    },
});

return new postForm;
});

来自backbone.js应用程序的表单帖子

{"name":"test","email":"test@test.com","website":"","project":"Web Development","message":"Testing."}

来自php的回复

array(1) {
  ["{\"name\":\"test\",\"email\":\"test@test_com\",\"website\":\"\",\"project\":\"Web_Development\",\"message\":\"$_POST$_POST$_POST$_POST$_POST$_POST$_POST\"}"]=>
  string(0) ""
}

这是php文件的开头:

<?php

parse_str(file_get_contents("php://input"),$_POST);

 $_POST = var_dump($_POST);

//$name =
//$email =

1 个答案:

答案 0 :(得分:0)

试试这个:     

$json = '{"name":"test","email":"test@test.com","website":"","project":"Web Development","message":"Testing."}';

$obj = json_decode($json);

$name = $obj->name;
$email = $obj->email;
$website = $obj->website;
$project = $obj->project;
$message = $obj->message;

?>