0

From a php script I'm returning 3 values : all are transitioning over without any difficulty.

if(data.code == 600){
 var PNRRef = data.locator;
 var Lname = data.lname;

What I am trying to do is use these two var values in my redirect link and can't seem to figure it out :

window.location.href = '/US/en/local/res/MyReservation.php?PNRRef="PNRRef"&Lname="Lname"';

But it's not outputting correctly... Would anyone have a tip about how to integrate these ?

Thanks!

asked Mar 8, 2018 at 23:14
2
  • because you are not doing string concatenation, Those " quotes are doing nothing. Commented Mar 8, 2018 at 23:17
  • take care of the variables encoding when replaced in the uri, check below, this saved me a lot of time ;) Commented Mar 8, 2018 at 23:24

4 Answers 4

1

You need either to concatenate:

window.location.href = '/US/en/local/res/MyReservation.php?PNRRef=' + PNRRef + '&Lname=' + Lname;

or use Template literals (also called String interpolation)

window.location.href = `/US/en/local/res/MyReservation.php?PNRRef=${PNRRef}&Lname=${Lname}`;

Those variables need to be encoded (encodeURIComponent(...)) before to make any request like ajax.

answered Mar 8, 2018 at 23:20
Sign up to request clarification or add additional context in comments.

Comments

0
window.location.href = `/US/en/local/res/MyReservation.php?PNRRef=${PNRRef}&Lname=${Lname}`
answered Mar 8, 2018 at 23:18

Comments

0

Probably template literals can be the solution for your issue.Instead of this:

window.location.href = '/US/en/local/res/MyReservation.php?PNRRef="PNRRef"&Lname="Lname"';

Use this:

window.location.href = `/US/en/local/res/MyReservation.php?PNRRef=${PNRRef}&Lname=${Lname}`;
answered Mar 8, 2018 at 23:19

Comments

0

I would go for a simpler solution (string concatenation) that always works

window.location.href = '/US/en/local/res/MyReservation.php?PNRRef='+encodeURIComponent(PNRRef)+'&Lname='+encodeURIComponent(Lname);

If you want to use a better URI encoding compliant to RFC3986 please use rfc3986EncodeURIComponent instead of encodeURIComponent:

function rfc3986EncodeURIComponent (str) { 
 return encodeURIComponent(str).replace(/[!'()*]/g, escape); 
}
answered Mar 8, 2018 at 23:20

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.