Javascript array in SQL prepared statement - javascript

Here is what I am trying to do. I have a simple piece of java script code which takes the value of a checkbox and deletes the corresponding from the database.. Say that I have three options red, blue, yellow displayed and red is selected, the record for red will be deleted.
This code does it perfectly.
var n = $('input[type=checkbox]:checked').val();
db.transaction(function (tx) {
tx.executeSql('delete from details where name = ?',[n],function(){
alert('The event is successfully deleted');
});
});
But the requirement is to capture multiple checkbox selections as well (i.e) if both red and blue are selected or all the three are selected, the code must deleted the records of the selected values. I am able to capture the values selected in an array with the following piece of code.
var checkboxValues = $('.myCheckbox:checked').map(function() {
return $(this).val();
}).get();
Now i need to use this array of strings in a SQL statement. I tried the following piece of code but of no use.
db.transaction(function (tx) {
tx.executeSql('delete from details where name in (?)',[checkboxValues],function(){
alert('The event is successfully deleted');
});
A few other answers suggested me to convert the array object into a comma-seperated string using toString() and join() function but those did not quite help me. I doubt whether my SQL statement is correct. Pls someone help me. Thanks in advance !!

Maybe you could turn the function around so that it iterates over the returned values and preforms the db function for each in turn. Something along the lines of:
$(checkboxValues).each(function(tx){...
Or you could consider preforming the SQL update as each value is found within the first function, as long as you are throwing security out the window. Then you would only need to include the value as a this and you could lose the variable.

Related

How to convert a Data Layer Variable from number to string in Google Tag Manager

I am implementing a custom HTML tag in GTM that should return certain values in the data layer. However, I’d like to encode one of the values as text.
I have a customer status variable that keeps track of how many orders a user has placed. I’d like to set up a JavaScript function that assigns a value of ‘New Customer’ when the customer status count = 0, and ‘Return Customer’ if the count is > 0.
Here is the code I have so far:
var returnCustomer = {{Shopify - Transaction - CustomerStatus}};
var returnStatus = returnCustomer(function(){
if (returnCustomer > 0) {
return 'Return Customer'
} else {
return 'New Customer'
}
});
I know this isn’t quite right-- anyone have ideas on how to solve this problem?
I won't comment in particular on your code (you call returnCustomer as a function - are you really sure {{Shopify - Transaction - CustomerStatus}} returns a function? Also your if clause does the opposite of what you describe in the text) because GTM has a built-in way to solve this without hand written code.
It's called a lookup table, and it allows you to return an output value depending on the value of an input variable.
Your requirement was:
I’d like to set up a JavaScript function that assigns a value of ‘New
Customer’ when the customer status count = 0, and ‘Return Customer’ if
the count is > 0.
So you have an input variable that's either 0 or 1, and you want to return a text output based on those values.
Go to Variables, New, and from the utilities sections chose "lookup table". I have named my Variable "My Output", you will probably use a better name.
Choose your input variable - I presume that's "{{Shopify - Transaction - CustomerStatus}}" in your case, I have named mine "My Input" for demonstration purposes.
Now click the "add row" buttons to add two rows. Each row will have two input fields.
Into the left hand side go the expected values from the input. Right hand side maps an output value to the input value.
Now, if "{{My Input}}" has a value of "0", "{{My Output}}" will assume a value of "New Customer". If it's "1", "{{My Output}}" will assume a value of "Return Customer". If it's neither, "{{My Output}}" will be undefined (you can set a default value, though).

insert two different msg.payload in a database

I want to do the query below into a function:
insert into table values (msg.payload.1, msg.payload.2);
this function has two input (2 different msg.payload)
so the code that I am trying to write is:
var m={
topic: "insert into info values ('"+msg.payload+"','"+msg.payload+"');"
};
return m;
it is adding two rows in the info table.
but want to add only one row with two different values.
any idea?
Thanks in advance
If I've understood properly you have 2 different nodes feeding data into this function and you want to use the values from these separate messages to build your insert statement.
This is not how Node-RED works, each message is handled totally separately from each other (hence why you see 2 rows added to the DB).
If you want to combine 2 separate messages you will need to make use of the context to keep state. You will also need a way to identify which message is which when it comes in, this is normally done by message topic, but it could be any unique feature.

Record answer randomization in Qualtrics using javascript

I am making a survey in Qualtrics. This survey has a repeating question with six answer choices. The six choices are randomized (in the standard way, no javascript). The question is being repeated using loop&merge, which works great because it's the same question structure over and over (36 times), but I can use the field function to adjust the question and answers for every iteration.
However, one problem I am running into is that Qualtrics does not (as standard) support the recording of the randomization data in the results - i.e. how it has randomized the six answer choices in each iteration. When I use the 'Export Randomized Viewing Order data' function when downloading results, it only shows the answer order of the last time it asked the question. So it seems that this is a value that gets overwritten after each iteration.
So now I'm looking to record the answer order for each iteration through javascript. However, I haven't found a function that gives the order answer (after randomization). I have consulted the Qualtrics javascript API and found some functions that seem promising, such as getChoices (), but when I try this all I get back is the order of answers without randomization (i.e. just 1,2,3,4,5,6).
Does anyone know a way to record the randomized choice order for each iteration, using javascript or otherwise?
I found a different way to record the loop and merge randomization order.
Create an embedded data field in survey flow. Here we will record the randomization order. I will call the field rand_order.
Add a loop and merge field with a unique number to identify each loop (e.g. 1, 2, 3, 4, 5, ..., n).
Then add the next javascript to any page of the looped block.
//*Place Your Javascript Below This Line*/
var questionText = "${lm://Field/1}"; // "${lm://Field/1}" will actually evaluate to
//whatever is Field 1 in the current Loop & Merge loop.
// You can do this with embedded data too, as seen in the next line
var order = "${e://Field/rand_order}" + "|" + questionText; // gets the value of the embedded data
// field "rand_order", concatenates the current loop's identifier to it,
//and stores that as a variable
Qualtrics.SurveyEngine.setEmbeddedData('rand_order', order); // updates the
//embeddeddata field "rand_order" to be our order variable, which has the current loop's
//identifier attached, effectively constructing a string of numbers representing the order
You will get a column with the name "rand_order" filled with "1|5|23|2...|n". You can change the separator to make more compatible with whatever script you are using to manipulate data.
Qualtrics already records this information for you. It's just a matter of explicitly asking for it when you download your data. Number 5 on this page has more info, but I'll recount the important bits:
In the “Data & Analysis” tab, click “Export & Import” and then “Export Data”.
In the “Download Data Table” window click “More Options”.
Check the box for “Export viewing order data for randomized surveys”.
I think the thing here is to look at the order of choices in the DOM. Qualtrics provides the getChoiceContainer() method to get the div containing the choices. Here's a snippet I wrote and minimally tested:
//get the div containing the choices, then get all input child elements of that div
var choices = this.getChoiceContainer().getElementsByTagName("input");
//initialize an array for the IDs of the choices
var choiceIDs = []
//add the ID of each choice to the array
for (var i=0; i < choices.length; i++) {
choiceIDs.push(choices[i].id);
}
//get the current choice order from embedded data and add this loop to it.
//Add a | to distinguish between loops.
var choiceOrder = "${e://field/choiceorder}" + choiceIDs.toString() + "|";
//set the embedded data with the new value
Qualtrics.SurveyEngine.setEmbeddedData("choiceorder", choiceOrder);
A couple of notes/caveats:
I only tested this on a basic multiple choice question with radio buttons. It may need to be adjusted for different question types.
I also just got the IDs of the question choices. You could probably modify it pretty easily to get other information, like the label of the choice, or the numeric value it corresponds to.

Getting a particular row's attribute value?

I have this function which I believe is following this process:
function verify(){
$.get("map_process.php", function (data) {
verified = $(data).find("marker").eq(-1).attr('verification');
});
}
Get data from php file/db
In the db, find the table "marker"
Find the last record in the table marker
Assign the value of the 'verification' column to the variable verified
This is doing what I want (kind of) but I need to be able to specify what record to get the 'verification' value from, but not by it's position in the table (as more records will be added and the above will just get the last record regardless). Is there another method that is kind of like .eq(x) but will allow me to specifically select a record based on another attr in that record.
eg. Say I want to find the verification value for record 1 through an event listener, and then find the verification value for record 6 through a different event listener.
I have a variable which can distinguish what row I want to get, but how can I incorporate this into the statement above. (i'm thinking instead of .eq(-1)
You can use filter() which can contain as many conditions as you need.
$.get("map_process.php", function (data) {
var myVariable = $(data).find("marker").filter(function(elementIndex, element){
return $(this).attr('someOtherAttribute') === 'valueWanted';
}).attr('verification');
});
Since I'm really not sure what the data looks like or what attribute you need the above is only a guess at how you would need to implement
See filter() API docs

SuiteScript: How does the dynamic mode of nlobjColumn.setURL work?

In NetSuite, I have a scripted search of transactions that is expected to return results of several different transaction types. The results are then rendered in an nlobjList. I would like one of the columns of said list to be a link to the transaction that the list row represents.
In all NetSuite examples, this is accomplished something like:
var column = list.addColumn('number', 'text', 'Number', 'left');
column.setURL(nlapiResolveURL('RECORD','salesorder'));
column.addParamToURL('id','id', true);
Unfortunately, transaction is not an acceptable record type to pass to nlapiResolveURL, so I would need to dynamically detect the record type for each row. The setURL function does accept a second Boolean parameter that makes it dynamic per row, but I am not sure how this actually works. There are no examples, and the JSDocs do not explain its usage.
Does anyone have any guidance on generating a list with dynamic URLs in NetSuite?
If you set the dynamic argument to true, then the first argument should be a column listed in the data source that will contain the base URL.
column.setURL('base_url', true);
column.addParamToURL('id','id', true);
Then, on each record of your results, make sure you have a base_url that is set to the url you are looking for.
Note, the following example assumes a regular javascript object instead of the search result object.
rec.base_url = nlapiResolveURL('RECORD', rec.type)
Transaction field is just an abstraction for all transaction types. You can search them but can't load them.
The field you need to retrieve is recordtype. Sample code is below.
var recs = nlapiSearchRecord('transaction',null,null,new nlobjSearchColumn('recordtype'));
for(var i in recs)
url = nlapiResolveURL('RECORD',recs[i].getValue('recordtype'));

Categories

Resources