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>
无效,定义为checkprice.php,代码为:
<?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;
?>
这一切应该做的是:从访问者(URL)传递变量到PHP的输入,加载DOM和XPath得到一个包含价格和返回网站的div类。
现在,加载DOM元素并获得价格的部分(PHP)。
我在网站和PHP之间存在AJAX通信问题。我在PHP中遇到的错误是:
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
我理解第4行和第9行的值。第2行出了什么问题?我指定了naslov。
GET / POST&lt; - 这些方法都不起作用。
如何将PHP变量返回到站点?