-2

I'm trying to insert a variable's value into a url, but it's not working; I'm just getting the variable not the value

'myid' and 'verif' are the variables and their values are integers.

This code inserts the url into a hidden field in a form

$('#return').val(http://pegdown.com/index.php?option=com_joom_creditspack&controller=paypal&view=paypal&layout=return&task=checkout&myid=myid&packid=1&verif=verif&jcode=xxx111xxx);

How do I write the following url so the variables 'myid' and 'verif' are converted to their values?

Jay
  • 57
  • 1
  • 1
  • 6
  • is this url for the same page, you are working on?? – jogesh_pi Nov 22 '12 at 03:38
  • Why not use POST instead,would be easier – dmi3y Nov 22 '12 at 03:40
  • 1
    Your question is answered, but on a related note, your url is 161 characters long as it appears in your question, and that does not take into account the real length of the values of myid and verif. While web browsers and most server software will handle URI's up to roughly two thousand characters, it is a best practice to keep them below 200. As dmi3y pointed out above, you may be better off getting some of the values out of GET here and into POST data, which has less restrictive limits. source: http://stackoverflow.com/questions/417142/what-is-the-maximum-length-of-a-url – Chris O'Kelly Nov 22 '12 at 04:10

4 Answers4

2

Well you are missing quotes so your code would not work at all.

$('#return').val("http://pegdown.com/index.php?option=com_joom_creditspack&controller=paypal&view=paypal&layout=return&task=checkout&myid=" + myid + "&packid=1&verif=" + verif + "&jcode=xxx111xxx");

You should probably use encodeURIComponent()

epascarello
  • 204,599
  • 20
  • 195
  • 236
1

You need to quotes " " the strings and concat the variables +

Try

$('#return').val("http://pegdown.com/index.php?option=com_joom_creditspack&controller=paypal&view=paypal&layout=return&task=checkout&myid="+myid+"&packid=1&verif="+verif+"&jcode=xxx111xxx");
Muthu Kumaran
  • 17,682
  • 5
  • 47
  • 70
1

JavaScript does not support string interpolation. Try something like this.

myIdVal = encodeURIComponent(myId);
verifVal = encodeURIComponent(verif);

var url = "http://pegdown.com/index.php?option=com_joom_creditspack&controller=paypal&view=paypal&layout=return&task=checkout&myid=" + myidVal + "&packid=1&verif=" + verifVal + "&jcode=xxx111xxx";

$('#return').val(url);
maček
  • 76,434
  • 37
  • 167
  • 198
-1

A simple string works for me:

given index = 2,

`a.setAttribute("href", "myDirectory/" + index + ".jpg");` links the anchor to

"myDirectory/2.jpg", ie. the file number is taken from variable index.

Not sure if the setAttribute tolerates multiple tokens in its second parameter, but generally, this works.