【问题标题】:jquery ajax and php arraysjquery ajax 和 php 数组
【发布时间】:2010-06-09 15:34:16
【问题描述】:

我正在尝试向 PHP 脚本提交一些数据,但是 PHP 脚本希望数据以特定格式到达,例如这个示例,它是帖子的转储,

Array ( [save] => Add to shortlist [cv_file] => Array ( [849709537] => Y [849709616] => Y [849709633] => Y ) )

当前的过程是用户使用复选框选择他们想要的产品,然后单击触发 PHP 脚本的提交按钮,

HTML 看起来像这样,

div class="row">

                        <ul>
                            <li class="drag_check ui-draggable">
                                <input type="checkbox" id="inp_cv_849709537" name="cv_file[849709537]" class="cv_choice" value="Y">
                            </li>

                            <li class="id"><a href="/search/cv/849709537">849709537</a></li>
                            <div class="disp">
                            <li class="location">Huddersfield</li>
                            <li class="status">
                            Not currently working                               </li>
                            <li class="education">other</li>
                            <li class="role">
                            Temporary                               </li>
                            <li class="salary">£100,000 or more</li>
                            <div class="s">&nbsp;</div>
                            </div>
                        </ul>
                            <dl>
                                <dt>Current Role</dt>
                                <dd>Developer </dd>
                                <dt>Sectors</dt><dt>
                                </dt><dd>
                                    Energy &amp; Utilities, Healthcare, Hospitality &amp; Travel, Installation &amp; Maintenance, Installation &amp; Maintenance                                    </dd>

                                <dt>About Me</dt><dt>
                                </dt><dd></dd>
                                </dl>
                                <div class="s"></div>
                    </div>

我现在需要使用 AJAX,但我需要以它期望的格式将数据发送到 PHP,这是我目前所拥有的,

$('#addshortlist').click(function() {
var datastring = ui.draggable.children().attr('name')+"="+ui.draggable.children().val()+"&save=Add to shortlist";
alert(datastring);
$.ajax({
    type: 'POST',
    url: '/search',
    data:ui.draggable.children().attr('name')+"="+ui.draggable.children().val()+"&save=Add to shortlist",
    success:function(){
        alert("Success"+datastring);
    },
    error:function() {
        alert("Fail"+datastring);
    }
}); 
return false;

});

非常感谢任何帮助

【问题讨论】:

  • 你能修改你的PHP脚本吗?如果是这样,您可以将数据作为 JSON 传递并在 PHP 脚本中对其进行解码

标签: php ajax arrays jquery


【解决方案1】:

要获得最终的 PHP 输出,您需要如下组装一个对象字面量:

var your_data = {
    save: 'Add to shortlist',
    cv_array: {
        '849709537': 'Y',
        '849709616': 'Y',
        '849709633': 'Y'
    }
};

那么你就可以这样做了

$.ajax({
    type: 'POST',
    url: 'http://example.com/path/to/script.php',
    data: your_data,
    success: function(data){
        $('#debug').html(data);
    },
    dataType: 'html'
});

(顺便说一句,生成输出的基本 HTML 将是):

<form action="http://example.com/path/to/script.php" method="POST" id="myForm">
    <input name="save" value="Add to shortlist">
    <input type="checkbox" name="cv_array[849709537]" value="Y" checked>
    <input type="checkbox" name="cv_array[849709616]" value="Y" checked>
    <input type="checkbox" name="cv_array[849709633]" value="Y" checked>
    <input type="submit" value="Continue &rarr;">
</form>

现在,如果您没有更改表单,您真正需要做的就是进行 ajax 调用,您传递的数据将使用 jQuery 的 serialize() 方法进行组装,该方法汇总了表单的所有内容变成可以传递给服务器端脚本的东西:

$.ajax({
    type: 'POST',
    url: 'http://example.com/path/to/script.php',
    data: $('#myForm').serialize(),
    success: function(data){
        $('#debug').html(data);
    },
    dataType: 'html'
});

【讨论】:

    猜你喜欢
    • 2012-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-13
    • 2011-02-06
    • 2011-09-17
    • 1970-01-01
    相关资源
    最近更新 更多