How to store multiple input in array? - javascript

Here is javascript code to add input field in on click but while storing only one row is stored . How to store all the row?
<div class = "row" id="dynamic_field">
<div class = "input-field col s12">
<input id = "list-title" name = "post[title]" type = "text" class = "validate" required value = "<">
<label for = "list-title ">Title</label>
</div>
</div>
$(document).ready(function() {
$('#added').click(function () {
$('#dynamic_field').append('<div class="row">' +
'<div class = "input-field col s12" >\n' +
' \n' +
'<input id = "list-title " name = "post[title]" type = "text" class = "validate" required value = "">\n' +
'<label for = "list-title ">Title</label>\n' +
'</div>'+
'</div>'
);
});
});
Here is my php code in store controller.
public function UpdateController() {
if (isset($_POST['post'])) {
$data = $_POST['post']
$model->title = $data['title'];
}
$model->save();
}

Related

Cannot read property : style of Null

I am working on a Trello Board using Vanilla Javascript and I am receiving an error on making my Cards . I have used Local and Session Storage to store the cards and lists respectively but I can't figure out why this error persists after I click on the Add Card Board Button
function newTask(x){
card_index= parseInt(localStorage.getItem("card_indices")) ;
//card_index = parseInt(sessionStorage.getItem("card_indices"));
list_index = parseInt(sessionStorage.getItem("index"));
document.getElementById('myTasks').innerHTML +=
'<div id = "list_' + list_index + ' " class="list-item animated zoomIn" > <h2 id = "title_' + list_index +'" onClick = "modifyObj.titleEdit('+ list_index +')" class="list-item__h2">'+x+'</h2><span id = "span_del_' + list_index + '" class ="btn title-delete" onClick ="modifyObj.titleDelete(' + list_index + ')">\u00D7</span><hr>'+
'<input id = "card_del_' + card_index + '" type="text" class="myInput" placeholder="Title...">' +
'<div class="btn add-items " id = "div_add_list_" onClick = "myCard.titleForm(' + list_index + ',' + card_index + ')" >Add List Item</div>'
'</div>'
sessionStorage.setItem("index", parseInt(sessionStorage.getItem("index"))+1);
}
var myCard = {
card:function(index, card_index){
var enteredElement = document.getElementById('card_del_' + card_index).value;
var textNode = document.getElementsByClassName('text_' + card_index);
textNode.innerText = enteredElement;
if (enteredElement === ""){
alert("You must write something ");
}
else{
document.getElementById("text_").style.display = "block";
}
},
titleForm: function(index,card_index){
element = document.getElementById('card_del_' + card_index);
text = '<li style="display:none" class="text_'+ card_index +'"> <span class = "btn items" onClick = "myCard.cardClose()">u00D7</span></li>'
element.insertAdjacentHTML('beforeend', text);
myCard.card(index,card_index);
localStorage.setItem("card_indices", parseInt(localStorage.getItem("card_indices"))+1);
// card_index+=1;
}};

Create array from Dynamic table

How can I create an array from this table/form? The onclick function formData() from the dynamic table only returns a concatenated string. I need to create an associative array in JSON using the 'device' variable as key, however I'll settle for any sort of array at all. Clearly, I'm not very good at this...
function createInputTable()
{
var num_rows = document.getElementById('rows').value;
var tableName = document.getElementById('conn_input_device').value;
var column_number = 2;
var tdefine = '<form id="form"><table id="table" border = "1">\n';
var theader = '<tr><th>No</th><th>Input</th><th>Output</th></tr>\n';
var caption = '<caption><input id="device" value ="' + tableName + '" /></caption>';
var tbody = '';
var tfooter = '</table>';
var createNewDevice = '<button onclick="formData();">Form Data</button></form>'
var i = 0;
for (var i= 0; i < num_rows; i++)
{
tbody += '<tr><td>' + (i+1) + '</td><td><input class="cell" id="i'+ i + '" type = "text"/></td>';
tbody += '<td><input class="cell" id="o'+ i + '" type="text"/></td></tr>\n';
}
document.getElementById('wrapper').innerHTML = caption + tdefine + theader + tbody + tfooter + createNewDevice;
}
function formData()
{
var cellData = document.getElementById("form");
//var device = document.getElementById('device').value;
//var j;
var obj = [];
for(j=0; j< cellData.length; j++)
{
obj += cellData[j].value;
}
var json = JSON.stringify(obj);
alert (json);
//document.getElementById('result').innerHTML = json;
}
<form id="tableGen" name="table_gen">
<label>Connecting device: <input type = "text" name = "conn_input_device" id = "conn_input_device"/></label><br />
<label>Number of inputs: <input type="text" name="rows" id="rows"/></label><br />
<input name="generate" type="button" value="Create Input Table!" onclick='createInputTable();'/>
</form>
<div id="wrapper"></div>
1) This my answer how do this on VueJS and jQuery
2) Vanilla js - CODEPEN - DEMO
// Get DOM elements
const $el = [
'#tmpl',
'#user-count',
'#people-count',
'#form-items',
'#btn-add',
'#form',
].reduce((res, item) => {
const method = item.startsWith('#')
? 'querySelector'
: 'querySelectorAll'
const key = item
.replace(/\W/ig, ' ').trim()
.replace(/\s+\w/g, v => v.trim().toUpperCase())
res[key] = document[method](item)
return res
}, {})
// Variable for dynamic template
const tmpl = $el.tmpl.innerHTML.trim()
// Click on Add new button
$el.btnAdd.addEventListener('click', () => {
const peopleCount = +$el.peopleCount.value
const html = Array(peopleCount)
.fill(tmpl)
.join('')
$el.formItems.insertAdjacentHTML('beforeend', html)
})
// Submit form
$el.form.addEventListener('submit', e => {
e.preventDefault()
alert('Submit form by ajax or remove this method for default behavior')
})
// Add form click (it's need for dynamic handler on child elements)
$el.form.addEventListener('click', e => {
// Delete behaviors
if (e.target.classList.contains('btn-del') && confirm('Are you sure?')) {
e.target.closest('.row').remove()
}
})
<div id="app">
<div>
<div>
<button id="btn-add">Add new user</button>
<label>Number of People:</label>
<input type="number" id="people-count" value="1" min="1">
</div>
<form id="form">
<div id="form-items" data-empty="Users list is empty"></div>
<button>Send</button>
</form>
</div>
</div>
<script type="text/x-template" id="tmpl">
<div class="row">
<label>
Name:
<input class="people" name="name[]">
</label>
<label>
Surname:
<input class="people" name="surname[]">
</label>
<label>
Email:
<input type="email" class="people" name="email[]">
</label>
<button class="btn-del">Delete</button>
</div>
</script>
<style>
.people {
width: 80px;
}
#form-items:empty + button {
display: none;
}
#form-items:empty:before {
content: attr(data-empty);
display: block;
}
</style>
I have edited your code,
function createInputTable()
{
var num_rows = document.getElementById('rows').value;
var tableName = document.getElementById('conn_input_device').value;
var column_number = 2;
var tdefine = '<form id="form"><table id="table" border = "1">\n';
var theader = '<tr><th>No</th><th>Input</th><th>Output</th></tr>\n';
var caption = '<caption><input id="device" value ="' + tableName + '" /></caption>';
var tbody = '';
var tfooter = '</table>';
var createNewDevice = '<button onclick="formData();">Form Data</button></form>'
var i = 0;
for (var i= 0; i < num_rows; i++)
{
tbody += '<tr><td>' + (i+1) + '</td><td><input class="cell" id="i'+ i + '" type = "text"/></td>';
tbody += '<td><input class="cell" id="o'+ i + '" type="text"/></td></tr>\n';
}
document.getElementById('wrapper').innerHTML = caption + tdefine + theader + tbody + tfooter + createNewDevice;
}
function formData()
{
var cellData = document.getElementsByTagName("tr");
var obj = [];
for(var i=0;i<cellData.length-1;i++){
obj.push(document.getElementById("i"+i).value);
obj.push(document.getElementById("o"+i).value);
}
alert(JSON.stringify(obj));
}
<form id="tableGen" name="table_gen">
<label>Connecting device: <input type = "text" name = "conn_input_device" id = "conn_input_device"/></label><br />
<label>Number of inputs: <input type="text" name="rows" id="rows"/></label><br />
<input name="generate" type="button" value="Create Input Table!" onclick='createInputTable();'/>
</form>
<div id="wrapper"></div>

AngularJS ng-model binding with dynamic elements

I am dynamically creating elements based on json data that i get from a service. The elements are being created fine but the values are not being bound to the model via ng-model. I tried putting the function in $timeout to force $apply(). Even that did not work. Following is the code :
function InstructionsCtrl($http, ApiCallFactory, $timeout) {
var insCtrl = this;
insCtrl.data = [];
insCtrl.answers = {};
insCtrl.questionGroup = ApiCallFactory.questionGroup;
$timeout(function(){
var html = "";
var questionSet;
for (var tempQuestionSet in insCtrl.questionGroup) {
if (insCtrl.questionGroup.hasOwnProperty(tempQuestionSet)) {
questionSet = insCtrl.questionGroup[tempQuestionSet];
for (var question in questionSet) {
if (questionSet.hasOwnProperty(question)) {
if (questionSet[question].type == 'text') {
html += ' <div class="form-group"> <label for="text">' + questionSet[question].text + '</label> <input type="text" class="hi" id="text" ng-model="insCtrl.answers.';
html += tempQuestionSet + '.' + question + '" value ="text" name="radio1"> </div> '
}
else if (questionSet[question].type == 'select') {
var noOfOptions = questionSet[question].values.length;
html += ' <div class="form-group"> <label for="radio">' + questionSet[question].text;
html += '<select>';
for (var i = 0; i < noOfOptions; i++) {
html += '<option>' + questionSet[question].values[i] + '</option>';
}
html += '</select></div>'
}
}
}
}
}
var formContainer = $('#jsonTest');
formContainer.empty();
formContainer.prepend(html);
});
}
I've currently added ng-model only to the text type fields. Pls let me know what i am doing wrong.

Get id number from a value using Jquery

I have the following code :
var currentuuid = $('#UUIDTOSEND').val();
html1 = '';
$.ajax({
type: "GET",
url: "database/Emarps/websrvc/websrvc.php?task=getData&UUID=" + currentuuid + "&DataGt=stitrtmnt",
dataType: "JSON",
success: function (data) {
client_history_list = $('#clinical_history_tbody').empty();
for (i = 0; i < data.length; i++) {
html1 += '<tr>\n\
<td class = "form-group">\n\
\n\
<input type = "text" class = "form-control facility_name col-md-6 col-sm-6 col-xs-12 " readonly="" name="facility_name[]" id = "facility_name" value="' + data[i].facility_name + '" placeholder = "Facility Name">\n\
</td>\n\
<td class = "form-group">\n\
\n\
<input type = "text" class = "form-control activity_timestamp col-md-6 col-sm-6 col-xs-12 " readonly="" name="activity_timestamp[]" id = "activity_timestamp" value="' + data[i].activity_timestamp + '" placeholder = "Visit Date">\n\
</td>\n\\n\
<td class = "form-group">\n\<input type = "text" class = "form-control clinical_visit_id' + data[i].id + ' " readonly="" name="clinical_visit_id[]" id = "clinical_visit_id" value="' + data[i].id + '" placeholder = "Visit ID">\n\
\n\
<button id="view_more' + data[i].id + '" class="form-control view_more' + data[i].id + '" >Select/View More</button>\n\
</td>\n\
</tr>';
$("#clinical_history_tbody").on("click", ".view_more" + data[i].id, function () {
var clinic_visit_ids = $("#" + this.id.replace("view_more", "clinical_visit_id")).val();
alert(clinic_visit_ids);
});
}
$('#clinical_history_tbody').empty();
$('#clinical_history_tbody').append(html1);
}
});
Which generates an auto table with data from the database.
I want to get the value of id from the button when I click the Select/View More button, which runs the following the $("#clinical_history_tbody").on("click", ".view_more" + data[i].id, function () { function on the script. How can I get the value of the button id and replace it so that I can use it in the clinic visit ? (When I alert I get an undefined).
The input for "clinical_visit" is never assigned a numeric value at the end of its id.
<input type = "text" class = "form-control clinical_visit_id' + data[i].id + ' " readonly="" name="clinical_visit_id[]" id = "clinical_visit_id" value="' + data[i].id + '" placeholder = "Visit ID">
it is instead concatenated to its class.
this goes wrong when you are tring to fetch the "clinical visit" field, since you are looking for an id with value "clinical_visit_id[number]".
It isn't there, so you end up with an empty value :
var clinic_visit_ids = $("#" + this.id.replace("view_more", "clinical_visit_id")).val();
try this :
<input type = "text" class = "form-control clinical_visit_id' + data[i].id + ' " readonly="" name="clinical_visit_id[]" id = "clinical_visit_id'+data[i].id+'" value="' + data[i].id + '" placeholder = "Visit ID">
OR
look for the "clinical_visit" input by class instead of id (keep in mind you still need to sanitize your HTML, since two elements should never have the same id
That would look like this :
$("#clinical_history_tbody").on("click", ".view_more" + data[i].id, function () {
var clinic_visit_ids = $("." + this.id.replace("view_more", "clinical_visit_id")).val();
alert(clinic_visit_ids);
});
You can use this script to get the id,
$("#clinical_history_tbody").on("click", "[class^=view_more]", function () {
alert($(this).attr("id"));
});
the above script wll bind click event to all the elements whose class name starts eith view_more. Then it will alert the current clicked elements id.
This is how you change the id of the button:
$("#clinical_history_tbody").on("click", function () {
var clinic_visit_ids = $(this).attr('id', "clinical_visit_id");
});
Attach event to that button onClick
onClick=giveMeID(data[i].id)
in Html and then define in JS
function giveMeID(id){
//Do whatever you want
}
Here's Example JSBIN DEMO
<html>
<body>
<button id="exa" onclick="genExamp()">Generate Example</button>
<div id="ex"></div>
<script>
function genExamp(){
var divEx = document.getElementById("ex");
for (var i = 0; i < 20; i++) {
var childBtn='<button id="view_more'+i+'" onclick="giveMeID('+i+');">View/More'+i+'</button>';
divEx.innerHTML+=childBtn;
}
}
//On Click:Gets ID and Replace it
function giveMeID(obj){
var origID="view_more"+obj;
var btn=document.getElementById('view_more'+obj);
btn.id="view_more"+obj*10;
var new_id=btn.id;
alert("Original ID:"+origID+"\n"+"New ID:"+new_id);
}
</script>
</body>
</html>

IBDCursor IntelXDK stop

I am implementing a Call list Where LOAD need a flame already Made is need list and Place checkbox Mark que um not been present . However NOT Giving .
I use a cursor idea , EO Bank Used And IndexedDB data , I do a Search All Students " , " the code " student " to equal When the " student " code in Table Presence , the problem happens here! HE DOES NOT When paragraph are a Correct ANSWER , he continues to test everyone.
The code below :
db.transaction ("tbl_PESSOAS"). ObjectStore ("tbl_PESSOAS"). get (cursor.value.COD_IDENT_PESSO) .onsuccess = function (event) {
$ wrapper = document.querySelector ('# check_presenca');
HTMLTemporario = $ wrapper.innerHTML;
= event.target.result person;
if (pessoa.FLG_IDENT_PESSO == "A" && cursor.value.FLG_IDENT_PESSO == "M") {
var w_codigo_reuniao = sessionStorage.getItem ("w_codigo_reuniao");
w_aux var = 01;
var bd = db.transaction ("tbl_PRESENCA"). ObjectStore ("tbl_PRESENCA")
index = bd.index ("COD_IDENT_REUNI");
index.openCursor (w_codigo_reuniao) .onsuccess = function (event) {
var = vector event.target.result;
if (vector) {
if (pessoa.COD_IDENT_PESSO == vetor.value.COD_IDENT_PESSO) {
HTMLNovo = '<li class = "item-item checkbox uib_w_69 widget" data-UIB = "ionic / checkbox" data-view = "0"> <label class = "checkbox"> <input class = "check" type = " checkbox "name =" '+ pessoa.COD_IDENT_PESSO +' "checked> <input id =" codigoPessoaPresente "value =" '+ pessoa.COD_IDENT_PESSO +' "type =" hidden "> </ label> '+ pessoa.TXT_NOMEX_PESSO +' </ li> ';
HTMLNovo = HTMLNovo + HTMLTemporario;
$ wrapper.innerHTML = HTMLNovo;
} else {
HTMLNovo = '<li class = "item-item checkbox uib_w_69 widget" data-UIB = "ionic / checkbox" data-view = "0"> <label class = "checkbox"> <input class = "check" type = " checkbox "name =" '+ pessoa.COD_IDENT_PESSO +' "> <input id =" codigoPessoaPresente "value =" '+ pessoa.COD_IDENT_PESSO +' "type =" hidden "> </ label> '+ pessoa.TXT_NOMEX_PESSO +' </ li > ';
HTMLNovo = HTMLNovo + HTMLTemporario;
$ wrapper.innerHTML = HTMLNovo;
vetor.continue ();
}
}
};
}
};
You will need to provide a key range if you want to filter the data.
store.openCursor(IDBKeyRange.lowerBound(0))
You can find some more info over here and about the different IDBKeyRanges here

Categories