简体   繁体   中英

Getting all list items of an unordered list in PHP

I have these tag cloud list

<ul #id = "tag-cloud-list">
    <li class = "items">Music</li>
    <li class = "items">Lion</li>
    <li class = "items">Dwarf</li<
</ul>

That was generated by JQuery. The items here are tags that I will insert to the database. Now my question is it possible to get all <li> items inside the ul? and put them inside a PHP array? because each of these tags will be inserted in the database. if there's no way I can get a li items using php then how would Insert these list items into the database? because I needed them since it is tag to a certain item or post

You have to make an AJAX call:

var lis = new Array();

// Iterate through the <li> items
$("#tag-cloud-list").children("li").each(function()
{
    lis.push($(this).text());
});

// Make AJAX call and set data to something like "Music::Lion::Dwarf"
$.ajax({
    url: "dostuff.php",
    type: "POST",
    data: { items: lis.join("::") },
    success: function() { alert("OK"); }
});

Then in the PHP script, use

$lis = $_POST['items'];
$liarray = explode("::", $lis);

to retrieve an array of the li items

You can't get these in PHP unless you send the data back to the server somehow. PHP executes on the server, if this is coming in via a jquery, then PHP knows nothing about it.

You will need to either use javascript to get all the information and send it back to the server, or you need to send the entire page HTML (somehow) back to the server to be looked at by PHP. I suggest the first option.

Edit: I am not flash-hot with javascript, just use some basic functions, but you could write an event into a <div> or where the data is coming into along. The function would find how many <li> there are in the <ul id="tag-cloud-list"> pop them into an array and send them to the PHP server, by a form or ajax query.

Sorry that I can't help much with the js code.

您需要使用JQuery的.find()函数获取ul中的所有项目,并使用ajax进行POST并处理信息

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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