I am loading an Ajax form with Smarty and jQuery.
It should load this:
{foreach $topFEED as $article}
<tr>
<td><form action='postbox.php' method='POST'></td>
<td><img class="favicon_prev" src="http://www.google.com/s2/favicons?domain={$article.img_link}"> <input type="text" name="link" style="width: 300px;" value="{$article.link}" READONLY/></td>
{if $loggedin}<td><input type='submit' id="abo" name="submit" value='Abonnieren' /></td>
<input type='hidden' name="action" value='add_feed' READONLY />{/if}
<td></form></td>
</tr>
{/foreach}
Ajax Code:
$.ajax({
type: 'GET',
url: "article.php?method=top_feeds",
dataType: 'html',
})
.done(function(html) {
$("#ajax_top_feeds").replaceWith(html);
});
Result:
<tr>
<td><form action="postbox.php" method="POST"></form></td>
<td><img class="favicon_prev" src="http://www.google.com/s2/favicons?domain=http://www.n-tv.de"> <input type="text" name="link" style="width: 300px;" value="http://www.n-tv.de/rss" readonly=""></td>
<td><input type="submit" id="abo" name="submit" value="Abonnieren"></td>
<input type="hidden" name="action" value="add_feed" readonly=""> <td></td>
</tr>
Your HTML is invalid, the browser is simply correcting it for you. form elements can't cross multiple table cells like that.
You can wrap a form around your entire table:
<form>
<table>
<!--- etc. --->
</table>
</form>
Or you can place a form inside of a table cell:
<table>
<tr>
<td>
<form>
<!--- etc. --->
</form>
</td>
</tr>
</table>
But you can't begin a form in one cell and end it in another. The hierarchy of HTML markup isn't structured that way. Tags must be closed before parent tags are closed.
Related
I'm working on dynamic 3 column table that has 3 input text type : Product, quantity and date
This works good but i want to change type format:
Product column => Dropdown type
Quantity column => number type
Date column => Date type (dd-mm-yyyy)
How i do change it?
Thank you
Original source: link
Code online: https://jsfiddle.net/bvotcode/r61ptxe9/7/
HTML
<body>
<div id="wrapper">
<table align='center' cellspacing=2 cellpadding=5 id="data_table" border=1>
<tr>
<th>Product</th>
<th>Quantity</th>
<th>Date</th>
</tr>
<tr id="row1">
<td id="product_row1">Car</td>
<td id="quantity_row1">10</td>
<td id="date_row1">20/12/2021</td>
<td>
<input type="button" id="edit_button1" value="Edit" class="edit" onclick="edit_row('1')">
<input type="button" id="save_button1" value="Save" class="save" onclick="save_row('1')">
<input type="button" value="Delete" class="delete" onclick="delete_row('1')">
</td>
</tr>
<tr id="row2">
<td id="product_row2">Book</td>
<td id="quantity_row2">22</td>
<td id="date_row2">26</td>
<td>
<input type="button" id="edit_button2" value="Edit" class="edit" onclick="edit_row('2')">
<input type="button" id="save_button2" value="Save" class="save" onclick="save_row('2')">
<input type="button" value="Delete" class="delete" onclick="delete_row('2')">
</td>
</tr>
<tr id="row3">
<td id="product_row3">Laptop</td>
<td id="quantity_row3">44</td>
<td id="date_row3">19/01/2022</td>
<td>
<input type="button" id="edit_button3" value="Edit" class="edit" onclick="edit_row('3')">
<input type="button" id="save_button3" value="Save" class="save" onclick="save_row('3')">
<input type="button" value="Delete" class="delete" onclick="delete_row('3')">
</td>
</tr>
<tr>
<td><input type="text" id="new_product"></td>
<td><input type="text" id="new_quantity"></td>
<td><input type="text" id="new_date"></td>
<td><input type="button" class="add" onclick="add_row();" value="Add Row"></td>
</tr>
</table>
</div>
</body>
</html>
</body>
CSS
.pt-3-half { padding-top: 1.4rem; }
Javascript
function edit_row(no)
{
document.getElementById("edit_button"+no).style.display="none";
document.getElementById("save_button"+no).style.display="block";
var product=document.getElementById("product_row"+no);
var quantity=document.getElementById("quantity_row"+no);
var date=document.getElementById("date_row"+no);
var product_data=product.innerHTML;
var quantity_data=quantity.innerHTML;
var date_data=date.innerHTML;
product.innerHTML="<input type='text' id='product_text"+no+"' value='"+product_data+"'>";
quantity.innerHTML="<input type='text' id='quantity_text"+no+"' value='"+quantity_data+"'>";
date.innerHTML="<input type='text' id='date_text"+no+"' value='"+date_data+"'>";
}
function save_row(no)
{
var product_val=document.getElementById("product_text"+no).value;
var quantity_val=document.getElementById("quantity_text"+no).value;
var date_val=document.getElementById("date_text"+no).value;
document.getElementById("product_row"+no).innerHTML=product_val;
document.getElementById("quantity_row"+no).innerHTML=quantity_val;
document.getElementById("date_row"+no).innerHTML=date_val;
document.getElementById("edit_button"+no).style.display="block";
document.getElementById("save_button"+no).style.display="none";
}
function delete_row(no)
{
document.getElementById("row"+no+"").outerHTML="";
}
function add_row()
{
var new_product=document.getElementById("new_product").value;
var new_quantity=document.getElementById("new_quantity").value;
var new_date=document.getElementById("new_date").value;
var table=document.getElementById("data_table");
var table_len=(table.rows.length)-1;
var row = table.insertRow(table_len).outerHTML="<tr id='row"+table_len+"'><td id='product_row"+table_len+"'>"+new_product+"</td><td id='quantity_row"+table_len+"'>"+new_quantity+"</td><td id='date_row"+table_len+"'>"+new_date+"</td><td><input type='button' id='edit_button"+table_len+"' value='Edit' class='edit' onclick='edit_row("+table_len+")'> <input type='button' id='save_button"+table_len+"' value='Save' class='save' onclick='save_row("+table_len+")'> <input type='button' value='Delete' class='delete' onclick='delete_row("+table_len+")'></td></tr>";
document.getElementById("new_product").value="";
document.getElementById("new_quantity").value="";
document.getElementById("new_date").value="";
}
You're using the type="text" for all of the <input> tags. You should consider changing the type of the inputs in your edit_row function.
For numbers, the type of input should be number;
For dates, date;
And finally, to render a dropdown box, the best solution is to use a <select> tag instead of an <input>. Don't forget to write all the options of your dropdown inside of <option> tags (which should be all inside of one <select> tag); Here you can find a simple explanation on how to use the <select> tag.
I have a table which show product's files. A file can have a note added. If a note was added, it is displayed in a row. If not, text area field with submit button is displayed instead.
Short story, it all works, except for the first row without a note. After typing a note and clicking submit button nothing happens.
HTML:
<div id="files-list" style="display: none">
<table class="table">
<thead>
<tr>
<th>File</th>
<th>Date</th>
<th>Notes</th>
<th></th>
</tr>
</thead>
<tbody>
<c:if test="${fn:length(bean.product.files)>0}">
<c:forEach items="${bean.product.files}" var="na">
<tr>
<td>${na.name}</td>
<td><fmt:formatDate value="${na.created}" pattern="yyyy-MM-dd HH:mm" /></td>
<td>
<c:if test="${empty na.note}">
<form:form id="${na.id}" method="POST" modelAttribute="bean" action="${pageContext.request.contextPath}/app/updateFile.ajax?id=${bean.product.id}&fileId=${na.id}" style="display: inline">
<form:textarea path="prodFiles" rows="1" cols="50" />
<input type="hidden" name="version" value="${bean.product.version}">
<input type="submit" id="submitButton" value="Save it!" />
</form:form>
</c:if>
<c:if test="${not empty na.note}">
<div style="max-width: 400px">${na.note}</div>
</c:if>
</td>
<td><button type="button" class="btn btn-xs btn-danger" onclick="$().mkdelformTable('${pageContext.request.contextPath}/app/deleteFile.ajax?id=${bean.product.id}&fileId=${na.id}', this)">Delete it!</button></td>
</tr>
</c:forEach>
</c:if>
</tbody>
</table>
</div>
Possible hints:
When I open the table (it's a pop-up) console shows multiple [DOM] Found X elements with non-unique id errors
Checking elements in dev's tools I noticed that the first row doesn't include the form part. Possibly connected with the multiple-same-ids problem. Example:
<td>
<textarea id="prodFiles" name="prodFiles" rows="1" cols="50"></textarea>
<input type="hidden" name="version" value="181">
<input type="submit" id="submitButton" value="Save it!">
</td>
VS
<td>
<form id="65" style="display: inline" action="/oferty/app/updateFile.ajax?id=12701&fileId=65" method="POST">
<textarea id="prodFiles" name="prodFiles" rows="1" cols="50"></textarea>
<input type="hidden" name="version" value="181">
<input type="submit" id="submitButton" value="Save it!">
</form>
</td>
Edit: Also, new files can be added so hard-coding different names/ids can't be done, if suggested.
As I found out, there was some legacy code. I managed to get rid of 'multiple same ids' etc. errors by adding a note in an additional pop-up - on a previos page, instead of an input, I included a button to the new pop-up.
I am trying to create a simple login page for a server:
<html>
<head>
<title>Login Page</title>
</head>
<body>
<form name="loginForm" method="post" action="login.php">
<table>
<tr>
<td colspan=2><center><font size=4><b>HTML Login Page</b></font>
</center></td>
</tr>
<tr>
<td>Username:</td>
<td><input type="text" size=25 name="userid"></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="Password" size=25 name="pwd"></td>
</tr>
<tr>
<td ><input type="Reset"></td>
<td><input type="submit" onclick="return check(this.form); load();"
value="Login">
</td>
</tr>
</table>
</form>
<script language="javascript">
function check(form)
{
if(form.userid.value == "n" && form.pwd.value == "n")
{
return true;
}
else
{
alert("Error Password or Username")
return false;
}
</script>
</body>
</html>
The HTML above is how i would like the page to look, however i want to add another function that when the login button is pressed i would like the page to take me to a different page like google for example.
Is there anyway I can do this?
Through Html you can do something like:
<form action="http://google.com">
<input type="submit" value="Go to Google">
</form>
Hi all i have login form design in html 5 and depending on login details enterd it shoud redirect to respective page. i am checking login details in javascript and redirecting page using javascript but its not working at all. page is not getting redirect.
<form id="html5form" method="post">
<table border="0" cellpadding="0" cellspacing="0" width="100%" class="tbl_form">
<tbody>
<tr>
<td>
<label>Username: <span>*</span></label>
</td>
<td>
<input type="text" name="txtLoginNm" id="txtLoginNm" class="field" required="required" placeholder="Enter Username" onkeydown="fnAdmPassword(e);"/>
</td>
</tr>
<tr>
<td>
<label>Password: <span>*</span></label>
</td>
<td>
<input type="password" name="txtPwd" id="txtPwd" maxlength="20" class="field" required="required" Placeholder="Enter Password" onkeydown="fnAdmPassword(e);"/>
</td>
</tr>
<tr>
<td>
</td>
<td>
<input type="submit" title="Login" value="Login" name="Login" class="buttom" onclick="javascript:return fnlogin();" />
</td>
</tr>
</tbody>
</table>
</form>
//// and the javascript function is as follows
function fnlogin()
{
if ((document.getElementById("txtLoginNm").value == "") && (document.getElementById("txtPwd").value == ""))
{
alert("Please enter Username & Password!");
}
else if ((document.getElementById("txtLoginNm").value == "user155") && (document.getElementById("txtPwd").value == "user155"))
{
window.location.href = "comp-admin/u_dashboard.shtm";
}
else if ((document.getElementById("txtLoginNm").value == "superadmin") && (document.getElementById("txtPwd").value == "superadmin")) {
window.location.href = "s-admin/edit-profile.shtm";
}
else (alert("Invalid username or password!"))
}
please tell me why page is not getting redirect
This is why it is not redirecting
<input type="submit" title="Login" value="Login" name="Login" class="buttom" onclick="javascript:return fnlogin();" />
You have to make 2 changes here
Change button type from submit to just button, because then submit functionality is taking precedence and you have no action url defined
Remove javascript: from your call. (As also suggested by Jan Hančič in comments)
So try this
<input type="button" title="Login" value="Login" name="Login" class="buttom" onclick="return fnlogin();" />
Is it OK to write a form within the tr tag?
<table>
% for my $word ( #$words_2 ) {
<tr>
<form action="/blacklist" method="post">
<td><%=$word%></td>
<td><input type="text" name="data" readonly hidden value="<%=$word%>" /></td>
<td><input class="remove" type="submit" value="Remove" /></td>
</form>
</tr>
% }
</table>
The tr doesn't allow form-tags as direct children. Most modern browsers will let you do a lot of crap and so you could use this - but I wouldn't call it OK. A better approach would be to but the complete form into one of the tds (tds allow text, forms, inline- and block-elements as children):
<table>
<% for my $word ( #$words_2 ) { %>
<tr>
<td><%=$word%></td>
<td>
<form action="/blacklist" method="post">
<input type="text" name="data" readonly hidden value="<%=$word%>" />
<input class="remove" type="submit" value="Remove" />
</form>
</td>
</tr>
<% } %>
</table>
or, a lot easier, simply use a link (but note that data gets sent using GET instead of POST - maybe you'll have to change something in your code that handles the blacklisting):
<table>
<% for my $word ( #$words_2 ) { %>
<tr>
<td><%=$word%></td>
<td>Remove</td>
</tr>
<% } %>
</table>
Is it OK to write a form within the tr tag?
No. Forms can contain tables. Table cells can contain forms.
I'd approach this problem like so:
<form action="/blacklist" method="post">
<fieldset>
<legend>Remove</legend>
% for my $word ( #$words_2 ) {
<label>
<input type="checkbox" name="data" value="<%=$word%>" />
<%=$word%>
</label>
% }
</fieldset>
<input class="remove" type="submit" value="Remove" />
</form>
No, that is not correct. The form tag has to be outside the table or inside a table cell.
Putting the form tag inside the table is an old trick to keep the form from taking up extra space. You should just use CSS for that:
form { margin: 0; padding: 0; }