我在compare.php中有一个函数,它接受一个参数$ data并使用该数据从web中查找某些内容并提取数据并返回一个数组。
function populateTableA($data);
所以要填充数组我这样做
$arrayTableA = populateTableA($name);
现在这个数组用于迭代表..
<table id="tableA">
<input type="text" name="search"/><input type="submit"/>
<?php foreach($arrayTableA as $row) { ?>
<tr>
<td><?php echo $row['name']?></td>
<td><?php echo $row['place']?></td>
</tr>
</table>
现在我要做的是在输入上输入一些数据,然后通过jquery ajax输入
function populateTableA($data);
应调用并使用新内容重新填充$ array,然后在tableA上填充而不刷新页面。
我写了这个jquery但没有结果。
$(document).on('submit',function(e) {
e.preventDefault(); // Add it here
$.ajax({ url: 'compare.php',
var name = ('search').val();
data: {action: 'populateTableA(name)'},
type: 'post',
success: function(output) {
$array = output;
}
});
});
我一直在做网络抓取,上面是了解如何实现该策略...我的php文件中的原始函数在下面
function homeshoppingExtractor($homeshoppingSearch)
{
$homeshoppinghtml = file_get_contents('https://homeshopping.pk/search.php?category%5B%5D=&search_query='.$homeshoppingSearch);
$homeshoppingDoc = new DOMDocument();
libxml_use_internal_errors(TRUE);
if(!empty($homeshoppinghtml)){
$homeshoppingDoc->loadHTML($homeshoppinghtml);
libxml_clear_errors();
$homeshoppingXPath = new DOMXPath($homeshoppingDoc);
//HomeShopping
$hsrow = $homeshoppingXPath->query('//a[@class=""]');
$hsrow2 = $homeshoppingXPath->query('//a[@class="price"]');
$hsrow3 = $homeshoppingXPath->query('(//a[@class="price"])//@href');
$hsrow4 = $homeshoppingXPath->query('(//img[@class="img-responsive imgcent"])//@src');
//HomeShopping
if($hsrow->length > 0){
$rowarray = array();
foreach($hsrow as $row){
$rowarray[]= $row->nodeValue;
// echo $row->nodeValue . "<br/>";
}
}
if($hsrow2->length > 0){
$row2array = array();
foreach($hsrow2 as $row2){
$row2array[]=$row2->nodeValue;
// echo $row2->nodeValue . "<br/>";
}
}
if($hsrow3->length > 0){
$row3array = array();
foreach($hsrow3 as $row3){
$row3array[]=$row3->nodeValue;
//echo $row3->nodeValue . "<br/>";
}
}
if($hsrow4->length > 0){
$row4array = array();
foreach($hsrow4 as $row4){
$row4array[]=$row4->nodeValue;
//echo $row3->nodeValue . "<br/>";
}
}
$hschecker = count($rowarray);
if($hschecker != 0) {
$homeshopping = array();
for($i=0; $i < count($rowarray); $i++){
$homeshopping[$i] = [
'name'=>$rowarray[$i],
'price'=>$row2array[$i],
'link'=>$row3array[$i],
'image'=>$row4array[$i]
];
}
}
else{
echo "no result found at homeshopping";
}
}
return $homeshopping;
}
答案 0 :(得分:1)
正如评论中所提到的,PHP是一种服务器端语言,因此您将无法从javascript运行PHP函数。
但是,如果您想要更新tableA
(不刷新整个页面),您可以创建一个新的PHP页面,只创建tableA
而不创建其他内容。然后你可以使用这个ajax调用(或类似的东西) -
$(document).on('submit','#formReviews',function(e) {
e.preventDefault();
$.ajax({
url: 'getTableA.php', //or whatever you choose to call your new page
data: {
name: $('search').val()
},
type: 'post',
success: function(output) {
$('#tableA').replaceWith(output); //replace "tableA" with the id of the table
},
error: function() {
//report that an error occurred
}
});
});
答案 1 :(得分:0)
嗨,你这样做的方式是错误的。你必须改变对html表的回复并覆盖旧表。
success: function(output) {
$("#tableA").html(output);
}
});
在ajax页面中,使用结果数组
创建一个表答案 2 :(得分:0)
你的方向非常错误我的朋友。
首先,JS代码中存在一些语法错误。
所以使用JavaScript Debugging 找到你错的地方。
之后Basic PHP with AJAX 获取ajax和PHP如何协同工作的参考
然后在你的代码
希望这会有所帮助