Is there a way to concatenate strings and variable values and new lines, in javascript?
My code:
var variab = "Make:"+ make \n
"Model:" + model \n
"Date taken: " + dateTime \n
"File name:" + fileName ;
variab = variab.replace(/\n/g, '<br>');
document.getElementById('exifid').innerHTML = variab;});
make, model, dateTime and fileName are all variables that return specific values, and I want them to be displayed into a list-like form in my div.
You almost got it:
var variab = "Make:" + make + "\n" +
"Model:" + model + "\n" +
"Date taken: " + dateTime + "\n" +
"File name:" + fileName ;
variab = variab.replace(/\n/g, '<br>');
document.getElementById('exifid').innerHTML = variab;
You can also put each line in an Array and use .join("<br>") to save yourself some typing.
Why use the replace?
var variab = "Make:"+ make + "<br>" +
"Model:" + model + "<br>" +
"Date taken: " + dateTime + "<br>" +
"File name:" + fileName ;
document.getElementById('exifid').innerHTML = variab;
You could stuff each element into an array and then join them.
var array = [
"Make:"+ make,
"Model:" + model,
"Date taken: " + dateTime,
"File name:" + fileName
];
document.getElementById('exifid').innerHTML = array.join('<br/>');
For greater readability, I'd do this:
var variab = [
"Make:" + make,
"Model:" + model,
"Date taken: " + dateTime,
"File name:" + fileName
].join("<br />");
You're also free to include newlines within your fields this way.
Unless your needs grow more complex. In that case, I'd use a templating framework.
It is much, much better to abstract the data into a readable form using a JavaScript object, like below, if possible.
var variab = {
"Make": "Audi",
"Model": "A3",
"Date Taken": 2006,
"File Name": "audia3.doc"
}
var string = "";
for (var detail_type in variab){
var detail_data = variab[detail_type];
string += detail_type + ": " + detail_data + "<br/>"
}
document.getElementById('exifid').innerHTML = variab;
Related
I'm trying to insert and values into my database and I thought having the same name for the inputs in my form would make it easier for me to insert them in bulk, I want to iterate OrderNo and quantity and insert them to a new column while keeping the other inputs such as fname the same until the values in OrderNo are done.
app.post("/send-data", (req,res)=>{
let sql = "INSERT INTO foodorder.orders (" +
"food_id," +
" qty,"+
" customer_FName," +
" customer_LName," +
" customer_number," +
" customer_email," +
" customer_facebook," +
" order_date," +
" delivery_option," +
" mode_of_payment," +
" delivery_time" +
") VALUES (" +
` ${req.body.OrderNo},` +
` ${req.body.quantity},` +
` "${req.body.fname}",` +
` "${req.body.lname}",` +
` "${req.body.Contact}",` +
` "${req.body.email}",` +
` "${req.body.facebook}",` +
` "${req.body.date}",` +
` "${req.body.delivery}",` +
` "${req.body.payment}",` +
`"${req.body.time}"` +
`)`;
con.query(sql, (err,result) => {
if(!err){
res.send(result);
}
else{
res.json(req.body);
throw err;
}
})
});
This is the code that I have and currently it only accepts 1 OrderNo and quantity and when I try to order many products with different quantities it will say column count doesn't match
value count at row 1 (see attached photo below)
[the output i get from my forms *I didnt include in the screenshot the other output details such as email etc.][1]
is there a way to just iterate the OrderNo and quantity and insert them creating new columns but with the same fname and lname etc.? or do i need to use MySql cursor?
Note that my code works and inserts if i only have selected 1 order and 1 quantity in my forms
UPDATE
Attached here is what I wanna achieve. I wanna know a way to iterate or loop the OrderNo and quantity and insert them into their respective columns while keeping the other values the same like fname and lname.
I wanna achieve this
Fixed by iterating the OrderNo. Thanks so much Diogo
app.post("/send-data", (req,res)=>{
let order = req.body.OrderNo;
let quantity = req.body.quantity;
let first = req.body.fname,
last = req.body.lname,
contact = req.body.Contact,
email = req.body.emailAdd,
fb = req.body.facebook,
date = req.body.date,
delivery = req.body.delivery,
payment = req.body.payment,
time = req.body.time,
address = req.body.address;
[order].forEach((product, index, arr)=>{
const q = quantity[index];
let sql = "INSERT INTO foodorder.orders (" +
"food_id," +
" qty,"+
" customer_FName," +
" customer_LName," +
" customer_address," +
" customer_number," +
" customer_email," +
" customer_facebook," +
" order_date," +
" delivery_option," +
" mode_of_payment," +
" delivery_time" +
") VALUES (" +
con.escape(product) + `,` +
con.escape(q) + `,` +
con.escape(first) + `,` +
con.escape(last) + `,` +
con.escape(address) + `,` +
con.escape(contact) + `,` +
con.escape(""+email) + `,` +
con.escape(fb) + `,` +
con.escape(date) + `,` +
con.escape(delivery) + `,` +
con.escape(payment) + `,` +
con.escape(time) +
`)`;
con.query(sql, (err,result) => {
if(!err){
res.redirect('thankyou.html');
}
else{
res.status(404).send('ERROR. Please Go back and Order Again');
}
})
})
});
I'm creating a communications generator that will standardise SMS communications regarding critical IT incidents for my company. I've got an IF/ELSE statement that identifies whether an issue is new, updated, or resolved to pull the necessary information together and format it accordingly. As far as I can tell, everything here is fine, however I'm having an issue writing to a text box ('smsToSend') which should allow the user to review before they copy the text across to our sms sender app, as nothing is being output into this box.
function generateSMS(){
var issueTitle = document.getElementById("incidentTitle");
var advisorImpact = document.getElementById("advisorImpact";
var incidentUpdate = document.getElementById("incidentUpdate");
var incidentStatus = document.getElementById("incidentState");
var startTime = document.getElementById("startTime");
var endTime = document.getElementById("endTime");
var incidentPriority = document.getElementById("incidentPriority");
var incidentBrand = "TechTeam";
var systemImpacted = document.getElementById("systemImpacted");
var incidentReference = document.getElementById("incidentReference");
var smsToSend = document.getElementById("smsToSend");
if (incidentStatus != "Closed"){
smsToSend = "P" + incidentPriority + " " + incidentBrand + "IT RESOLVED: " + systemImpacted + ": " + incidentUpdate + ": Start: " + startTime + " End: " + endTime + " Reference: " + incidentReference;
}
else{
if (incidentUpdate == "We are investigating this issue"){
smsToSend = "P" + incidentPriority + " " + incidentBrand + "IT ISSUE: " + systemImpacted + ": " + issueTitle + ". " + advisorImpact + ": " + incidentReference;
}
else {
smsToSend = "P" + incidentPriority + " " + incidentBrand + "IT UPDATE: " + systemImpacted + ": " + incidentUpdate + ": " + incidentReference;
}
}
}
generateSMS();
alert.getElementById(smsToSend);
Try replacing all assignments to smsToSend like this:
smsToSend = yourValue;
With this:
smsToSend.value = yourValue;
Your problem happens because smsToSend is an instance of an element, rather than a variable linked to the displayed text. To update the element's value, you have to change the value property of the element.
The Right way of setting the value to a text box is
var smsToSend = document.getElementById("smsToSend");
smsToSend.value = "Your value";
I'm trying to take information out of a form controlled by a select statement and store it in a string. The plan is to send the information gathered from the user via email, which I used PHP to do. I have an idea how to do it (a while loop that's controlled by how many lines are in the list,) but I can't quite gather the information.
Here's the loop:
var empMessage = function(){
messageString = "";
var noEmp = $("#drpEmp option").length;
var i = 0;
$("#drpEmp").selector.index = i;
while (i < noEmp){
$("#drpEmp").index = i;
messageString = messageString +
"Name: " + firstName.val + " " + MI.val + " " + lastName.val + "<br/>" +
"Business: " + BusinessName.val + "<br/>" +
"Address: " + address.val + "<br/>" +
" " + city.val + ", " + state.val + " " + zip.val + "<br/>";
}
messageString = "<strong>Employee Information</strong><br/>" . messageString;
i++;
}
$("#stringHolder").val(messageString);
}
Here's the JS Fiddle:
https://jsfiddle.net/xptxz7by/3/
I know I probably really off. I know I have the loop working and the list is getting counted, but I can't seem to retrieve the information. I'm also thinking I'm approaching sending the information wrong and there's probably a better way of doing it, I just haven't figured out how.
The data you want is in the Employee object that you saved in each option's .data(), so you need to get that data and use the properties.
messageString = "<strong>Employee Information</strong><br/>";
$("drpEmp option:selected").each(function() {
var employee = $(this).data("employee");
messageString +=
"Name: " + employee.firstName + " " + employee.MI + " " + lastName + "<br/>" +
"Business: " + employee.BusinessName + "<br/>" +
"Address: " + employee.address + "<br/>" +
" " + employee.city + ", " + employee.state + " " + employee.zip + "<br/>";
});
Can anybody help to assign this xml as constant string in javascript. How to assign this xml as constant without any changes?
"<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<wp:Notification xmlns:wp=\"WPNotification\">
" +
"<wp:Tile>
" +
"<wp:BackgroundImage>" + TextBoxBackgroundImage.Text + "</wp:BackgroundImage>" +
"<wp:Count>" + TextBoxCount.Text + "</wp:Count>" +
"<wp:Title>" + TextBoxTitle.Text + "</wp:Title>" +
"<wp:BackBackgroundImage>" + TextBoxBackBackgroundImage.Text + "</wp:BackBackgroundImage>" +
"<wp:BackTitle>" + TextBoxBackTitle.Text + "</wp:BackTitle>" +
"<wp:BackContent>" + TextBoxBackContent.Text + "</wp:BackContent>" +
"
</wp:Tile> " +
"
</wp:Notification>";
Thanks in advance
JavaScript doesn't have a constant modifier.
Closest you could do is define it with defineProperty() and set writable to false.
Also, your string...
"<wp:Tile>
" +
...won't work as the parser currently doesn't support multiline strings. You'll need to break it up with " + " or use the escape character as the last character.
Im new here, Im new in programming as well and I seldom code anything, but recently I've always wanted to improve my work place and I just want to create a simple form for my colleagues.
To make things short, I've created the form, what I need is just generating all the result of the form into a textarea.
Now my problem is, I've made several type of "Event" in the form that have different set of choices, and what I want is the result of that set of "Event" generated as well with the basic information.
Well, here example of my code; Begin Javascript
function generateresult() {
name = document.FORM.namefromtextarea.value;
phone = document.FORM.phonefromtextarea.value;
address = document.FORM.addressfromtextarea.value;
name2 = "Name: " + name + "\n";
phone2 = "Phone: " + phone + "\n";
address2 = "Address: " + address + "\n";
//problem type 1
lostitem = document.FORM.lostitemfromtextarea.value;
when = document.FORM.whenfromtextarea.value;
where = document.FORM.wherefromtextarea.value;
lostitem2 = "Lost Item?: " + lostitem + "\n";
when2 = "When?: " + when + "\n";
where2 = "Where?: " + where + "\n";
//problem type 2
lostperson = document.FORM.lostpersonfromtextarea.value;
personage = document.FORM.personagefromtextarea.value;
personcloth = document.FORM.personclothfromtextarea.value;
lostperson2 = "Person Name?: " + lostperson + "\n";
personage2 = "Age?: " + personage + "\n";
personcloth2 = "Wearing?: " + personcloth + "\n";
if (document.FORM.problemtype.value="Lost Item")
{
eventtype = type1;
}
else if (document.FORM.problemtype.value="Lost Person")
{
eventtype = type2;
}
type1 = lostitem2 + when2 + where2 ;
type2 = lostperson2 + personage2 + personcloth2 ;
document.FORM.generateresulttext.value = name2 + phone2 + address2 + eventtype ;}
End of javascript
And if a user clicked option that have value "Lost Person", the result for generated text will be taken from event "type2"
So I've managed to get the result for name, phone, and address, but as for lost item result, the value of the result became "undefined".
So... how exactly can I code this? I am not even sure if Im really doing the right script / method for my simple form... Thanks in advance
It looks to me like you might be trying to create a variable that takes data from an inexistant element. You might want to put the code you have inside another function.
function generateresult() {
name = document.FORM.namefromtextarea.value;
phone = document.FORM.phonefromtextarea.value;
address = document.FORM.addressfromtextarea.value;
name2 = "Name: " + name + "\n";
phone2 = "Phone: " + phone + "\n";
address2 = "Address: " + address + "\n";
//problem type 1
function firstType(){
lostitem = document.FORM.lostitemfromtextarea.value;
when = document.FORM.whenfromtextarea.value;
where = document.FORM.wherefromtextarea.value;
lostitem2 = "Lost Item?: " + lostitem + "\n";
when2 = "When?: " + when + "\n";
where2 = "Where?: " + where + "\n";
document.FORM.generateresulttext.value = name2 + phone2 + address2 + lostitem2 + when2 + where2 ;
}
//problem type 2
function secondType(){
lostperson = document.FORM.lostpersonfromtextarea.value;
personage = document.FORM.personagefromtextarea.value;
personcloth = document.FORM.personclothfromtextarea.value;
lostperson2 = "Person Name?: " + lostperson + "\n";
personage2 = "Age?: " + personage + "\n";
personcloth2 = "Wearing?: " + personcloth + "\n";
document.FORM.generateresulttext.value = name2 + phone2 + address2 + lostperson2 + personage2 + personcloth2 ;
}
if (document.FORM.problemtype.value="Lost Item")
{
firstType();
}
else if (document.FORM.problemtype.value="Lost Person")
{
secondType();
}
}
In the future, you should NOT put numbers in a variable's name. You should also NEVER declare variables like you're doing here. When you want to create a variable, you type var variableName = variableValue. You also NEVER use words like where or when for a variable name, but instead name it to something like lostWhere or lostWhen.