-1

Im new in PHP. Can you tell me how can I get price from span named "wordCounterPrice". I have something like this:

class getPrice {
    public $price;
    public function __construct($data){
        $this->price = addslashes($data['wordCounterPrice']);
    }
}
...
if(isset($_POST['count'])){
    echo '<script>location.href="#test"</script>';
    $price = new getPrice($_POST);
...
}

HTML from PHP (Edit):

<?php
echo '... <span id="wordCounterPrice" name="wordCounterPrice">0</span>
<button type="submit" name="count" id="count" class="btn btn-primary">Get it</button> ...'
?>

So much thanks

Giacomo
  • 341
  • 2
  • 8
  • 25
  • 1
    Does PHP generate the page with the span on it? If not, you will need to download the entire page then extract the text using a DOM parser or regex or similar. – Jacob Mulquin Dec 17 '21 at 10:50
  • 1
    If you want to submit some data from PHP to the server, put it in a form field instead of a span. Take a HTML forms+PHP tutorial to improve your general understanding. – ADyson Dec 17 '21 at 10:57
  • PHP generate my HTML, look my edited HTML code – Giacomo Dec 17 '21 at 12:01
  • If PHP generates the HTML, then why do you need to get the value from the HTML? Get it from PHP before the HTML is even generated…!? – deceze Dec 17 '21 at 12:07
  • PHP generates the HTML but 0 is counted by JavaScript after I complete the other form fields. Do I need to do a PHP function that calculates again the price? – Giacomo Dec 17 '21 at 12:29
  • Please read https://stackoverflow.com/q/13840429/476. – deceze Dec 17 '21 at 12:51

1 Answers1

1

If PHP does not generate your HTML, you could use a Library like paquettg/php-html-parser to gain access to your DOM. for instance you could do this if you use this library:

$dom = new Dom;
$dom->loadFromFile('your-html-file.html');
$price = $dom->find('#wordCounterPrice')[0]->innerHtml;

This is only a recommendation, there are so many other HTML DOM Parser libraries you could use. Except so, there is no other way you can access the content of HTML via PHP, but you can always use javascript.

EDIT

If your HTML is generated by PHP, you could do this with the library instead of loading in an html file:

$html = '
    <span id="wordCounterPrice" name="wordCounterPrice">0</span> 
    <button type="submit" name="count" id="count" class="btn btn-primary">
        Get it
    </button>';

$dom = new Dom;
$dom->loadStr($html);
$price = $dom->find('#wordCounterPrice')[0]->innerHtml;

** EDIT **

If your HTML is dynamically generated by PHP then you should already have access to the wordCounterPrice by reference and there would be absolutely no need to put your server through the rigors of DOM parsing.

3m1n3nc3
  • 353
  • 7
  • 21
  • 1
    PHP generate my HTML =) – Giacomo Dec 17 '21 at 12:01
  • 1
    Check my edits. – 3m1n3nc3 Dec 17 '21 at 12:30
  • Why would you be parsing HTML you have just created? That's overcomplicated and wasted effort. – deceze Dec 17 '21 at 12:52
  • 1
    @deceze What if in his case, the HTML is dynamically generated? – 3m1n3nc3 Dec 17 '21 at 13:10
  • 1
    If it's dynamically generated by PHP, then PHP has the information to dynamically generate the HTML already, so you shouldn't need to read it back from HTML that you've just created. If it's dynamically altered *client-side by Javascript*, then this isn't a job for PHP in the first place. – deceze Dec 17 '21 at 13:40
  • 1
    @deceze Part of my answer has suggested to use Javascript, but for knowledge sake, this could come in handy someday, It might not be a single one line of code... What if that HTML is bieng genrated from the view file of an MVC framework and needs to be handled in a similar fashion, which i know is not his use case, but it could really come in handy to know. – 3m1n3nc3 Dec 17 '21 at 13:48
  • You should first have a really clear understanding of the [difference between the client side and the server side](https://stackoverflow.com/q/13840429/476), which I think is what's primarily missing here. Once you've got that clear, the question itself probably becomes moot. – deceze Dec 17 '21 at 14:08