Saving dynamic fields to the database - javascript - javascript

I want to save the text and select inputs I have added with Append to the database. How do I get the value of the dynamically added select and input fields?
http://jsfiddle.net/qBURS/1848/
function register() {
var count = document.getElementById("buildyourform").childElementCount;
for (var i = 0; i < count; i++) {
}
alert(count);
}

using $( 'form').serializeArray() you can get array of input and select values
Demo : http://jsfiddle.net/qBURS/1849/
$('#register').click(function(){
var x = $("form").serializeArray();
$.each(x, function(i, field){
$("#results").append(field.name + ":" + field.value + " ");
});
});

Related

If-Else not working as expected if found matching element in array

I have a form where user can fill details and on clicking of add attendee button can add up to 9 people details. I am getting value of user name, selected checkbox item id and item quantity and storing inside array and passing these values in URL to add selected items and created user. On first click I'm checking if item is already there in array.
On first click I'm adding id's and quantity in array what I want to achieve is to increase quantity of that id which is already present in array, each time i click on this button it checks array and update quantity if its already present but its not working.
Here is my code
jQuery("#btnadd2").click(function() {
var qty;
var allVals = [];
var nameatt = jQuery("#txtAttendeeNames").val();
var nameatt= jQuery("#txtAttendeeNames").val();
var emattendee= jQuery("#txtAttendeeEmails").val();
var eventname = jQuery("#hdEventName").val();
var eventlocation = jQuery("#hdLocation").val();
jQuery(".big:checked").each(function() {
qty = parseInt(jQuery(this).parents().nextAll().children().find('input[name="quantity"]').val());
if (jQuery.inArray(jQuery(this).val(), allVals) == -1) {
allVals.push(jQuery(this).val()); //not present in array
} else {
qty = parseInt(jQuery(this).parents().nextAll().children().find('input[name="quantity"]').val() + 1);
}
});
for (var i = 0; i < allVals.length; i++) {
var rslt = allVals.join(',' + qty + ',' + 'custcol_name_of_attendees|' + nameatt + '||custcol_email_o f_attendees|' + emattendee + '||custcol_event_name|' + eventname + '||custcol_event_location|' + eventlocation + ';');
rslt1 = jQuery('#txtCntNameReg').val(rslt);
console.log(rslt1);
}
});
Try this line:
qty = qty + 1;
instead of this,
qty = parseInt(jQuery(this).parents().nextAll().children().find('input[name="quantity"]').val() + 1);
in else condition.

nodeJS save post request of inputboxes in a file

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]

How to add auto complete address to dynamically added input fields

I want that user to see an alert whenever he doesn't enter proper Google address (or select from suggestion box) on dynamically added text areas. How can I perform that?
I have tried the following code.
I want that when I add new divs at runtime the auto-complete method should apply on that input field and when the user clicks submit button without selecting any suggested address it should display an alert.
Please help me where am I wrong in my code?
function updateNames() {
var rows = $(".clonedInput");
var index = 0;
rows.each(function () {
var inputFields = $(this).find("input");
inputFields.each(function () {
var currentName = $(this).attr("name");
$(this).removeClass("hasDatepicker");
$(this).removeClass("hasTimepicker");
$(this).attr("name", currentName.replace(/\[(.*?)\]/, '[' + index + ']'));
});
var textareaFields = $(this).find("textarea");
textareaFields.each(function () {
var currName = $(this).attr("name");
$(this).attr("name", currName.replace(/\[(.*?)\]/, '[' + index + ']'));
var places = new google.maps.places.Autocomplete(currName);
var a = places.getPlace();
if (a == null) {
alert('Please Select a valid location');
}
else { }
var currId = $(this).attr("id");
$(this).attr("id", currId.replace(/\[(.*?)\]/, '[' + index + ']'));
});
var numberSpan = $(this).find("span.taskNumber");
numberSpan.html(index + 1);
index++;
});
}

Javascript Counter not Working

Counter Not Working In this code.Provide any Example.My code is below
var rnos = jQuery("#ContentPlaceHolder1_hdnf").val(); //Get 1 value From c#
function getProducts() {
alert(jQuery("#ContentPlaceHolder1_hdnf").val());
var decode = jQuery("#hdnfield").val();
// var decode = "7503960000";
$.getJSON("http://localhost:8244/api/FRecharge?rno=" + rno + "&id=" + decode,
function (data) {
$('#products').empty(); // Clear the table body.
// Loop through the list of products.
$.each(data, function (key, val) {
// Add a table row for the product.
var row = '<td>' + val.status + '</td>';
$('<tr/>').html(row) // Append the name.
.appendTo($('#products'));
});
});
rnos = rnos + 1;
$("#ContentPlaceHolder1_hdnf").val()=$("#ContentPlaceHolder1_hdnf").val(rnos);//Increment Value and assign to Hidden field
}
=-=-=-=-=-===-
I am already try:
$("#ContentPlaceHolder1_hdnf").val()=$("#ContentPlaceHolder1_hdnf").val(rnos);//
And
$("#ContentPlaceHolder1_hdnf").val(rnos)
Alert always show 1.
Thnx Guys I get Answer (increase the value in hidden field .I m set 0 value on page load time)
var startElement = $("#ContentPlaceHolder1_hdnf");
var value = parseInt(startElement.val());
alert(value);
//startElement.val(value + 1);
var rnos = startElement.val(value + 1);
//alert(rnos);`

post data from table row like json format

this is related to my last question(
NOTE: I already got some good answers there). I'm doing a program that will filter. I didn't include this question because i thought that it is easier for me to add text as long as i know how to get the data from the row. But to my dismay, I wasn't able to code a good program til now.
Im currently using this javascript code (thanks to Awea):
$('#out').click(function(){
$('table tr').each(function(){
var td = '';
$(this).find('option:selected').each(function(){
td = td + ' ' + $(this).text();
});
td = td + ' ' + $(this).find('input').val();
alert(td);
});
})
my question is: How to add text before the data from the row? like for example, this code alert
the first row like data1.1 data1.2 data1.3,
then the second row like data2.1 data2.2 data2.3,
I want my output to be displayed like this
[
{"name":"data1.1","comparison":"data1.2", "value":"data1.3"},
{"name":"data2.1","comparison":"data2.2", "value":"data2.3"},
{"name":"data3.1","comparison":"data3.2", "value":"data3.3"}
{.....and so on......}]
but before that happen, i want to check if all the FIRST cell in a row is not empty. if its empty, skip that row then proceed to next row.
is there somebody can help me, please...
Building on my answer to your previous question, see http://jsfiddle.net/evbUa/1/
Once you have your data in a javascript object (dataArray in my example), you can write the JSON yourself, per my example, but you will find it much easier to use a library such as JSON-js (see this also).
// object to hold your data
function dataRow(value1,value2,value3) {
this.name = value1;
this.comparison = value2;
this.value = value3;
}
$('#out').click(function(){
// create array to hold your data
var dataArray = new Array();
// iterate through rows of table
for(var i = 1; i <= $("table tr").length; i++){
// check if first field is used
if($("table tr:nth-child(" + i + ") select[class='field']").val().length > 0) {
// create object and push to array
dataArray.push(
new dataRow(
$("table tr:nth-child(" + i + ") select[class='field']").val(),
$("table tr:nth-child(" + i + ") select[class='comp']").val(),
$("table tr:nth-child(" + i + ") input").val())
);
}
}
// consider using a JSON library to do this for you
for(var i = 0; i < dataArray.length; i++){
var output = "";
output = output + '{"name":"data' + (i + 1) + '.' + dataArray[i].name + '",';
output = output + '"comparison":"data' + (i + 1) + '.' + dataArray[i].comparison + '",';
output = output + '"value":"data' + (i + 1) + '.' + dataArray[i].value + '"}';
alert(output);
}
})
There are two things you need to do here. First get the data into an array of objects, and secondly get the string representation.
I have not tested this, but it should give you a basic idea of what to do.
Edit Please take a look at this JS-Fiddle example I've made. http://jsfiddle.net/4Nr9m/52/
$(document).ready(function() {
var objects = new Array();
$('table tr').each(function(key, value) {
if($(this).find('td:first').not(':empty')) {
//loop over the cells
obj = {};
$(this).find('td').each(function(key, value) {
var label = $(this).parents('table').find('th')[key].innerHTML;
obj[label] = value.innerHTML;
});
objects.push(obj);
}
});
//get JSON.
var json = objects.toSource();
$('#result').append(json);
});
var a = [];
a[0] = "data1.1 data1.2 data1.3"
a[1] = "data1.6 data1.2 data1.3"
var jsonobj = {};
var c = []
for (var i = 0;i
alert(c); //it will give ["{"name":"data1.1","comp...1.2","value":"data1.3"}", "{"name":"data1.6","comp...1.2","value":"data1.3"}"]
u have to include library for function from JSON.stringify from
https://github.com/douglascrockford/JSON-js/blob/master/json2.js
hope this helps

Categories

Resources