Write down user input in a text file - html

i tryed it with a basic html code like :
<label for="fname">First name:</label><br>
<input type="text" id="fname" name="fname" value="John"><br>
<label for="lname">Last name:</label><br>
<input type="text" id="lname" name="lname" value="Doe"><br><br>
<input type="submit" value="Submit">
</form>
but then the input is saved like fname=John&lname=Doe
I wrote a script in python that gets the file xxx/xxx/display.txt
when i use the basic html input function, the text will get saved as fname =john & sname=rober
what i woud need is the string to get saved as john rober
how can i write the input down in the txt file without = and & , and how do i save it in the file, while overwriting everything previus in the txt file?

For that you need your js-code to be server side. I'd reccoment Node.js for that.
Than we need to format the input with js:
// Requiring fs module in which writeFile function is defined:
const fs = require('fs');
var fname = document.getElementById("fname").value;
var lname = document.getElementById("lname").value;
var fullname = fname + lname;
// data which will write in a file
let data = fullname;
// Write "data" in "file.txt":
fs.writeFile('file.txt', data, (err) => {
// In case of an error trhow err:
if (err) throw err;
})

Related

Send form data to a pdfkit iframe document

I would like to retrieve the values of a form and send them to a pdfkit document generated in back end.
For the moment, I only have created a simple document by a "get" route :
router.get(
"/",
authCheck,
asyncHandler(async (req: express.Request, res: express.Response) => {
const doc = new PDFDocument();
let filename = "toto";
filename = encodeURIComponent(filename) + ".pdf";
res.setHeader(
"Content-disposition",
'attachment; filename="' + filename + '"'
);
res.setHeader("Content-type", "application/pdf");
const content = "contenu";
doc.y = 300;
doc.text(content, 50, 50);
doc.pipe(res);
doc.end();
})
);
Here is the code to retrieve the created document :
<iframe className="preview-pdf__container" src={pdfContent} />
How do I send my form data and get it back into the document? With a POST?
This is how I was able to get data from my form into my server then export it to different files with in my server using node.js if it helps. I am not entirely sure on how to get it back out of your server into an iframe though.
HTML:
<div class="wrapper">
<form action="/login" method="POST">
<label id="email-label">Email</label>
<div class="textarea" id="email">
<input type="email" name="email" id="authentactor-email" value="" required>
</div>
<label>Password</label>
<div class="textarea" id="password">
<input type="password" name="password" id="authentactor-password" value="" required>
</div>
<div id="button-wrapper">
<button type="submit" id="button">Login</button>
</div>
</form>
</div>
node.js
//Allows you to Export form data from index.js
app.post('/login');

How do I send form data to a link?

I have a HTML page with a form, that has 2 inputs, an email, and a username. I want to pass these inputs to a node.js express server called exampleserver.com with fetch(). I have no problems with the server, however, I have problems with the form. Here is my html form:
<h1 align="center">Form</h1>
<input type="text" placeholder ="person#email.com" id="email">
<br>
<input type="text" id="username" placeholder="person">
<br>
<button type="submit" onclick="myfunc()" id="demo">Click Me</button>
<script type="text/javascript">
var email = document.getElementById('email')
var formattedemail = email.value.replace(/\./, '-') //periods dont work as a path in the express server, so this changes the period to a hyphen.
var username = document.getElementById('username');
function myfunc() {
let fetchRes = fetch("https://exampleserver.com/inputform/" + formattedemail + "/" + username.value);
fetchRes.then(res =>
res.json()).then(response => {
console.log(response.status)
})
}
</script>
Thank you!
Your code is working fine with jsonplaceholder api, make sure your API is working as expected.
var email = document.getElementById('email')
var formattedemail = email.value.replace(/\./, '-') //periods dont work as a path in the express server, so this changes the period to a hyphen.
var username = document.getElementById('username');
function myfunc() {
// temp data
formattedemail = "todos";
username.value = "1";
let fetchRes = fetch("https://jsonplaceholder.typicode.com/" + formattedemail + "/" + username.value);
fetchRes.then(res =>
res.json()).then(response => {
console.log(response);
})
}
<h1 align="center">Form</h1>
<input type="text" placeholder="person#email.com" id="email">
<br>
<input type="text" id="username" placeholder="person">
<br>
<button type="submit" onclick="myfunc()" id="demo">Click Me</button>

Express.js not adding into mysql

I'm a beginner to ExpressJS, I want to be able to add records to the "site" table. However, when I run the following code, it says:
Error: ER_BAD_FIELD_ERROR: Unknown column 'BeastMode' in 'field list'.
"BeastMode" is an entry I made to the shortName field.
A little context: I'm not supposed to use ORM. I have to use raw sql queries to add to MYSQL database.I'm using 'mysql'package for Nodejs to connect to the database.
var squery = "INSERT INTO SITE (shortName,addressLine1,addressLine2,city,state,zipcode,phoneNumber) VALUES "+
"("+
req.body.shortName+", "+
req.body.addressLine1+", "+
req.body.addressLine2+", "+
req.body.city+", "+
req.body.state+", " +
req.body.zipcode+", " +
req.body.phoneNumber+" );"
console.log(req.body);
dbconnector.query(squery,function(err,rows,fields){
if(!err){
res.send("Record Added Successfully: "+req.body);
}else{
res.send("Error: "+ err);
}
});
});
Also, here is my dbconnect.js file:
var mysql = require('mysql');
dbconnect = mysql.createConnection({
host: "localhost",
user: "root",
password: "",
database:"rsacs"
});
module.exports = dbconnect
Here is my HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<% include head %>
</head>
<body class="container">
<header>
<% include header %>
</header>
<main>
<div>
<h1><%=title%></h1>
<form method="post" action="/site/create" >
<div class="form-group">
<label for="shortName">Shortname</label>
<input type="text" class="form-control" placeholder="Shortname" name="shortName"><br>
<label for="Address Line 1"> Address Line 1:</label>
<input type="text" class="form-control" placeholder="Address Line 1" name="addressLine1"><br>
<label for="Address Line 2"> Address Line 2:</label>
<input type="text" class="form-control" placeholder="Address Line 2" name="addressLine2"><br>
<label for="City">City:</label>
<input type="text" class="form-control" placeholder="City" name="city"><br>
<label for="State">State:</label>
<input type="text" class="form-control" placeholder="State" name="state"><br>
<label for="Zipcode">Zipcode:</label>
<input type="text" class="form-control" placeholder="Zipcode" name="zipcode"><br>
<label for="PhoneNumber">Phone Number:</label>
<input type="text" class="form-control" placeholder="PhoneNumber" name="phoneNumber"><br>
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
</div>
</main>
<footer>
<% include footer %>
</footer>
</body>
</html>
Here is my Site table structure
To echo #AnshumanJaiswal's solution, you're probably encountering an escape character problem.
The solution I'm going to propose, though, is different. The mysql nodejs driver supports prepared queries. As such, the most robust way to sort your query is:
var squery = "INSERT INTO SITE (shortName,addressLine1,addressLine2,city,state,zipcode,phoneNumber) VALUES (?,?,?,?,?,?,?);
var objs = [req.body.shortName,req.body.addressLine1,req.body.addressLine2,req.body.city,req.body.state,req.body.zipcode,req.body.phoneNumber]
sql = mysql.format(squery, objs);
// now you have a properly-escaped SQL query which you can execute as usual:
connection.query(squery, objs, function (error, results, fields) {if (error) throw error;});
Let me know if this doesn't sort your problem.
the values are string and you are not passing them as string.
There are two possible ways:
Solution 1.
add `` to your string values like:
var squery = "INSERT INTO SITE (shortName,addressLine1,addressLine2,city,state,zipcode,phoneNumber) VALUES "+
"('"+
req.body.shortName+"', '"+
req.body.addressLine1+"', '"+
req.body.addressLine2+"', '"+
req.body.city+"', '"+
req.body.state+"', '" +
req.body.zipcode+"', " +
req.body.phoneNumber+" );"
...
Solution 2.
make an object from body data as:
var data = {
shortName: req.body.shortName,
addressLine1: req.body.addressLine1,
addressLine1: req.body.addressLine2,
city: req.body.city,
state: req.body.state,
zipcode: req.body.zipcode,
phoneNumber: req.body.phoneNumber
};
var squery = "INSERT INTO SITE SET ?";
dbconnector.query(squery, data, function(err,rows,fields){
if(!err){
console.log(rows);
res.send("Record Added Successfully.");
}else{
res.send("Error: "+ err);
}
});

How to correctly write into Google Spreadsheet with Google Apps Script?

I'm creating a webpage with a form and I have to send data to a Google Spreadsheet. I found a good code searching this site but I don't know how to edit it to store my data in Google Spreadsheet.
My form is made this way
<form name="reqForm" id="reqForm" method="get" action="SCRIPT" accept-charset="UTF-8">
<input type="hidden" name="reqID" id="reqID" />
<label for="name">Name:</label>
<input type="text" name="Name" id="Name"/>
<label for="surname">Surname:</label>
<input type="text" name="surname" id="surname"/>
<label for="SERIAL">Serial:</label>
<input type="text" name="serial" id="serial"/>
<label for="email">E-mail:</label>
<input type="text" name="email" id="mail"/>
<label for="text">Request:</label>
<textarea id="text"></textarea>
<input type="submit" value="Send" />
</form>
The script is the same, from the example I linked (I just changed the function name because of an error)
function doPost(e){
var id = "";
var name = e.parameter['name'];
var surname = e.parameter['surname'];
var serial = e.parameter['serial'];
var eMail = e.parameter['email'];
var text = e.parameter['text'];
var date = new Date();
var ans = ""
var flag = "WIP";
var vals = [id, date, name, surname, serial, eMail, text, ans, flag];
var sheetObj = SpreadsheetApp.openById("myKey").getSheetByName('Requests').appendRow(vals);
// return ContentService.createTextOutput(e);
}
If I fill the form in this way:
Name: John
Surname: Doe
Serial: 123456
Email: 123#456.com
Request: Hello
in my Spreadsheet I see data in this order:
[DATE] [Serial] [Mail] [Name] [Surname]
How can modify the function to put my data into the spreadsheet in some kind of order that I can decide?
[UPDATE]: I updated the code: the result of this is a line correctly ordered but filled with "undefined"...
After submitting the form, you can get form parameters using e.parameter[<parameter_name>].
For example to get the surname,
var surname = e.parameter['surname'];
To insert form data in Google Spreadsheet,
var sheetObj = SpreadsheetApp.openById("myKey").getSheetByName("sheetname");
//Here you can shuffle the order of values anyway you want.
sheetObj.appendRow([DATE, Serial, Mail, Name, Surname]);
//or you can do it like this
sheetObj.appendRow([DATE, Mail, Name, Surname, Serial]);
Hope this helps.

Add form information in to URL string and submit

I need to get information from my online form added in to my URL string and get it submitted to the dialler.
I have a working URL string that submits data to our dialler ok.
I need to get the first name, last name and phone number from the form submission in to the URL string.
This is how the URL string looks;
http://domain.com/scripts/api.php?user=admin&pass=password&function=add_lead&source=MobileOp&phone_number=07000000000&phone_code=44&list_id=3002&first_name=NAME&last_name=SURNAME&rank=99&campaign_id=campaign&callback=Y&callback_datetime=NOW
This is the form I have;
<form id="contact_form" method="post" action="">
<div class="contactform">
<fieldset class="large-12 columns">
<div class="required">
<label>Your First Name:*</label>
<input name="first_name" type="text" class="cms_textfield" id="first_name" value="" size="25" maxlength="80" required="required" />
</div>
<div class="required">
<label>You Last Name:*</label>
<input name="last_name" type="text" class="cms_textfield" id="last_name" value="" size="25" maxlength="80" required="required" />
</div>
<div class="required">
<label>Phone Number:*</label>
<input name="phone_number" type:"number" id="phone_number" size="25" maxlength="11" required="required"></input>
</div>
</fieldset>
<p class="right"><strong>Call us now on 01656 837180</strong></p>
<div class="submit"><input type="submit" value="Submit" class="button small radius"></div>
</div>
</form>
I am struggling to get anywhere with this. I have a basic knowledge of PHP.
If you change your form to method="GET" and the action to your url action="http://domain.com/scripts/api.php" it will include it in the URL string. That said, showing a user's password as a query string variable is probably a bad idea in the long run.
Instead, you can process the input from the form in PHP by referring to the $_POST array in your code. For example, to get the first name you'd just use $_POST['first_name']
Change
<form id="contact_form" method="post" action="">
to
<form id="contact_form" method="GET" action="">
(notice the method 'GET'). GET sends form variables through the URL.
You can use PHP for this.
if you have an input field of name attribue 'first_name', It'll be stored in the variable $_POST['first_name'] in case of POST as method and $_GET['first_name'] in case of GET method
If you have a url
http://domain.com/scripts/api.php?user=admin&pass=password&function=add_lead&source=MobileOp&phone_number=07000000000&phone_code=44&list_id=3002&first_name=NAME&last_name=SURNAME&rank=99&campaign_id=campaign&callback=Y&callback_datetime=NOW,
notice the x=y pattern repeating in it, like user=admin. Here, the first element, x becomes the key to tha PHP array and the second becomes the value.
You can use this function. on your submission page
<script type="text/javascript">
function iter() {
var str = "";
$("#contact_form .contactform .required :input").each(function () { // Iterate over inputs
if ($(this).attr('id')) {
str += $(this).attr('id') + "=" + $(this).val() + "&"; // Add each to features object
}
});
str = str.substring(0, str.length - 1);
$.ajax({
type: "GET",
url: "http://domain.com/scripts/api.php",
data: str,
async: true,
error: function (error) {
},
success: function (data) {
}
});
}
</script>
just attach it to the submit button as shown below
$("#contact_form .submit").on("click", function () {
iter();
return false;
});