I am trying to display the user ID from a database in a textbox. In the input I am trying to retrieve the user ID, which works, but when I try display it in a textbox it does not appear? The java part if my code is working but I am unsure how I can display the results set in a textbox? Any help much appreciated.
<!DOCTYPE html>
<html>
<head>
<title>JPage</title>
</head>
<body>
<%
try{
String session_id =null;
HttpSession session1=request.getSession(false);
if(session1!=null){
session_id=(String)session1.getAttribute("name");
}
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/alt", "root", "");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from register where uid='"+session_id+"'");
rs.next();
String uid = rs.getString("uid");
%>
<form action="register_1.jsp" method="post">
<input type="text"name="TestType" value="Chemical">
<imput type="text" value=" <%out.print(uid);%> " name="uid" >
<input value="Create" type="submit" class="btn" style="width: 100px;">
</form>
<%
}catch(Exception e){
out.println(e);
}
%>
</body>
</html>
`
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);
}
});
i need your help for a simple question:
I have a form and i need that when user select another value iin a , it will change all values of of form.
My code is as follow:
<form action="updatecost.jsp" method="post" onsubmit=" return check(this)">
<tr>
<td><i> choice cost</i></td>
<td align="left"><select name="oldcost">
<%
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb","user","pass");
Statement statement = conn.createStatement();
ResultSet rs = statement.executeQuery("select * FROM cost");
String id = "";
String year="";
String time="";
String cf="";
while (rs.next()) {
id = rs.getString("IdCost");
year=rs.getString("year");
time=rs.getString("time");
cf=rs.getString("CF");
%>
<option value="<%=id%>"><%=year%></option>
<%
}
%>
</select></td>
<br>
<td><i><font color="#660033"> actual year</font></i></td>
<br>
<input type="text" name="year" value="<%=year%>" size="25" /></td>
<br>
<td><i><font color="#660033">actual cost </font></i></td>
<br>
<input type="text" name="cost" value="<%=cost%>" size="25" /></td>
<br>
<td><i><font color="#660033"> cf </font></i></td>
<br>
<input type="text" name="cf" value="<%=cf%>" size="25" /></td>
<br>
<br>
It is better to save all of the results into ArrayList<Cost> costs then do the following:
Javascript:
var ids = new Array(),
years = new Array(),
times = new Array(),
cfs = new Array();
<%
for(Cost c: costs) {
%>
ids.push("<%=c.getIdCost()%>");
years.push("<%=u.getYear()%>");
times.push("<%=u.getTime()%>");
cfs.push("<%=u.getCF()%>");
<%
}
%>
Then insert an onchange="setCost(<%=id%>);" function into the select and add the JavaScript function
function setCost(id) {
for(var i = 0; i < ids.length; i++) {
if(ids[i] == id) {
document.getElementsByName("years")[0].value = years[i];
document.getElementsByName("cost")[0].value = costs[i];
document.getElementsByName("cf")[0].value = cfs[i];
}
}
}
Try this <select><%for(Cost c: costs) {%><option value="<%=c.getIdCost()%>" onchange="setCost(<%=c.getIdCost()%>)"><%=c.getYear()%></option><%}%></select>
I'm trying to make a login page powered by asp and sql. After clicking submit, the form will not even trigger (i.e. the error message will not even display if the fields are blank)
Here is my code:
<!DOCTYPE html>
<html>
<title>
Query
</title>
<link href="http://hr-computing/public/AlexS/Tests/Site.css" rel="stylesheet">
<body>
<div id="main">
<%
username = ""
password = ""
ErrorMessage = ""
if request.form <> "" then
username = Request.Form("firstname")
password = Request.Form("password")
if username = "" or password = "" then
ErrorMessage = "You must specify a username and password."
else
set conn=Server.CreateObject("ADODB.Connection")
conn.Open ="{private}
set rs=Server.CreateObject("ADODB.recordset")
set rs = Server.CreateObject("ADODB.recordset")
rs.Open "Select firstname, PASSWORD FROM teachers WHERE firstname = '" & firstname & "'", conn
if rs.EOF = false then
if rs.fields("Password") = password then
Response.Redirect("http://hr-computing/public/AlexS/Tests/default.asp")
end if
end if
ErrorMessage = "Login failed"
end if
end if
%>
<h1>Login</h1>
<form method="post" action="http://hr-computing/public/AlexS/Tests/login.asp">
<fieldset>
<legend>Log In to Your Account</legend>
<ol>
<li>
<label>Username:</label>
<input type="text" id="firstname" name="firstname" />
</li>
<li>
<label>Password:</label>
<input type="password" id="password" name="password" />
</li>
<li>
<p><input type="submit" value="Login" /></p>
</li>
</ol>
</fieldset>
</form>
</div>
</body>
</html>
Someone tell me what is wrong, please.
Where do you print out your error message? It doesn't look like you have that displayed anywhere. Classic ASP is kind of painful about this stuff - but somewhere you need a
<%= ErrorMessage %>
call to display the login error.
As you are using asp.net so for validating username and password for empty you can use required field validator. Be sure to set validation group to both the textbox as well as submit button.
This is my index.aspx form.
<form role="form" method="post" action="SendMail.aspx">
<div class="form-group">
<input type="text" class="form-control" id="name" name="name" placeholder="Name"
required>
</div>
<div class="form-group">
<input type="text" class="form-control" id="mobile" name="mobile" placeholder="Mobile Number"
required>
</div>
<div class="form-group">
<input type="email" class="form-control" id="email" name="email" placeholder="Email"
required>
</div>
<button type="submit" id="submit" name="submit" class="btn btn-primary pull-right">
Submit Form</button>
</form>
And this is my SendMail.aspx form.
<%
Response.Write("Need : " & Request.Form("whatneed") & "<br>")
Response.Write("Budget : " & Request.Form("budget") & "<br>")
Response.Write("When : " & Request.Form("whenneed") & "<br>")
Response.Write("Location : " & Request.Form("location") & "<br>")
Response.Write("Name : " & Request.Form("name") & "<br>")
Response.Write("Description : " & Request.Form("address") & "<br>")
Response.Write("Mobile No : " & Request.Form("mobile") & "<br>")
Response.Write("Landline No : " & Request.Form("landline") & "<br>")
Response.Write("Email Id : " & Request.Form("email") & "<br>")
MailMessage mailMessage = new MailMessage();
mailMessage.To.Add("saravana17.ams#gmail.com");
mailMessage.From = new MailAddress("saro17.ams#gmail.com");
mailMessage.Subject = "ASP.NET e-mail test";
mailMessage.Body = "Hello world,\n\nThis is an ASP.NET test e-mail!";
SmtpClient smtpClient = new SmtpClient("mail.feo.co.in");
smtpClient.Send(mailMessage);
Response.Write("E-mail sent!");
%>
I don't know why mail is not sending here.Please help me to fix this.
As per your JsFiddle, I found that there are so many silly mistakes in your HTML code.
Here is your ASP.NET code:-
<form id="form1" runat="server">
<div class="form-group">
<asp:TextBox ID="txtname" runat="server" CssClass="form-control"></asp:TextBox>
</div>
<div class="form-group">
<asp:TextBox ID="txtmobileno" runat="server" CssClass="form-control"></asp:TextBox>
</div>
<div class="form-group">
<asp:TextBox ID="txtEmail" runat="server" CssClass="form-control"></asp:TextBox>
</div>
<div class="form-group">
<asp:TextBox ID="txtSubject" runat="server" CssClass="form-control"></asp:TextBox>
</div>
<asp:Button ID="btnSubmit" runat="server" CssClass="btn btn-primary pull-right" OnClick="btnSubmit_OnClick" Width="100" Text="Submit" />
</form>
Code behind cs code
Created a SendMail() function which will fire on buttonclick
Note:- I haven't added validations on the controls, so if you want it you can add as per your requirement.
protected void SendMail()
{
// Gmail Address from where you send the mail
var fromAddress = "Gmail#gmail.com";
// any address where the email will be sending
var toAddress = txtEmail.Text.ToString();
//Password of your gmail address
const string fromPassword = "Your gmail password";
// Passing the values and make a email formate to display
string subject = txtSubject.Text.ToString();
// Passing the values and make a email formate to display
string body = "From: " + txtname.Text + "\n";
body += "Email: " + txtEmail.Text + "\n";
// smtp settings
var smtp = new System.Net.Mail.SmtpClient();
{
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
smtp.Timeout = 20000;
}
// Passing values to smtp object
smtp.Send(fromAddress, toAddress, subject, body);
}
Now the above function will be called on Button click so that every time you enter the details you can call that function.
protected void btnSubmit_OnClick(object sender, EventArgs e)
{
try
{
SendMail(); // Send mail function to send your mail
txtname.Text = "";
txtEmail.Text = "";
txtmobileno.Text = "";
txtSubject.Text = "";
}
catch (Exception ex)
{
ex.Message.ToString();
}
}
For a detail explanation have a look at below link:-
http://www.codeproject.com/Tips/371417/Send-Mail-Contact-Form-using-ASP-NET-and-Csharp