简体   繁体   English

传递序列化的jQuery数据到PHP,我在做什么错?

[英]passing serialized jquery data to php, what am i doing wrong?

I currently have a form with inputs and name attributes. 我目前有一个带有输入和名称属性的表单。

I'm able to get what I needed through jquery: 我可以通过jquery得到我需要的东西:

 var inputValues= $('.myForm').serialize();

if I alert(inputValues) , I get what I needed, (like form GET variables: categories=examplevalue&name=examplename&email=exampleemail ) 如果我alert(inputValues) ,我会得到所需的信息(例如GET变量形式: categories=examplevalue&name=examplename&email=exampleemail

Now, I'm trying to pass it to PHP with ajax, like so ( ajax.js ) 现在,我正在尝试使用ajax将其传递给PHP,例如( ajax.js

            $.ajax({
                url: "myfile.php",
                type: "POST",
                data: "inputs="+inputValues
            }).done(function(data){
                alert(data);
            });

In my PHP i have this: 在我的PHP我有这个:

if(isset($_POST['inputs'])){
   echo $_POST['inputs'];
}

I assume that it would alert out bunch of variables depending on my form, but it only echo's out the first name variable which is categories 我假设它会根据我的表单提醒大量变量,但它只会回显属于categories名字变量

What am i doing wrong here? 我在这里做错了什么?

Thanks 谢谢

Two things: 两件事情:

  1. assignment of data: 数据分配:

     $.ajax({ url: "myfile.php", type: "POST", data: $('.myForm').serialize() }).done(function(data){ alert(data); }); 

    Prefix field names with with inputs. 带输入的前缀字段名称。 Like <input name="x"/> becomes <input name="inputs[x]"/> . 就像<input name="x"/>变成<input name="inputs[x]"/>

  2. Displaying arrays in php: 在php中显示数组:

     if(isset($_POST['inputs'])){ var_dump($_POST['inputs']); } 

    ( echo would just print Array when its not a scalar value). (当它不是标量值时, echo只会打印Array )。

Update 更新资料

IF, Lior Cohen is right and your server does not recognise the field name notation (not the case with default php settings) then you can use this simple workaround: 如果Lior Cohen是正确的,并且您的服务器无法识别字段名称表示法(默认php设置不是这种情况),则可以使用以下简单解决方法:

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

and then use $POST instead of $_POST . 然后使用$POST代替$_POST Be aware though that $POST won't be super-global. 请注意,虽然$POST不会是超级全局的。

serialize() generates a query string ( foo=bar&baz=yikes ) format that cannot be assigned to a single parameter ( inputs=... in your case). serialize()生成查询字符串( foo=bar&baz=yikes )格式,该格式无法分配给单个参数(在您的情况下为inputs=... )。

In your $.ajax call, remove inputs= and just use inputValues alone. $.ajax调用中,删除inputs=而仅使用inputValues This would allow you to get the individual variables in your form using $_POST['myvar'] . 这将允许您使用$_POST['myvar']获得表单中的各个变量。

Check this out: http://jquery.webspirited.com/2011/02/jquery-serializepost/ 看看这个: http : //jquery.webspirited.com/2011/02/jquery-serializepost/

Sorry about formatting I will fix when on computer 抱歉,格式化后在电脑上会修复

 (function($) {

        $.fn.serializePost = function() {

var data = {};

var formData = this.serializeArray();

for (var i = formData.length; i--;) {

var name = formData[i].name;

var value = formData[i].value;

var index = name.indexOf('[]');

if (index > -1) {

name = name.substring(0, index);

if (!(name in data)) {

data[name] = [];

}

data[name].push(value);

}

else

data[name] = value;

}

return data;

};

})(jQuery);

Just look at what you're sending to the PHP... if you concatenate 'inputvalues=' with the serialzied form, you wind up sending the following to the PHP: 只需看看您要发送给PHP的内容...如果将“ inputvalues =”与序列化形式连接起来,就可以将以下内容发送给PHP:

inputs=categories=examplevalue&name=examplename&email=exampleemail

So, yeah... it is going to be confused. 所以,是的...这将是令人困惑的。 : ) :)

Instead, just do this in the jQuery: 相反,只需在jQuery中执行此操作:

data:inputValues

You'll need to change your acceptance conditions on the post. 您需要在帖子上更改接受条件。

You can also serialize your data into JSON and deserialize it, but this should get you started. 您还可以将数据序列化为JSON并反序列化,但这应该可以帮助您入门。

I do not tested this code but i think it works 我没有测试此代码,但我认为它有效

    var inputValues= $('.myForm').serialize();
    $.ajax({
        url: "myfile.php",
        type: "POST",
        data: inputValues,
        success: function(data){
            alert(data);
            // or use: console.log(data); /*you need firebug to log*/
        }
    });

// and php //和php

if(isset($_POST)){
   var_dump($_POST);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM