var input = '';
for (i = 0; i < array.length; i++) {
input += <input name=line_"+i+ " type=text >"+"\n";
}
This cycle create HTML input boxes depending on the length of an array
and saves these in the string "input".
For each of them I assign the name "line_" and the number of index i,
for example the name of first will be line_0, the second line_1 etc.
if (req.url == '/postContent') {
var body = '';
req.on('data', function(data) {
body += data;
});
req.on('end', function() {
var post = qs.parse(body);
for (i = 0; i < array.length; i++) {
fs.appendFile(databaseFile, "line" + i + ": " + post.line_ + "[" + i + "]" + "\n");
}
res.writeHead(302, {
'Location': '/'
});
res.end();
});
Now when there is a post request in one of the input boxes I want to save this in a file. I try to write post.line_+"["+i+"] but the program does not find the name of input boxes in fact the result is this:
riga0: undefined[0]
How can I do to write post.line_i "i" in the sense of variable.
thanks.
Did you try this?
post['line_' + i]
Related
Through ajax response I'm passing array data from controller to blade.
On Ajax success I'm looping through array with 2 elements and concatenating string to display later on in my bootstrap popover.
success: function (data) {
var content = "";
var num = 1;
for (var i = 0; i < data.length; i++) {
content = content.concat(num + "." + " " + data[i]);
num++;
}
$("#content").popover({content: content});
}
Result:
I would like to add new line, so that each item or "artikel" would be displayed in new line e.g. :
1.Artikel...
2.Artikel...
I tried to add "\n" (as below) or html break but nothing works, it only appends as string.
content = content.concat(num + "." + " " + data[i] + "\n");
Use this:
content.concat(num + "." + " " + data[i] + "<br/>");
And this:
$("#content").popover({ html:true, content: content });
Here's a simple loop I'm running:
for (var key in TestApp.config.services) {
if (TestApp.config.services[key].files != "") {
var files = TestApp.config.services[key].files.split(',');
for (var i = 0; i <= files.length - 1; i++) {
var file_url = files[i];
console.log("About to download :" + file_url);
$.getJSON('http://whateverorigin.org/get?url=' + encodeURIComponent(file_url) + '&callback=?', function(data) {
console.log("Downloaded file: " + file_url);
console.log(key);
});
}
}
}
The problem is that the key value is always the same by the time the JSON request finishes. How can I avoid this race condition so that the right key value is used when the $.getJSON is finished?
you need an Immediately-invoked function expression (IIFE):
for (var key in TestApp.config.services) {
if (TestApp.config.services[key].files != "") {
var files = TestApp.config.services[key].files.split(',');
for (var i = 0; i <= files.length - 1; i++) {
var file_url = files[i];
console.log("About to download :" + file_url);
// IIFE
(function(thiskey,this_file_url){
$.getJSON('http://whateverorigin.org/get?url=' + encodeURIComponent(this_file_url) + '&callback=?', function(data) {
console.log("Downloaded file: " + this_file_url);
console.log(thiskey);
});
})(key,file_url);
}
}
}
One simple solution is to simply send the key with the request. I prefer to write it in {} notation.
Let me give you the basis to an answer to your question; I will disregard the part with the files, to emphasize where to look at.
index.php
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
var items = ['Hello', 'World', 'foo', 'bar'];
for (var i=0; i < items.length; i++) {
console.log("About to download :" + items[i] + '" - key: ' + i);
$.getJSON( "ajax.php",
{
url: items[i],
key: i
},
function(data) {
var key = data.key;
console.log("Downloaded file: " + data.url + '" - key: ' + key);
}
);
}
</script>
ajax.php
<?php
echo json_encode(array('url'=>$_GET['url'], 'key'=>$_GET['key']));
?>
I am using my function in onclick nav tabs event (when click on any tab this below function activates). I just want that no same name can twice be inserted into the dropdownlist. Below function is working just perfectly. I just need a check maybe like name.text != arr[i] something like that to prevent it to insert the same name twice in the list. Any help would be appreciated.
js:
<script>
$(".nav-tabs li").click
(
function()
{
var getnumber = document.getElementById("permanentno").value;
var getData = 'methodname=getList&no='+getnumber;
$.ajax
({
type: 'GET',
url: 'Dropdown List/List.php',
data: getData,
success: function(resp)
{
alert(resp); // names for example: mile,stone,
var arr = resp.split(",");
var list = $(".dropdownlist");
var html = "";
for(var i = 0; i < arr.length; i++)
{
var name = arr[i];
if(name.length != 0)
{
html += "<option value='" + name + "'>";
html += name;
html += "</option>";
}
}
$(".dropdownlist").append(html);
}
});
}
);
</script>
You could keep track of the names with another array and IndexOf. Note that for IE<9 support you'll need a shiv to use it.
var names = [];
for(var i = 0; i < arr.length; i++)
{
var name = arr[i];
if(name.length != 0 && names.indexOf(name) == -1)
{
html += "<option value='" + name + "'>";
html += name;
html += "</option>";
names.push(name);
}
}
You can append options to the dropdownlist on the loop, and check repeated names using jQuery like:
var list = $(".dropdownlist");
for (var i = 0; i < arr.length; i++) {
var name = arr[i];
if (name.length != 0 && !list.find("option[value='" + name + "']").length) {
var html = "<option value='" + name + "'>";
html += name;
html += "</option>";
list.append(html);
}
}
I need to print out receipts that inserted into my database, and the latest receipt should be on the top of my page. But when I query my database, I store all the data inside a var s, and naturally it will make the data that was inserted earliest on the top of my page, how do I reverse this?
function querySuccess(tx, results){
if(results.rows.length == 0)
{
$("#ReceiptsList").html("<p>You have no receipts captured yet</p>");
}
else
{
var s = "";
for(var i = 0; i<results.rows.length; i++)
s += "<a href='edit.html?id="+results.rows.item(i).id+"'><strong><font size = 3> " + results.rows.item(i).name +"<font></strong>" + " <font size = 1>Date:" +results.rows.item(i).date + "<font></a> ";
}
//I tried s.reverse(); but didn't work, nothing was showed on the page
$("#ReceiptsList").html(s);
}
Reverse the loop? Start at the end?
for(var i = results.rows.length-1; i>=0; i--)
Or append the beginning of the string, not the end!
Or have the server return the correct order in the first place.
function querySuccess(tx, results){
if(results.rows.length == 0)
{
$("#ReceiptsList").html("<p>You have no receipts captured yet</p>");
}
else
{
var s = "";
for(var i = results.rows.length-1; i>=0; i--)
s += "<a href='edit.html?id="+results.rows.item(i).id+"'><strong><font size = 3> " + results.rows.item(i).name +"<font></strong>" + " <font size = 1>Date:" +results.rows.item(i).date + "<font></a> ";
}
$("#ReceiptsList").html(s);
}
I will briefly explain my issue:
I have a numbers looks like this: 971505896321;971505848963;971505478231;971509856987;
My client should write the above numbers in a text field and I should take the 971505896321 and 971505848963 and 971505478231 and 971509856987 and add them into a list.
I success now to add the numbers to the list but I have difficulties how to get the numbers without ;.
function AddPhoneNo()
{
var recipientNumber = document.smsmobile.mobile_no;
var opt = "<option value='" + recipientNumber.value + "'>" + recipientNumber.value + "</option>"
if (recipientNumber.value != "")
{
if(verifyPhone(recipientNumber.value))
{
$('#selectedOptions').append(opt);
recipientNumber.value = "";
}
}
}
All the numbers should start with 971 and the length of each number is 12. For example: 971506987456.
Appreciate your help.
Thanks,
var recipientNumber = "971505896321;971505848963;971505478231;971509856987;";
var mobileArr = recipientNumber.split(";");
and then u can run a loop on the above array and add those to your options
You just need to replace ; if I m getting you corretly than code for you is
var recipientNumber = document.smsmobile.mobile_no.replace(';','');
EDIT
if you are entering all number one than you need to split out your string
var recipientNumber = "971505896321;971505848963;971505478231;971509856987;";
var arrayofNumbers= recipientNumber.split(";");
var i; for (i = 0; i < arrayofNumbers.length; ++i)
{
var opt = "<option value='" + arrayofNumbers[i]+ "'>" + arrayofNumbers[i]+ "</option>"
if (arrayofNumbers[i] != "")
{
if(verifyPhone(arrayofNumbers[i]))
{
$('#selectedOptions').append(opt);
}
}
}