I need to pass from search.jsp the id of the checked row of the table to delete.jsp in order to delete it , but when i am doing this with the bellow code , nothing happens , none checked row deleted from database
delete.jsp
<%
Class.forName("com.mysql.jdbc.Driver");
String myDatabase = "jdbc:mysql://localhost:3306/project_app?user=root&password=1234";
Connection myConnection = DriverManager.getConnection(myDatabase);
Statement myStatement = myConnection.createStatement();
myStatement.executeUpdate("DELETE * FROM Dest WHERE idDest ='"+request.getParameter("id1")+"' ");
myStatement.close();
myConnection.close();
%>
search.jsp
<%
int id2;
Class.forName("com.mysql.jdbc.Driver");
String myDatabase = "jdbc:mysql://localhost:3306/project_app?user=root&password=1234";
Connection myConnection = DriverManager.getConnection(myDatabase);
Statement myStatement = myConnection.createStatement();
String search=request.getParameter("search");
ResultSet rs = myStatement.executeQuery("SELECT * FROM dest WHERE Country='"+search+"' OR City='"+search+"'");
if (rs.next()) {
%>
<table style="width:100%">
<table id="Dest_table">
<tr>
<th id="chke">Check </th>
<th id="Country">Country</th>
<th id="City">City</th>
<th id="URL">Url of Destination</th>
<th id= "DestNO">Dest no </th>
<th id="Act"> Action</th>
</tr>
<tr> <%while(rs.next()){ %>
<td><input type="checkbox" name=chk onclick='window.location.assign('delete.jsp?id1='<%=rs.getString("idDest") %>")' /> </td>
<td> <%=rs.getString("Country") %></td>
<td> <%=rs.getString("City") %> </td>
<td> <a href=<%=rs.getString("Url") %> > <%=rs.getString("URL") %> </a>
<td> <%=rs.getString("idDest") %> </td>
<TD><a href="delete.jsp" class="button cross" ></a>
<TD><input <a href="edit.jsp" type="submit" name="edit" value="Edit" style="background-color: ff0000;font-weight:bold;color:#ffffff;"></TD>
</TR>
</tr>
<% }}
else { out.println("NOT FOUND Try Again"); }%>
</table>
<%
myStatement.close();
myConnection.close(); %>
Related
I have a button inside a table for each database entry. As forms cannot be in tables I can't use the form to make a request. I added a little portion of javascript to each button that should make a get request with the database id and redirect to a different page. It does not seem to be working. I tested creating a dummy form and making the same get request on the page and that worked. Any ideas why this isn't working?
Nodejs file Router
const express = require('express')
const router = express.Router()
router.get('/testRedirect/:id', (req, res) => res.render('pages/about'))
module.exports = router
<!DOCTYPE html>
<html>
<head>
<% include ../partials/header.ejs %>
</head>
<body>
<% include ../partials/nav.ejs %>
<div class="container">
<table class="table">
<thead>
<tr>
<th scope="col">Id</th>
<th scope="col">Name</th>
<th scope="col">Edit</th>
<th scope="col">Delete</th>
</tr>
</thead>
<tbody>
<% results.forEach(function(r) { %>
<tr>
<th scope="row">
<%= r.id %>
</th>
<th scope="row">
<%= r.name %>
</th>
<td>
<button type="submit" id=<%=r.quiz_id %> class="btn btn-primary">Edit</button>
<script type="text/javascript">
$(function() {
var id = <%=r.id %>
var idString = "#" + id
$(idString).click(function() {
$.get('/testRedirect/' + id)
})
});
</script>
</td>
<td>
<button type="submit" id=<%=r.Id %> class="btn btn-primary">Delete</button>
<script type="text/javascript">
</script>
</td>
</tr>
<% }); %>
</tbody>
</table>
</div>
<form action="/testRedirect/1" method="GET">
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</body>
</html>
Do you realy need a script block for every button. Can't you use onclick attribute:
<button type="button" id=<%=r.quiz_id %> onclick="location.href='/testRedirect/#<%=r.quiz_id %>'" class="btn btn-primary">Edit</button>
This is my first question on StackOverflow. When following RESTful routes, you can make a delete button that destroys a single page and entry in MongoDB.
My question is how to make a delete button that destroys only a single table row and the same data from MongoDB? I can add a contact successfully, but I don't know how to delete it from a page
This is my code for a table that needs modification:
<table class="table table-bordered">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Telephone Number</th>
<th>Action</th>
</tr>
</thead>
<% phonebook.forEach(function(contact){ %>
<tbody>
<tr>
<td>
<%= contact.firstName %>
</td>
<td>
<%= contact.lastName %>
</td>
<td>
<%= contact.phone %>
</td>
<td>
<form action="/phonebook?_method=DELETE" method="POST">
<button class="btn btn-danger">Delete</button>
</form>
</td>
</tr>
</tbody>
<% }); %>
</table>
And this is my code from routes, that needs modification:
//DESTROY
app.delete("/phonebook/:id", function(req, res){
Phonebook.findByIdAndRemove(req.params.id, function(err){
if(err){
res.redirect("/phonebook");
} else {
res.redirect("/phonebook");
}
});
});
Change this line:
<form action="/phonebook?_method=DELETE" method="POST">
To:
<form action="/phonebook/<%= contact.id %>?_method=DELETE" method="POST">
I have a small Backbone / Underscore app. I have rendered one template, along with rendering a method call "onclick" of a button (which works smoothly, btw). The function which is called, renders another template, which has another method call "onclick" of a button.
#showTransportsTemplate: first template / has a "transport" model passed to it
bookTicket(tid): first function / uses "tid" to fetch the associated transport model from the collection AND sets them into a "booking model" which is passed on to the second template
#confirmBookingTemplate: second template
confirmBooking() : second method (which exists but is not being recognized and hence not called)
Here is the code:
related part of app.js:
function bookTicket(tid)
{
alert("received tid: "+tid);
var transport = transportList.findWhere({id:tid});
console.log(transport);
var newBooking = new Booking();
newBooking.set('id',"b"+getBookingId());
newBooking.set('mode',transport.get('mode'));
newBooking.set('source',transport.get('source'));
newBooking.set('destination',transport.get('destination'));
newBooking.set('date',transport.get('date'));
newBooking.set('class',transport.get('class'));
newBooking.set('rate',transport.get('rate'));
var confirmBookingTemplate = _.template($('#confirmBookingTemplate').html(), {booking: newBooking});
alert(confirmBookingTemplate); // for testing purposes
$(confirmBooking.el).show();
$(confirmBooking.el).html(confirmBookingTemplate);
}
function confirmBooking()
{
alert("confirmBooking"); // no further code written coz this function is not getting called
}
The two templates:
<script type="text/template" id="showTransportsTemplate">
<table border="1">
<tr>
<th> Source </th>
<th> Destination </th>
<th> Date Available </th>
<th> Class </th>
<th> Rate </th>
<th> Book </th>
</tr>
<% _.each(selTransports, function (transport) {
var myid= transport.get("id");
%>
<tr>
<td align="center"> <%= transport.get("source") %> </td>
<td align="center"> <%= transport.get("destination") %> </td>
<td align="center"> <%= transport.get("date") %> </td>
<td align="center"> <%= transport.get("class") %> </td>
<td align="center"> <%= transport.get("rate") %> </td>
<td> <input type="button" onclick="bookTicket('<%= myid %>')" value="Book">
</input>
</td>
<td> <%= myid %> </td>
</tr>
<%
});
%>
</table>
</script>
<script type="text/template" id="confirmBookingTemplate">
<br /><br />
<h3> Booking Details </h3>
<br />
<table cellspacing="3" cellpadding="3">
<tr>
<td> <b> Customer Name: </b> </td>
<td> <input type="text" id="cname" /> </td>
</tr>
<tr>
<td colspan="2">
<%
var travel;
if (booking.get("mode") === "F") travel = "Flight"
else if (booking.get("mode") === "B") travel = "Bus"
else if (booking.get("mode") === "T") travel = "Train"
%>
<b><%= travel %></b> from <b><%=booking.get("source")%></b> to <b><%=booking.get("destination")%></b>
in <b><%=booking.get("class")%></b> class on <b><%=booking.get("date")%></b>
</td>
</tr>
<tr>
<td> <b> Cost per ticket: </b> </td>
<td> Rs. <%= booking.get("rate") %> </td>
</tr>
<tr>
<td> <b> No. of Adults: </b> </td>
<td> <input type="text" id="adults" /> </td>
</tr>
<tr>
<td> <b> No. of Children: </b> </td>
<td> <input type="text" id="children" /> </td>
</tr>
<tr>
<td> <b> Total Amount: </b> </td>
<td> <output id="totalAmount"> </output> </td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="button" value="Book Ticket" id="confirm" onclick="confirmBooking()">
</td>
</tr>
</table>
</script>
I tried with Chrome, the error was "object is not a function". Firebug says: "confirmBooking()" is not a function, BUT IT ALREADY EXISTS.
Underscore's template function uses a javascript with block when evaluating templates, which means that any "unqualified names" are assumed to be part of the data object passed-in to the template method. In this case, confirmBooking is not part of that object, and so the template is not able to find it.
If you truly wish to invoke a global function from within your template (and you should think twice about defining functions in the global namespace), then you may do so by explicitly referencing the window object, like so:
window.someGlobalFunction = function() {
return "It worked!";
}
<script type="text/template" id="template">
<%= window.someGlobalFunction() %>
</script>
A functioning example: http://jsfiddle.net/dmillz/pqjtR/
I am trying to create a table, whereas when the user clicks on one of the rows, a block will slide down the same length of the table. I tried using a div, but I guess you can't have a div anywhere in the table unless inside of a cell. Here is my current code:
<table class="table-carrier">
<thead>
<tr>
<th>
<%= Html.Encode(SFSys.Inst.I18n.GetText("Name")) %>
</th>
<th>
<%= Html.Encode(SFSys.Inst.I18n.GetText("Value")) %>
</th>
<th><!-- Data to be added later --></th>
</tr>
</thead>
<% foreach (var Carrier in ViewData.Model.Carriers) { %>
<tbody>
<tr>
<td class="carrier-row" onclick="$(this)
.closest('tbody')
.next('.section')
.slideToggle('fast');">
<%= Html.Encode(Carrier.Name) %>
</td>
<td>
<%= Html.Encode(Carrier.Value) %>
</td>
<td>
<%using(Html.BeginForm("Action", "Form")) {%>
<input type="hidden" name="FormKey" id="FormKey" value="<%= this.Request.Url.Query %>" />
<input type="submit" name="action" value="<%= Html.Encode(SFSys.Inst.I18n.GetText("Change")) %>" onclick='scriptForward = true' />
<%}%>
</td>
</tr>
</tbody>
<tbody class="section">
<tr>
<td colspan="3">
<div >
<table>
<tr>
<td>left 1</td>
<td>right 1</td>
</tr>
<tr>
<td>left 2</td>
<td>right 2</td>
</tr>
</table>
</div>
</td>
</tr>
</tbody>
<% } %>
</table>
I am able to get each section to come up on its own, but when it does this, it alters my table, slides the information down from one cell, then stretches it over. Is there a way to make the information slide down at the length of the table?
Although I was unable to find a proper solution, using the jQuery Accordion objects managed to create what I was looking for. More information about that can be found at jQuery Accordion
I have a user preference page in which the user can type in their current password, new password and confirm new password. When the user submits the form it sends them to an action. The problem is that when the form submits, the password fields stay filled. I want them to clear. I attempted javascript but when I use document.preferences.submit(), the submit isn't fired, or the action isn't fired, or both. Here is the code:
<script language="JavaScript" type="text/javascript">
function clearpwd() {
document.preferences.submit();
document.preferences.currentPassword.value = '';
document.preferences.newPassword.value = '';
document.preferences.confirmPassword.value = '';
}
</script>
<% using (Html.BeginForm("ChangePreferences", "Home", FormMethod.Post, new { id="preferences", name="preferences" })) { %>
<div>
<div style="text-align:left; width:500px;">
<div class="FormLabel">User Information:</div>
<table border="0" cellpadding="4" cellspacing="0">
<tr>
<td>First Name:</td>
<td>
<%= Html.TextBox("firstName")%>
<%= Html.ValidationMessage("FirstName") %>
</td>
</tr>
<tr>
<td>Last Name:</td>
<td>
<%= Html.TextBox("lastName") %>
<%= Html.ValidationMessage("LastName") %>
</td>
</tr>
<tr>
<td>New email:</td>
<td>
<%= Html.TextBox("email")%>
<%= Html.ValidationMessage("Email") %>
</td>
</tr>
<tr>
<td>Billing Method:</td>
<td>
<%=Html.RadioButton("billingMethod", 'E', new { id = "BillingMethod_E", value = 'E' })%><label for="BillingMethod_E">Electronic</label>
<%=Html.RadioButton("billingMethod", 'M', new { id = "BillingMethod_M", value = 'M' })%><label for="BillingMethod_M">Mailing</label>
</td>
</tr>
</table>
</div>
<br /><br />
<div style="text-align:left; width:500px;">
<div class="FormLabel">Login and Security Information:</div>
<table border="0" cellpadding="4" cellspacing="0">
<tr>
<td>Current Password</td>
<td>
<%= Html.Password("currentPassword") %>
<%= Html.ValidationMessage("currentPassword") %>
</td>
</tr>
<tr>
<td>New password:</td>
<td>
<%= Html.Password("newPassword") %>
<%= Html.ValidationMessage("newPassword") %>
</td>
</tr>
<tr>
<td>Confirm new password:</td>
<td>
<%= Html.Password("confirmPassword") %>
<%= Html.ValidationMessage("confirmPassword") %>
</td>
</tr>
<tr>
<td>Security Question:</td>
<td>
<%= Html.TextBox("securityQuestion") %>
<%= Html.ValidationMessage("SecurityQuestion") %>
</td>
</tr>
<tr>
<td>Security Answer:</td>
<td>
<%= Html.Password("securityAnswer") %>
<%= Html.ValidationMessage("SecurityAnswer") %>
</td>
</tr>
</table>
</div>
<br /><br />
<table>
<tr>
<td></td>
<td><input type="submit" value="Change Preferences" onclick="clearpwd()" /></td>
</tr>
</table>
</div>
<% } %>
To Debug JavaScript Problems, better post the generated HTML
To debug JavaScript problems easier, use Firefox browser with Firebug Extension.
Edit: You are trying to clear the password, after page is submitted. After submission page navigates to another page, so i think, JavaScript won't affect any DOM elements after that.
You can try the following sequence.
Store Username and Password on JavaScript variables.
clear Username and Password values.
Post to the page with the stored values.
It could be your browser filling in the password field if you have it set to remember your password(s). I've had this happen to me.