Javascript 在php中通过jquery AJAX填充数组
我在compare.php中有一个函数,它接受一个参数$data,并使用该数据从web中查找某些内容,提取数据并返回一个数组Javascript 在php中通过jquery AJAX填充数组,javascript,php,jquery,arrays,ajax,Javascript,Php,Jquery,Arrays,Ajax,我在compare.php中有一个函数,它接受一个参数$data,并使用该数据从web中查找某些内容,提取数据并返回一个数组 function populateTableA($data); 所以要填充数组,我要这样做 $arrayTableA = populateTableA($name); 现在,该数组用于迭代表 <table id="tableA"> <input type="text" name="search"/><input type="submit"
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>
应调用,并且应使用新内容重新填充$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;
}
函数homeshoppingExtractor($homeshoppingSearch)
{
$homeshoppinghtml=文件\u获取\u内容('https://homeshopping.pk/search.php?category%5B%5D=&search_query=“.$homeshoppingSearch);
$homeshoppingDoc=新DOMDocument();
libxml\u使用\u内部错误(TRUE);
如果(!empty($homeshoppinghtml)){
$homeshoppingDoc->loadHTML($homeshoppinghtml);
libxml_clear_errors();
$homeshoppingXPath=newdomxpath($homeshoppingDoc);
//家庭购物
$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”);
//家庭购物
如果($hs行->长度>0){
$rowarray=array();
foreach($hsrow作为$row){
$rowarray[]=$row->nodeValue;
//echo$row->nodeValue。“
”;
}
}
如果($hsrow2->length>0){
$row2array=array();
foreach($hsrow2作为$row2){
$row2array[]=$row2->nodeValue;
//echo$row2->nodeValue。“
”;
}
}
如果($hsrow3->length>0){
$row3array=array();
foreach($hsrow3作为$row3){
$row3array[]=$row3->nodeValue;
//echo$row3->nodeValue。“
”;
}
}
如果($hsrow4->length>0){
$row4array=array();
foreach($hsrow4作为$row4){
$row4array[]=$row4->nodeValue;
//echo$row3->nodeValue。“
”;
}
}
$hschecker=计数($rowarray);
如果($hschecker!=0){
$homeshopping=array();
对于($i=0;$i$rowarray[$i],
“价格”=>$row2array[$i],
“链接”=>$row3array[$i],
'image'=>$row4array[$i]
];
}
}
否则{
echo“在homeshopping中未找到结果”;
}
}
返回$homeshopping;
}
您好,您这样做是错误的。您必须更改对html表的响应并覆盖旧的响应
success: function(output) {
$("#tableA").html(output);
}
});
在ajax页面中,使用注释中提到的结果数组创建一个表。PHP是一种服务器端语言,因此无法从javascript运行PHP函数 但是,如果要更新
tableA
(不刷新整个页面),可以创建一个新的PHP页面,该页面只创建tableA
,而不创建其他内容。然后您可以使用这个ajax调用(或类似的调用)-
你走错了方向,我的朋友 首先,JS代码中存在一些语法错误 所以使用 找出你错在哪里 之后 参考ajax和PHP如何协同工作 然后看看你的密码
希望这将有助于放入您的php文件代码,您是否尝试过console.log(output)将显示任何数据检查更新的问题php是服务器端语言,javascript是客户端,从javascript被执行的那一刻起,php就已经被执行了。你不能用javascript更新php变量。函数
populateTableA
是用php还是用javascript?你有没有试过用echo exit调试你对php文件的ajax调用?请看他的比较。php使用ajax需要做一些更改,所以基本上计划是这样的!我将创建一个新的php文件,该文件只创建表,然后通过ajax调用它,并将其html放在一个分区中。这就是我的建议。
success: function(output) {
$("#tableA").html(output);
}
});
$(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
}
});
});