CS Student CS Student - 2 months ago 10
Ajax Question

send two alert data from php back to my script

I have 2 php files :-

first one:=

echo "Connected successfully ";

echo "here";


Second one=

function retrieveData(){
$.post('retrieveData.php',{}, function(data){
window.alert (data)

the problem is here that only one alert value :-
Connected successfully

I need data to alert each time it finds echo in the other
file separately.

in other words I wish I will 2 alerts one with
Connected successfully

and other with

please help


With echo directly it's complicated to send two or more alert data at a time (complicated but feasible).

You can do it in below simple way:-

First page(php page):-


$data = array(); // create an array
$data[] =  "Connected successfully  "; // add all echo data to the array

$data[] =  "heree"; // add all echo data to the array

echo json_encode($data); // encode the array into json and send it back to jqury code as a response


Second Page:-

<script src = "https://code.jquery.com/jquery-3.1.0.min.js"></script>

   function retrieveData(){
        $.post('abc.php',{}, function(data){
            var new_data = $.parseJSON(data); // decode json data and covert it to jquery array

            $.each(new_data, function(index, element) { // iterate over array
               window.alert(element); // alert each element of the array

Output at my local end:-



As you ask in comment do like below:-

$data = array();
if ($result->num_rows > 0) { 
while( $row = $result->fetch_assoc()) {
     $data[] = $row["input"]; // make sure that db table column name is `input`
echo json_encode($data);