在ajax数组中使用jQuery .serialize()将PHP $ _POST作为变量传递? [英] Using jQuery .serialize() within ajax array to pass the PHP $_POST as Variables?

查看:243
本文介绍了在ajax数组中使用jQuery .serialize()将PHP $ _POST作为变量传递?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我用来将表单详细信息发送到php函数的jQuery代码:

This is the jQuery code i'm using to send the form details to a php function:

jQuery(document).ready(function($) {    
    jQuery('.submit').click(function(){
        var str = $("#ajaxForms").serialize();
        var data = {
            action: 'myajax-submit',
            serialize: str,
            beforeSend: function(){
                alert('Sending...');
            }
        };  
        jQuery.post(MyAjax.ajaxurl, data,  function(response) {
            alert('Got this from the server: ' + response);
        });
        return false;   
    });
});

这是php函数:

function myajax_submit() {
    $whatever = $_POST['serialize'];
    echo $whatever;
    die(); 
}

一切正常,但是当出现警告框时,文本将显示来自我的html表格#ajaxForms的值的字符串.我相信这是因为php函数会回显$_POST['serialize'].

Everything works, but when the alert box appears the text displays a string of values from my html form #ajaxForms. I believe this is because the php function echos the $_POST['serialize'].

在我的表单中,我有一个输入框,例如:

In my form i have an input box such as:

<input id="postID" name="postID" value="First Name" type="text" />

但是当我尝试在php中回显$_POST['postID']变量时,它不会在警报框中显示任何内容.

but when i try echo the $_POST['postID'] variable in the php it doesn't display anything in the alert box.

我认为通过发送序列化为php函数的表单数据,我可以使用与表单输入相关联的$ _POST变量?

I thought by sending the form data serialized to the php function i could use the $_POST variable associated with the form inputs?

帮助表示赞赏. :)

推荐答案

通过使用jQuery的序列化表单输入序列化,您可以创建一个类似于以下内容的字符串:

By serializing the form inputs with jQuery's serialize you create a string like:

a=1&b=2&c=3&d=4&e=5&postID=10

为了获取postId,您需要对$ _POST ['serialize']进行反序列化.为此,您应该执行以下操作:

In order to get the postId you need to de-serialize the $_POST['serialize']. In order to do so you should do something like:

parse_str($_POST['serialize'], $whatever);

现在要搜索的是$whatever['postID'].

修复parse_str():)

Fixing parse_str() :)

这篇关于在ajax数组中使用jQuery .serialize()将PHP $ _POST作为变量传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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