Alex Parser Alex Parser - 2 months ago 5
Ajax Question

Populating an array through jquery AJAX in php

I have a function in a compare.php that takes a parameter $data and uses that data to find certain things from web and extracts data and returns an array.

function populateTableA($data);

So to fill array I do this

$arrayTableA = populateTableA($name);

now this array is then used to iterate tables..

<table id="tableA">
<input type="text" name="search"/><input type="submit"/>
<?php foreach($arrayTableA as $row) { ?>
<td><?php echo $row['name']?></td>
<td><?php echo $row['place']?></td>

Now what I want to do is to enter some data on input and then through jquery ajax

function populateTableA($data);

should be called and $array should be refilled with new contents and then populated on tableA without refreshing the page.

I wrote this jquery but no results.

$(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;

I have been doing web scraping and the above was to understand how to implement that strategy... original function in my php file is below

function homeshoppingExtractor($homeshoppingSearch)
$homeshoppinghtml = file_get_contents(''.$homeshoppingSearch);
$homeshoppingDoc = new DOMDocument();
$homeshoppingXPath = new DOMXPath($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');

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){
// echo $row2->nodeValue . "<br/>";
if($hsrow3->length > 0){
$row3array = array();
foreach($hsrow3 as $row3){
//echo $row3->nodeValue . "<br/>";
if($hsrow4->length > 0){
$row4array = array();
foreach($hsrow4 as $row4){
//echo $row3->nodeValue . "<br/>";
$hschecker = count($rowarray);
if($hschecker != 0) {
$homeshopping = array();
for($i=0; $i < count($rowarray); $i++){
$homeshopping[$i] = [
echo "no result found at homeshopping";
return $homeshopping;


As mentioned in the comments PHP is a server side language so you will be unable to run your PHP function from javascript.

However if you want to update tableA (without refreshing the whole page) you could create a new PHP page that will only create tableA and nothing else. Then you could use this ajax call (or something similar) -

$(document).on('submit','#formReviews',function(e) {
        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