php - Separator data received via post request -


my problem is: through post request variable "data" in javascript, this:

var data = name1 text1 name2 text2 name3 text3; 

how divide each data element in variable this:

var group1 =  name1 - text1; var group2 =  name2 - text2; var group3 =  name3 - text3; 

then decided put separator in php, character "-" in each group (variable) form, , deal javascript function "split()" string. problem in text there "-" character , other character, how saparare in example without using json? control both server side (php) or client-side (javascript).

i'm going make wide assumption php code looks like, since conveniently left out. imagine looks (off top of head, may not work):

$response = ''; mysql_query('select name, text mytable'); while ($row = mysql_fetch_assoc()) {     $response = $row['name'] . ' ' . $row['text'] . ' ' .$response; } echo $response; 

you should instead respond object this:

$response = array(); mysql_query('select name, text mytable'); while ($row = mysql_fetch_assoc()) {     $response[$row['name']] = $row['text']; } echo json_encode($response); 

then, in javascript, this:

var data = {"name1": "text1", "name2": "text2", "name3": "text3"}; 

and can create groups this:

var groups = []; (var name in data) {     groups.push(name + " - " + data[name]); } 

to create array of groups wanted.


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

c# - must be a non-abstract type with a public parameterless constructor in redis -

ajax - PHP/JSON Login script (Twitter style) not setting sessions -