Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery - find last element with a certain class and pass it to php?

Basically, I have a list:

<li class="list">fruit</li>
<li class="list">vegetables</li>
<li class="list">protein</li>

And, I want to find the last list item, get the text from it, and then assign it to a php variable, so that:

<?php

$variable = 'protein';

?>

I'm fuzzy on how to get it into a php variable?

like image 890
n00b0101 Avatar asked Dec 30 '09 23:12

n00b0101


1 Answers

The JavaScript / jQuery Side:

$.post("script.php", { value: $(".list:last").text() }, function (result) {
  alert(result); // alerts whatever comes from the php script
});

The PHP Side

print strtoupper( $_POST["value"] );
like image 187
Sampson Avatar answered Oct 05 '22 04:10

Sampson