-1

I am modifying a code example that I will use (and not very familiar with Javascript)

This is the piece of code

function chk() { cnt++;
 var resp=ajax('chk.php', 'POST', 'ordernr='XXXXXX'&r='+((new Date()).getTime()));
 if (resp=='3') {

I am posting to this file withe a Querystring-varaible named ordenr

What is the correct syntax to enter the value of Querystring.ordernr instead of XXXXXX

charlietfl
  • 170,828
  • 13
  • 121
  • 150
Knoelen
  • 33
  • 4
  • 1
    `'ordernr=' + encodeURIComponent(yourJavaScriptVariableName) + '&` etc – ADyson Nov 24 '17 at 16:14
  • If I understand your question correctly this should answer it: https://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript – frontendherodk Nov 24 '17 at 16:17
  • 1
    And probably this https://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call – Alon Eitan Nov 24 '17 at 16:20
  • 1
    Possible duplicate of [How can I get query string values in JavaScript?](https://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript) – Liam Nov 24 '17 at 16:46

1 Answers1

1

var orderNumber = 1;

var requestParamsStr = 'ordernr=\'' + orderNumber + '\'&r=' + ((new Date()).getTime());

console.log(requestParamsStr);

function chk() {
  //cnt++;
  var resp = ajax('chk.php', 'POST', encodeURIComponent(requestParamsStr));
  //rest of code
}

I think I understand your question. If you need to put quotes around the order number then you must use the escape character / the way I'm doing in the code snippet, so that it doesn't delimit the string (or, alternatively, you could use double quotes within the single quotes). You must also use + to concatenate the the strings.

UPDATE: I've encoded the request param string as per @ADyson's comment.

Tom O.
  • 5,730
  • 2
  • 21
  • 35
  • 1
    the parameters could do with url-encoding as well, to avoid problems with special characters etc. We don't know for sure what the content of orderNr will actually be - might not really be a number. Anyway it's just good practice in general. – ADyson Nov 24 '17 at 17:04