0
i have url http://localhost/testo.php?id1=20&qty1=5&id2=13&qty2=4&id3=23&qty3=5.5 and so on with id4 etc etc

and success get the parameter with code like below:

var a = location.href; 
var b = a.substring(a.indexOf("?")+1);

the result value of var b is :

id1=20&qty1=5&id2=13&qty2=4&id3=23&qty3=5.5 

i want to parse this result value of var b and also the index which is 1,2 and 3 into javascript integer/decimal variable so the result can be like below:

id1  = 20
qty1 = 5
id2  = 13
qty2 = 4
id3  = 23
qty3 = 5.5

and the most important thing is how to get the index which is 1,2 and 3

Please help... >.<

Very Thanks :)

Devisy
  • 159
  • 3
  • 12

3 Answers3

0

Your url is kinda non standard: http://localhost/testo.php?id1=20?qty1=5?id2=13?qty2=4?id3=23?qty3=5.5

  • '?' means you have parameters behind
  • '&' should be the parameters separator

Thus your url should be http://localhost/testo.php?id1=20&qty1=5&id2=13&qty2=4&id3=23&qty3=5.5

To parse your GET parameters: How can I get query string values in JavaScript?

Community
  • 1
  • 1
xsenechal
  • 349
  • 1
  • 4
0

You're going to want to use getParameterByName(). The way you're setting it up looks fine:

getParameterByName('id1'); # 20 getParameterByName('qty1'); # 5 getParameterByName('id2'); # 13 getParameterByName('qty2'); # 4 getParameterByName('id3'); # 23 getParameterByName('qty3'); # 5.5

mendokusai
  • 107
  • 7
0
var url = 'http://localhost/testo.php?id1=20?qty1=5?id2=13?qty2=4?id3=23?qty3=5.5';

var result = [];
url.split('?').forEach(function(str, index) {
  if(index === 0) {
    return;
  }

  var arr = str.split('=');
  var nameParts;
  if(arr.length > 1) {
    nameParts = /^([^\d]+)(\d+)$/.exec(arr[0]);
    if(nameParts && nameParts.length > 2) {
      result.push({
        key: nameParts[1],
        index: nameParts[2],
        value: arr[1]
      });
    }
  }
});

console.dir(result);

/* 
    output:
    0   Object { key="id", index="1", value="20"}
    1   Object { key="qty", index="1", value="5"}
    2   Object { key="id", index="2", value="13"}
    3   Object { key="qty", index="2", value="4"}
    4   Object { key="id", index="3", value="23"}
    5   Object { key="qty", index="3", value="5.5"}
 */
kostik
  • 1,179
  • 10
  • 11