0

HTML + JS:

<script> 
    $(document).ready(function() {
        $('#hit').click(function() { 
   var getURL = $('.cena_vozila_avtonet')[0].value;
   console.log(getURL);
    $.ajax({
                    type: "GET",
                    url: "checkprice.php",
                    data: {naslov: getURL},
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (response) {
                        var res = cena_vozila;
      console.log(cena_vozila);
     }
      });
        });
    });
        
</script>
<div id="search">
  <input id="term" type="text" value="enter your search" class="cena_vozila_avtonet" />
  <button id="hit" type="button" name="">Search</button>
</div> 

is not working, with defined checkprice.php, which code is:

<?php
$URL = $_GET['naslov'];

$getURL = file_get_contents($URL);
$dom = new DOMDocument();
@$dom->loadHTML($getURL);
$xpath = new DOMXPath($dom);

$cena_vozila = $xpath->evaluate('//p[contains(@class,"OglasDataCenaTOP")]')->item(0)->nodeValue;
echo $cena_vozila;
?>

What this all should be doing is: Get input from visitor (URL) pass variable to PHP, load DOM and with XPath get a class for div which contains price and return to website.

Now, the part (PHP) which loads DOM elements and gets the price works.

I have issues with AJAX communication between website and PHP. Errors I get in PHP are:

PHP Notice:  Undefined index: naslov in on line 2
PHP Warning:  file_get_contents(): Filename cannot be empty in on line 4
PHP Notice:  Trying to get property of non-object in  on line 9

I understand the values on line 4 and 9. What is wrong on line 2? I`ve specified naslov.

GET/POST <- none of this methods work.

How would I return PHP variable back to site?

Albert Einstein
  • 7,472
  • 8
  • 36
  • 71
  • Have you checked to make sure `getURL` is not empty? – Jay Blanchard Sep 20 '17 at 16:54
  • you use `isset($_GET['naslov'])` like this then it wont throw any kind of error. `if(isset($_GET['naslov'])){ $URL = $_GET['naslov']; // rest of your code }` – RamaKrishna Sep 20 '17 at 16:54
  • Also you do not console.log the response. Assuming the PHP returns proper JSON which I doubt. I do not see any `header("Content-type: application/json");` in the PHP code but try this `$('#hit').click(function() { $.getJSON("checkprice.php",{"naslov":$('#term').val()}, function(response) { console.log(response); }); });` – mplungjan Sep 20 '17 at 16:57
  • ur naslov isnt getting posted in call try {'naslov' : getURL} as you should get error for this in your ajax call itself..check console for any errors..also add "error" handler for ajax to handle if any errors – RohitS Sep 20 '17 at 16:58
  • Try with .val() instead of .value maybe? – Fredrik Andersson Sep 20 '17 at 17:02
  • @FredrikAndersson that does not make any difference. He is accessing the DOM - clumsily but it works – mplungjan Sep 20 '17 at 20:31
  • Yes, I get DOM, I also send it to checkprice.php. Checkprice is also working correctly. If I inspect headers, I am also sending correct values.: naslov:https://www.avto.net/Ads/details.asp?id=12408158 ... I just do not know, why this part in checkprice.php does not work: – saki_theflaki Sep 21 '17 at 10:00

0 Answers0