ahm*_*mad -1 javascript php jquery
我有以下结构:
<ul>
<li code=12>C++</li>
<li code=5>Java</li>
<li code=17>PHP</li>
</ul>
Run Code Online (Sandbox Code Playgroud)
当用户点击保存按钮时,表单将提交给PHP函数.
我想将12,5,17(列表项代码)作为数组传递给PHP函数(带有$ _POST数组)以将其存储在数据库中.
做这个的最好方式是什么?
// 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"))
});
// You can't post an array, so we turn it into a comma separated string
codes = codes.join();
// Do a post request to your server resource
$.post("/path-to-your-php-code/", {"codes" : codes}, function (response) {
// Handler for successful post request
alert("The ajax request worked!");
});
});
Run Code Online (Sandbox Code Playgroud)
您还需要解析php中的代码字符串,该字符串将在
$_POST["codes"];
Run Code Online (Sandbox Code Playgroud)