How should this querystring value be formatted in ajax input [closed] - javascript

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
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

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.

Related

? Incorrect regex expression [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I’ve been trying to do find if a variable contains a digit inside of it. And for some reason, I put (exp.) ‘Majorpassword09’ and it just prints a false?
// vars
var regex = new RegExp(‘.*\d.*’)
var str = ‘Scorpio08’
// main
function reg1() {
if (regex.test(str)){
console.log(‘true’);
}else{
console.log(‘false’);
}
}
reg1();
EDIT: I’ve put the title as Incorrect regex expression because that's the only thing that I think is wrong.
You're most likely on a mac.
Your code shows that you're using fancy quotes. Like these ones: ‘’. You need to use normal quotes, like ', and ".
Here's a solution to your problem.
// vars
var regex = new RegExp(".*\\d.*")
var str = "Scorpio08"
// main
function reg1() {
if (regex.test(str))
{
console.log("true");
}else{
console.log("false");
}
}
reg1();
Whatever text editor you're using, it has fancy quotes on. Consider looking in the settings, or on Google, to find out how to disable them.
as you are using string to create a RegExp Object thus have to use two backslashes to properly escape
var regex = new RegExp(‘.*\\d.*’)
or
var regex = /.*\d.*/

How to Change or replace a part of URL string in javascript? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I have one url which contains special character like
#1# or #2#
http://10.10.10.10:8000/admin/taskinstance/?flt1_dag_id_equals=#1#&flt2_state_equals=#2#
I want to change with some other value like in place of #1# I need "hello" and In place of #2# I need "there", I tried with some code, but it's not working
var str = response.data;
var mapObj = {
#1#:dagId,
#2#:state
};
str = str.replace(/#1#|#2#/gi, function(matched){
return mapObj[matched];
});
In response.data I am getting same URL which I have mention above,
I am getting:
SyntaxError: Invalid or unexpected token for line no 3.
Is something is wrong in syntax?
I am fine with any syntax angularjs or javascript.
You have to change the regex a bit by wrapping strings with parenthesis. Your object is not valid as well:
var str = 'http://10.10.10.10:8000/admin/taskinstance/?flt1_dag_id_equals=#1#&flt2_state_equals=#2#';
var mapObj = {
"#1#":"dagId",
"#2#":"state"
};
str = str.replace(/(#1#)|(#2#)/gi, function(matched){
return mapObj[matched];
});
console.log(str)

Why url query string doesn't pass a variable value? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I am trying to pass a parameter in query string i.e._type=1 but it doesn't pass. It doesn't appear in URL, other values does but not this one. Why ?
SitePaymentReportByBranch = function () {
$('#btnprintSitePaymentByBranch').on('click', function (e) {
e.preventDefault();
if ($("#form1").validationEngine('validate')) {
var _employerID = "";
if ($('#cmbEmployerSitPaymentByParameter :selected').text() == "-Select-") {
alert('Plz Select Employer');
}
var url = '/Reports/frmSitePayment.aspx?_EmployerID=' + $('#cmbEmployerSitPaymentByParameter :selected').val() + '&_Formdate=' + $("#formdate").val() + '&_Todate=' + $("#todate").val() +'_type=1';
commonStartup.openReportWindow(url);
}
});
},
You missed & before _type fix will be '&_type=1'
var url = '/Reports/frmSitePayment.aspx?_EmployerID=' + $('#cmbEmployerSitPaymentByParameter :selected').val() + '&_Formdate=' + $("#formdate").val() + '&_Todate=' + $("#todate").val() +'&_type=1';
Query parameters must be separated with &. You have omitted this for your _type parameter:
'_type=1'
should be;
'&_type=1'

Pass jquery variable to php [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have a question I tried to pass a jquery variable to php but when I look in console I got an error:
SyntaxError: missing ) after argument list):.append('Edit') and I don't understand where is the problem
var items=[];
var link = "<?php echo base_url()?>firm/editFirm/";
$.each(obj, function(i,val)
{
$('#finalResult').text("Results");
items.push($('<li/>').text
(
val.name_firm + "---" +
val.idno+"---" +
val.adresa+ "---" +
val.cont_banca+ "---" +
val.swit+ "---" +
val.banc_name+"---"
).append("<a href='"link+val.idno"'>Edit</a>")
);
});
Help me please.
You've missed the concatenation in the last bit:
append("<a href='"link+val.idno"'>Edit</a>")
Should be
append("<a href='" + link + val.idno + "'>Edit</a>")
I hope you aren't calling the php inside a .js file. I find it's cleaner to retrieve server-side variables using data-attributes inside DOM elements.
In the case of storing URLs for your use in Javascript, I'm really fond of the localize_script function of Wordpress, you just render the variables in a tag in the header or footer via PHP and you have them all available in a nicely separated place.

Javascript eval in function [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I want to make a function that has to generate a random number between 2 numbers and you can save it in a variable. I'm using this code:
function rand(ran1, ran2, randVar) {
var randomNumb = Math.floor(Math.random()*(ran2 - ran1)) + ran1;
eval("var " +randVar+ " = "+randomNumb+";");
}
rand(12, 49, rand1)
alert("Your number is: "+rand1)
The error I get is: Can't find variable: rand1
Can anyone help me?
Using eval for this is entirely unnecessary. I'd recommend something like this instead:
function rand(ran1, ran2) {
var randomNumb = Math.floor(Math.random()*(ran2 - ran1)) + ran1;
return randomNumb;
}
var rand1 = rand(12, 49);
alert("Your number is: " + rand1);
Notice that in calling the function, the only real difference is in where you place the identifier, rand1.
Further Reading
Functions
return

Categories

Resources