Mallander
Mallander

Reputation: 336

PHP - Get contents of div generated by jQuery

I have the following div that has a number generated by jQuery:

HTML:

<td id="Total" class="total">
</td>

jQuery:

var sum = 0;
$(".item-price").each(function() {
    var NewQuoteTotal = $(this).html().replace('£', '')
    sum += parseInt(NewQuoteTotal);
})
console.log(sum)
$(".total").text('£' + sum)

I need to get the generated number from that element so that I can use it in PHP to be displayed elsewhere.

I've meddled with DOM (Which I don't know):

$dom = new domDocument;
$html = file_get_contents("FILELOCATION"); //I don't use FILELOCATION, that's just a placeholder for StackOverflow.
$dom->loadHTML($html);
$dom->preserveWhiteSpace = false;
$thePrice = $dom->getElementById("Total");
if(!$thePrice) {
    die("Element Not Found!");
}
echo "Element is there!";
$xpath = new DOMXPath($dom);
$divContent = $xpath->query('//td[id="Total"]');
echo $divContent;

And tried to use jQuery to $.post() a variable containing the number but I've had no success.

Upvotes: 0

Views: 84

Answers (2)

Mallander
Mallander

Reputation: 336

I can't believe I didn't think of it earlier, but I just made the variable in PHP rather than jQuery...

Thanks for all the help!

Upvotes: 0

Ohgodwhy
Ohgodwhy

Reputation: 50767

$.post('/path/to/my/php/script.php', {total: $('#Total').text()}, function(){
    //what to do with the returned data from the server
});

And in script.php (as indicated above):

$total = isset($_POST['total']) ?: false;
if($total !== false):
    //you can use $total now
endif;

Upvotes: 3

Related Questions