Php 在数据库中存储列表项代码

Php 在数据库中存储列表项代码,php,javascript,jquery,Php,Javascript,Jquery,我的结构如下: 当用户单击save按钮时,表单将提交到PHP函数。 我想将12,5,17列表项代码作为数组传递给PHP函数,并使用$\u POST数组将其存储在数据库中。 最好的方法是什么 $.post('url.php', { li1:$("li:eq(0)").text(), li2:$("li:eq(1)").text(), li3:$("li:eq(2)").text()}, function(){ alert('done'); }); 您还需

我的结构如下:

当用户单击save按钮时,表单将提交到PHP函数。 我想将12,5,17列表项代码作为数组传递给PHP函数,并使用$\u POST数组将其存储在数据库中。 最好的方法是什么

$.post('url.php', {
      li1:$("li:eq(0)").text(),
      li2:$("li:eq(1)").text(),
      li3:$("li:eq(2)").text()}, function(){
   alert('done');
});
您还需要解析php中的代码字符串,该字符串将在中提供

$_POST["codes"];

扩展@Adam的答案,要以PHP可以识别为数组的方式传递数据,可以使用以下事实:在HTTP中,可以使用多个值发布同一个键。根据经验,我注意到PHP将以[]结尾的多值键视为数组。因此,您可以使用此未经测试的代码来实现最初的目标:

// Event handler for when you click the button
$("button.save").click(function () {
    var codes = [];

    // For each of your li's with a code attribute
    $("li[code]").each(function () {

        // Stuff the code into an array
        codes.push($(this).attr("code"));
    });

    // >>>>> Set up the post data to be recognized as an array by PHP
    var post_data = [];
    $(codes).each(function() {
        post_data.push({
            name: "codes[]",
            value: this
        });
    });

    // Do a post request to your server resource
    $.post("/path-to-your-php-code/", post_data, function (response) {
        // Handler for successful post request

        alert("The ajax request worked!");
    });

});

您想传递列表项中的数字还是文本?-1。请做一些基础研究。这被称为AJAX,可能是多年来互联网技术发展中谈论最多的话题……谁否决了这个问题?它清楚地解释了所有的要求并征求建议。仅仅因为OP不知道如何理解浏览器HTTP POST接口并不意味着这是一个坏问题。我知道我可以使用AJAX来完成,但我搜索的东西允许我在提交时发送代码数组,而不是单独发送,例如在隐藏数组中存储代码数组,这是最佳做法吗?谢谢您的重播,但是我想在用户单击save按钮提交表单时发送代码数组,因此当用户单击save时,隐藏数组将被发送到函数,该函数将使用$\u post['code\u array\u name']获取数组代码,并将其存储在数据库中,我们可以这样做吗?
$_POST["codes"];
// Event handler for when you click the button
$("button.save").click(function () {
    var codes = [];

    // For each of your li's with a code attribute
    $("li[code]").each(function () {

        // Stuff the code into an array
        codes.push($(this).attr("code"));
    });

    // >>>>> Set up the post data to be recognized as an array by PHP
    var post_data = [];
    $(codes).each(function() {
        post_data.push({
            name: "codes[]",
            value: this
        });
    });

    // Do a post request to your server resource
    $.post("/path-to-your-php-code/", post_data, function (response) {
        // Handler for successful post request

        alert("The ajax request worked!");
    });

});