Processing of  multiple variables echoed from php, as java scrip variables.

Ivan Golubar
Ivan Golubar used Ask the Experts™
on
I attached illustration there is result of  code snippets below: string with user names (ivan4joni) and two users avatars .
I have two users : ivan4 and joni. As I have a while loop in php, I get names together as one string.

What would be the code for ajax to get a javascript variable as array [ ivan4 , joni]?
I don't need  users to be displayed as array on main page but as a array which will be then used in js functions.

  $.ajas x({
  method:"POST",
  url: '/wp-content/themes/net4/usersOnMainPageAjax.php',
  data:  {
      "querry2":1,
      "actualproject": whichProjectToSave
   },
   datatype: 'text',
    success: function(strdate){
   
  $('#usersWindowID > p').html(strdate);
     },
     error: function(error, txtStatus) {
      console.log(txtStatus);
      console.log('error');
    }
  });  

Open in new window


<?php
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) {
$partners_username=...............;
$partnersHTML=...............; 
echo $partners_username;
}
echo $partnersHTML;  
?>

Open in new window


In general I need to learn how to get two variables with ajax from php.
partners.png
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
NerdsOfTechTechnology Scientist
Commented:
From the syntax of your script, I presume you are using jQuery.

There are various ways to create the array. One way is by making the datatype: 'JSON' and have your PHP script return a JSON array.

Are you trying to have the PHP process and return a table instead? If so you would output HTML from the PHP script and make the ajax datatype: HTML
This is working.

 success: function(strdate){
   var result1 = $.parseJSON(strdate);
  $('#usersWindowID > p').html(result1[0]);
 $('#usersWindowID > p').html(result1[1]);
     },

Open in new window


echo json_encode(array($partnersHTML,$partners_username));

Open in new window



Now  I may  use each element of array separately.
result1[0]
result1[1]


newVariable= result1[1];
NerdsOfTechTechnology Scientist

Commented:
try this 1-liner:

PHP:
echo json_encode(mysqli_fetch_assoc($query));

Open in new window

Author

Commented:
I have found one example on the net code which helped me to make my own code.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial