0

I'm looking how I can get <p id="test"></p> as number in PHP. I have select option and with code below I get my variation id selected as html paragraph <p id="test"></p> but I need it as number in PHP to run another PHP code to compere something how can I convert <p id="test"></p> to a number in php without using another java script I just need number form my code below as variable number in php.

I'm using woocommerce plugin in WordPress and I need new way of showing images based on user selected option in single product page instead of switching I need to overlay product image with variation images because of that I have user selected option with code below but I need it to work in php I don't know how to convert <p id="test"></p> this code to php to get number and do my other things I know how to do I just need to get <p id="test"></p> as number in php .

 <?php

 if ( $product->is_type('variable') ) {

      ?>
      <script>
      jQuery(document).ready(function($) {

         $('input.variation_id').change( function(){
            if( '' != $('input.variation_id').val() ) {

               var var_id = $('input.variation_id').val();
               document.getElementById('test').textContent = var_id;
            }
         });

      });
      </script>
      <?php

   }

  ?> 

I need to get instead of this <p id="test"></p> a number in php .

vivanred
  • 13
  • 5
  • What should this number correspond to ? – PHPnoob Jan 19 '20 at 14:24
  • @PHPnoob I'm using WordPress woocommerce plugin and I need to have another way of showing Images because of I should compere my new way of showing Images based on woocommerce variation selected I have with this code what user selected but I need it as number to compere selected with variation images and my product image to have overlay variation images on product image as frame picture instead of switching image between them instead of woocommerce base way of showing image – vivanred Jan 19 '20 at 14:52
  • So you have several p elements which each contain an image, and you want to identify the selected one ? – PHPnoob Jan 19 '20 at 15:10
  • @PHPnoob no . I just have one and need it to convert to php as number variable . and then based on number bring products variation name and image . because I need user selected option for know what user wants for show . I just wnat have image product overlay by variation images based on user selected . – vivanred Jan 19 '20 at 15:53

1 Answers1

0

This question was already answered

Here https://stackoverflow.com/a/3577662/10972786

&

Here https://stackoverflow.com/a/11240083/10972786

First, make sure the server let you fetch the data.

Second, use a html parser instead to parse the data.

// Gets the webpage
$html = @file_get_contents('https://yourwebsite.com/yourpage');

//if no webpage found die and return an error
if (!$html) {
  die('can not get the content!');
}

//Create a new DOM Doc
$doc = new DOMDocument();

//Load the HTML of the page you fetch in the DOM Doc
$doc->loadHTML($html);

//Get the element with the ID test
$content = $doc->getElementById('test');


Note that if the element is empty, the Dom returns NULL

Don't use javascript for that.

You can learn more about DOMDocument on the PHP doc:

https://www.php.net/manual/en/class.domdocument.php

And here's more about the getElementById

https://www.php.net/manual/en/domdocument.getelementbyid.php