-1

this is my xml requested page... I want to return the array of values getting from the query to the javascript function call this request

<?php
$id = intval($_GET['val']);

$con = mysqli_connect('localhost','root','','factory');
$aa = 'fact'.$id;
$sql1 = "SELECT table_name FROM information_schema.tables where TABLE_NAME LIKE '$aa%' and table_schema='factory';";
$result1 = mysqli_query($con,$sql1);
foreach ($result1 as $key) {

how to return the array??
 }

?>
Saad Suri
  • 1,352
  • 1
  • 14
  • 26

1 Answers1

0

You can easily achieve this by preparing a application/json response and sending back to the jQuery (presume that's what you use).

First you will need to prepare your data array. To do that, you will need to iterate through the query results, like this.

$dataArray = array();
foreach ($result1 as $row) {
   $dataArray [] = $row;
}

At this point you will have the array loaded with data. Then all you need to do is send back as a application/json response.

header('Content-Type: application/json');
echo json_encode($dataArray);

The calling jQuery function will look like this,

$(document).ready(function(){
    $("button").click(function(){
        $.ajax({url: "submit.php", success: function(result){
            console.log(result);            
        }});
    });
});

The above console.log line will dump you the response came from the PHP and it will look like this.

{ name: "Test Name", description: "Test Desription", date: "22/10/2018"}

Please note that above data is based on the following data array. Only for the demonstration purposes.

array('name' => 'Test Name', 'description' => 'Test Desription', 'date' => '22/10/2018');

I hope this helps, Cheers.

Anjana Silva
  • 8,353
  • 4
  • 51
  • 54