0

I am working with ajax php i am having a problem i.e

I am passing a javascript list in a ajax request

data: "user_names="+ user_names ; 

i am getting this list on php page like

$participants_names = $_GET['user_names'];

and i am trying to iterate that list like

   for ($i = 0; $i <= $participants_names; $i++) {
      print $i;
   }

but i am not getting the result actualy i am from python and touching this first time in ,php so plz help me out where i am derailing .

5 Answers 5

1

$_GET['user_names'] is a string. You can use explode or split php function to make it an array.

If user_names is a comma-separated list of names :

$participants_names = explode( ',', $_GET['user_names']);
Sign up to request clarification or add additional context in comments.

3 Comments

and what about if i use json_encode() to encode that list in json and then iterate ......?
Why would you want to do that? Json is used as an exchange format between client and server. Once the value has arrived on the server there is no reason to encode it to json before processing it.
ohk actually i was just asking whether it possible or not
1

I think you can't simply pass a list as a parameter, you should serialize it. I suggest you to use JSON, which is supported by most browsers.

In JS pass this as parameter

"user_names="+JSON.stringify(user_names);

and in PHP use this

$participants_names = json_decode($_GET['user_names']);

Comments

0

Do a...

var_dump($participants_names);

... and se what it holds. I'm guessing it's something like a comma separeated list?

$list = explode(',', $participants_names); // Create an array named $list
for($i = 0; $i <= count($list); $i++) {
    echo $i;
}

Comments

0

Try (assuming your user names are split by a semicolon)

$arrayUsers = explode(";", $_GET['user_names']);  
foreach($arrayUsers as $user){
   echo $user;
}

Comments

0
var usernames = ["A", "B", "C"]
data: "user_names="+ user_names.join(",") ; 
$participants_names = explode(",", $_GET['user_names']);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.